Raspberry Pi GPIO control
We have been working with the new Raspberry Pi board for a while but didn’t show it to you guys before now.
Many of you might already have seen and read plenty of videos and articles about it so I thought it would be more appropriate to make a tutorial on how to use the GPIO’s, and especially on how to speed up the GPIO’s.
In this video I walk you thru all the steps from installing the Raspbian image which is based upon Debian. This is by far the most complete and well working image I’ve discovered.
Together with a complete X-window system it also comes with many different developer tools preinstalled such as Python and GCC.
So go watch the video while to set up your own Raspberry Pi for GPIO control.
For you who find it difficult to see the lines I’m writing in the terminal in each step here is a short write-up of the commands.
First we start by downloading and extracting our code collection package by executing the following commands:
tar xvf Raspberry_Pi_GPIO.tar
All the scripts in collection use pin 1 (BCM pin 18) for the toggling which can be found on pin 12 of the GPIO connector on the Raspberry Pi.
The BCM pins table can be found here: http://elinux.org/RPi_BCM2835_GPIOs while the wiringPi pins table, which is a bit different, can be found here: https://projects.drogon.net/raspberry-pi/wiringpi/pins/
First test in the video is the raw C code. To compile and run this code the following commands has to be executed:
Next step is to install and test the BCM2835 library. To download and install it we run the following commands:
tar zxvf bcm2835-1.8.tar.gz
sudo make check
sudo make install
Now you should be able to compile and execute the test code from our code collection:
gcc IOToggle_Clib_bcm2835.c -o IOToggle_Clib_bcm2835 -I/usr/local/include -L/usr/local/lib -lbcm2835
The final step in the C world is to test the wiringPi library which is the one we recommend using. First you need to download, compile and install the library.
As the most recent version of the library is found on GitHub we first have to install the “git-core” on our Raspberry Pi to be able to clone the repository.
Now we should be ready to fetch the library, compile it and finally install it in the C libraries directory of the Raspberry Pi.
sudo make install
Finally we can go back to our main folder with the extracted code collection and compile the wiringPi test code.
gcc IOToggle_Clib_wiringPi.c -o IOToggle_Clib_wiringPi -I/usr/local/include -L/usr/local/lib -lwiringPi
To control the GPIO’s with Python we also have to install a library. In this case we will use the RPi.GPIO library.
OBS: We have been told that this RPi.GPIO Python library now comes pre-distributed with the image, so all you have to do is to install it.
You can check whether or not your current image already contains the RPi.GPIO package by executing the following command:
ledmon - Enclosure LED Utilities
python-rpi.gpio - Python GPIO module for Raspberry Pi
python3-rpi.gpio - Python 3 GPIO module for Raspberry Pi
If not, you should install the library manually by following the tutorial on the “RPi.GPIO library” page. But before installing this library you might also want to install the “python-dev” package on our Raspberry Pi as it is used to setup the library properly.
After a while this should be installed and you will be able to execute the Python test code.
If you are in the X-window environment you will also be able to start the GPIO GUI test by executing the following command in the terminal application:
The last thing demonstrated in the video is how to control the GPIO’s using Bash terminal commands. This is done using another part of the wiringPi library so we will go back to the downloaded Git repository and install this extra part.
sudo make install
Now you should be able to use the simple “gpio” commands to control the GPIO’s. The complete description of the commands can be found here: The GPIO Utility.
To control a pin as an output you should use the following commands:
gpio write 1 1 # set pin 1 to high
gpio write 1 0 # set pin 1 to low