In this post I will talk a little about GPIOs.
There are a lot of libraries that make using of GPIOs really easy.
I decided to use the PIGPIO library, available here
The PIGPIO has a lot of features
- sampling and time-stamping of gpios 0-31 between 100,000 and 1,000,000 times per second
- provision of PWM on any number of the user gpios simultaneously: this is a great features because you don't need to use an hardware PWM pin, making the hardware design much effective. For this project in particular, the enOcean board connector covers most of the pins
- provision of servo pulses on any number of the user gpios simultaneously.
- callbacks when any of gpios 0-31 change state (callbacks receive the time of the event accurate to a few microseconds).
- notifications via pipe when any of gpios 0-31 change state.
- callbacks at timed intervals.
- reading/writing all of the gpios in a bank (0-31, 32-53) as a single operation.
- individually setting gpio modes, reading and writing.
- socket and pipe interfaces for the bulk of the functionality in addition to the underlying C library calls. For this project, I will use the C interface
- the construction of arbitrary waveforms to give precise timing of output gpio level changes (accurate to a few microseconds).
Installation
Installing PIGPIO is very easy
- 1. make a directory where the zipped library will be downloaded
cd /home/pi
mkdir pigpio
- 2. download the zipped library
wget http://abyz.co.uk/rpi/pigpio/pigpio.zip
- 3. unzip the library
unzip pigpio.zip
- 4. change to the source directory
cd PIGPIO
- 5. make the files
make
make install
Use of the library
Using the library is as easy as installing.
All GPIOs are identified by their Broadcom number. Details about the pinout of the Raspberry Pi B+ board can be found here
http://www.raspberrypi-spy.co.uk/2014/07/raspberry-pi-b-gpio-header-details-and-pinout/
The Broadcom number is the number written in the cyan label. For example if you connect a LED to the pin 40 of the connector, you can see in the diagram below that the cyan label says "GPIO21". So you should invoke PIGPIO function passing 21 for the "gpio" parameter.
The basic steps to build a C application are
- 1. invoke gpioInitialise to initialize library
- 2. to drive a digital output (for example a LED), use the gpioWrite(unsigned gpio, unsigned level). You don't need to explicitly invoke the gpioSetMode function because the gpioWrite automatically sets the proper pin mode
- 3. to drive a servo, use the gpioServo(unsigned gpio, unsigned pulseWidth). The pulse width can be 0 (to switch PWM off) or any value from 500 (servo rotate in the most anti-clockwise position) to 2500 (servo in the most clockwise position)
- 4. invoke gpioTerminate to finalize the library and release all the resources
Hardware connections
Sample application
Attached to this post is a sample application that drives two servos and 4 LED. Application is self-explicatory: using 'a' and 'z' keys you can drive first servo and with 's' and 'z' keys