controlling the gpios of the raspberry pi in c pi/gpio... · using the gpio pins of the raspberr pi...

5
Using the GPIO pins of the Raspberr Pi 12.02.2016 ©2016 Simon Burkhardt 1 / 5 Controlling the GPIOs of the Raspberry Pi in c Installing the necessary tools You can technically follow all the instructions from http://wiringpi.com/ IDE (optional) Geany C/C+++ editor $ sudo apt-get install geany Tools git git cient for cloning projects $ sudo apt-get install git-core Resources wiringPi repository C libraries for GPIOs $ git clone git://git.drogon.net/wiringPi $ cd wiringPi $ git pull origin $ ./build GPIO Pin Maping according to: http://pi4j.com/pins/model-2b-rev1.html

Upload: tranthuan

Post on 14-Mar-2018

248 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Controlling the GPIOs of the Raspberry Pi in c pi/GPIO... · Using the GPIO pins of the Raspberr Pi 12.02.2016 ©2016 Simon Burkhardt 1 / 5 Controlling the GPIOs of the Raspberry

Using the GPIO pins of the Raspberr Pi

12.02.2016 ©2016 Simon Burkhardt 1 / 5

Controlling the GPIOs of the Raspberry Pi in c

Installing the necessary tools

You can technically follow all the instructions from http://wiringpi.com/

IDE (optional)

Geany C/C+++ editor $ sudo apt-get install geany

Tools

git git cient for cloning projects $ sudo apt-get install git-core

Resources

wiringPi repository C libraries for GPIOs $ git clone git://git.drogon.net/wiringPi $ cd wiringPi $ git pull origin $ ./build

GPIO Pin Maping

according to: http://pi4j.com/pins/model-2b-rev1.html

Page 2: Controlling the GPIOs of the Raspberry Pi in c pi/GPIO... · Using the GPIO pins of the Raspberr Pi 12.02.2016 ©2016 Simon Burkhardt 1 / 5 Controlling the GPIOs of the Raspberry

Using the GPIO pins of the Raspberr Pi

12.02.2016 ©2016 Simon Burkhardt 2 / 5

Controlling the GPIOs through the console / ssh

OUTPUT

Initialize the GPIO pin with the following command:

(sets GPIO 0 as output)

Write the pin as follows:

(sets the GPIO 0 high)

(sets GPIO 0 low)

INPUT

Initialize the GPIO pin with the following command:

(sets GPIO 2 as output)

Read the pin as follows:

(note, that you don’t need an “echo” command or something)

Outputs the following if the pin is low:

0

Outputs the following if the pin is high:

1

$ gpio mode 0 out

$ gpio write 0 1

$ gpio write 0 0

$ gpio mode 2 in

$ gpio read 2

Page 3: Controlling the GPIOs of the Raspberry Pi in c pi/GPIO... · Using the GPIO pins of the Raspberr Pi 12.02.2016 ©2016 Simon Burkhardt 1 / 5 Controlling the GPIOs of the Raspberry

Using the GPIO pins of the Raspberr Pi

12.02.2016 ©2016 Simon Burkhardt 3 / 5

Programming in c

The overall syntax of the wiring library is the same as in wiring (or Arduino).

Open the Geany editor (Start > Programming > Geany)

File > New

Enter the following “blinky” code:

1 2 3 4 5 6 7 8 9

10 11 12 13

#include <wiringPi.h> int main ( void ) { wiringPiSetup(); pinMode( 0, OUTPUT); for (;;){ digitalWrite( 0, HIGH); delay( 500 ); digitalWrite( 0, LOW); delay( 500 ); } }

Save the file as “blink.c” or whatever you want to call it.

Open the console and navigate to the place you stored the blink.c file

Compile it with the following command:

Execute the program by entering the generated file:

The connected LED should blink forever, until you kill the program by pressing Ctrl + C.

$ gcc –o blink blink.c –lwiringPi

$ ./blink

Page 4: Controlling the GPIOs of the Raspberry Pi in c pi/GPIO... · Using the GPIO pins of the Raspberr Pi 12.02.2016 ©2016 Simon Burkhardt 1 / 5 Controlling the GPIOs of the Raspberry

Using the GPIO pins of the Raspberr Pi

12.02.2016 ©2016 Simon Burkhardt 4 / 5

Programming a PWM in C

The PWM of this library is software generated, so you need to include some more libraries to compile it.

Here’s an example:

1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16

#include <wiringPi.h> #include <softPwm.h> int main ( void ) { wiringPiSetup(); pinMode( 0, OUTPUT); pinMode( 1, OUTPUT); softPwmCreate( 1, 10, 100); // 10% at a period duration of 100 for (;;){ digitalWrite( 0, HIGH); delay( 500 ); digitalWrite( 0, LOW); delay( 500 ); } }

According to: http://wiringpi.com/reference/software-pwm-library/

Compile it like this:

(add the extra library at the end)

And again, execute as:

PWM Speed and Resolution

Theoretically any resolution is possible, the problem is the speed. The period is calculated like this:

� � ��� ∗ 1�� Therefore is: �

�� ∗�

The higher the resolution, the lower the frequency!

Right image:

resolution = 100

frequency = 100 Hz

$ gcc –o pwm pwm.c –lwiringPi -lpthread

$ ./pwm

Page 5: Controlling the GPIOs of the Raspberry Pi in c pi/GPIO... · Using the GPIO pins of the Raspberr Pi 12.02.2016 ©2016 Simon Burkhardt 1 / 5 Controlling the GPIOs of the Raspberry

Using the GPIO pins of the Raspberr Pi

12.02.2016 ©2016 Simon Burkhardt 5 / 5

Additional Features

The WiringPi library offers many more features like SPI, I2C and Serial Communication.

Look up the reference material on their site to find out how to use it.

http://wiringpi.com/reference/

Screenshot of IDE and shell