Home > Development boards, Embedded Linux, Guides, Raspberry Pi > Raspberry Pi GPIO control

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:

cd ~
wget http://www.tkjelectronics.dk/uploads/Raspberry_Pi_GPIO.tar
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:

gcc IOToggle_Craw.c -o IOToggle_Craw
sudo ./IOToggle_Craw

Next step is to install and test the BCM2835 library. To download and install it we run the following commands:

wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.8.tar.gz
tar zxvf bcm2835-1.8.tar.gz
cd bcm2835-1.8
./configure make
sudo make check
sudo make install

Now you should be able to compile and execute the test code from our code collection:

cd ~
gcc IOToggle_Clib_bcm2835.c -o IOToggle_Clib_bcm2835 -I/usr/local/include -L/usr/local/lib -lbcm2835
sudo ./IOToggle_Clib_bcm2835

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.

sudo apt-get install git-core

Now we should be ready to fetch the library, compile it and finally install it in the C libraries directory of the Raspberry Pi.

git clone git://git.drogon.net/wiringPi
cd wiringPi/wiringPi
sudo make install

Finally we can go back to our main folder with the extracted code collection and compile the wiringPi test code.

cd ~
gcc IOToggle_Clib_wiringPi.c -o IOToggle_Clib_wiringPi -I/usr/local/include -L/usr/local/lib -lwiringPi
sudo ./IOToggle_Clib_wiringPi

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.

sudo apt-get install python-rpi.gpio

You can check whether or not your current image already contains the RPi.GPIO package by executing the following command:

apt-cache search gpio
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.

sudo apt-get install python-dev

After a while this should be installed and you will be able to execute the Python test code.

sudo python IOToggle_Python.py

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:

sudo python rpi-gui-gpio.py

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.

cd ~/wiringPi/gpio
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 mode 1 out    # set pin 1 as output
gpio write 1 1     # set pin 1 to high
gpio write 1 0     # set pin 1 to low
  1. skif234
    September 30th, 2012 at 01:52 | #1

    1. not
    bmc2835 <—-- (many times)

    2. not
    cd ~/wiringPi/gpio
    cd ~/wiringPi/wiringPi/gpio

  2. skif234
    September 30th, 2012 at 01:55 | #2

    3. not
    tar -xzvf Raspberry_Pi_GPIO.tar
    tar -xvf Raspberry_Pi_GPIO.tar

    PS: In any case thank you for this perfect tutorial :)

  3. September 30th, 2012 at 09:58 | #3

    Thank you for noticing.

    Though it should not be “cd ~/wiringPi/wiringPi/gpio” -- please check the github repository.
    The GPIO function is in a seperate folder than the wiringPi library itself.

  4. Arjan
    October 3rd, 2012 at 18:22 | #4

    You write “the wiringPi library which the recommend using.”
    Please will you elaborate on this recommendation? Thanks.

  5. October 4th, 2012 at 10:33 | #5

    I recommend using the wiringPi library as it seems like the most featured and best performance library from the one I have been testing.
    It is very easy to install and makes the C programming (the functions) very similar to the Arduino.

    Therefor I think it is a good starter library for the Raspberry Pi when you would start messing around with its’ GPIO’s.

  6. daveca
    November 1st, 2012 at 20:01 | #6

    The video shows using a wired mouse and KBDon the Pi.
    Try using a wireless KBd and mouse and watch the Pi crash.

    What is the point in having full PC capabilities in the Pi, then doing the GPIO programming through an Ethernet link from a PC?

    That should only apply to psogramming a lower level microcontroller via a PC where the uC cannot host a high level interface.

    Beware, anyone attempting to do real development work on the Pi, its circuits and board layout are faulty and thats the reason the USB ports do not operate properly.

  7. November 4th, 2012 at 13:09 | #7

    The reason for using a PC and an Ethernet connection for the programming of the Pi is to avoid having to use an HDMI monitor. This means you could embed your Raspberry Pi wherever you need to and still be able to program it.
    Switching from a smaller uC to the Raspberry Pi might be necessary for larger applications doing home automation with surveillance cameras, data logging or display interfacing.

    It is true that there is some major problems with the USB ports and the Ethernet connection, as this is connected to the USB controller. But this is due to the still not fully developed and functional Broadcom BCM2835 Linux drivers. As it is only a driver issue and not a hardware issue it will be possible to fix it with a software update.
    Just give the community and the developers time to realize a fully functional driver.

    Regards Thomas

  8. Kashif
    December 7th, 2012 at 12:29 | #8

    once again, an awesome tutorial by TKJ Electronics, loved it.

  9. clueless_me
    December 8th, 2012 at 20:51 | #9

    Hi and thanks for the nice video!
    Can you maybe help me? I am new to electronics and fairly new to linux. I want to use the Rpi as a (poor, I know, but I want to learn) multitrack recorder. I have already written the bash scripts to start and stop the recording and do overlays (using arecord, mplayer and sox). Now all I want to do is configure the Rpi so it starts one of these shell scripts upon the push of a button. Since I want the whole thing to run headless I figure I need to write a script that starts upon boot up and tells the Rpi which GIOP pins need to be set to input so it waits for me to press them.
    I have searched the web for a similar scenario, but only python scripts are used. So am I correct that I have to compile the library as you did in the video (the part for the shell) and put a script in the startup folder? How would that script have to look like in principle? How do I tell the Rpi “get these pins ready and if that one pin is pushed, execute sh /path/to/script ?
    I hope I don’t bother you with this (I guess it actually is simple, but I am just not deep into the stuff yet).
    Any help is heartily welcome.

  10. December 23rd, 2012 at 15:26 | #10

    Well the main aspects of what you are trying to do is simply starting a Python script at boot which will take care of the I/O initialization and then start listening on the buttons, and if any is pressed, execute the proper shell command.

    1. First you will have to make sure that you have installed all the required packages, such as Python and the GPIO library as described in the tutorial.

    2. Next you will have to make the boot-up Python script that needs to be placed within /etc/init.d/.
    The file should contain the following lines:

    #!/usr/bin/env python
    from time import sleep
    import os
    import RPi.GPIO as GPIO
    GPIO.setup(23, GPIO.IN)
    GPIO.setup(24, GPIO.IN)
    GPIO.setup(25, GPIO.IN)
    while True:
            if ( GPIO.input(23) == False ):
                    os.system('<command when button 23 is pressed> &')
            if ( GPIO.input(24) == False ):
                    os.system('<command when button 24 is pressed> &')
            if ( GPIO.input(25)== False ):
                    os.system('<command when button 24 is pressed> &')

    Remember to save it within /etc/init.d/

    3. Next you should make it executable

    chmod +x <filename.py>

    4. And Finally you should add it to the boot process by executing the following command:

    update-rc.d <filename.py> defaults

    Now when you reboot your Pi this Python script should run in the background!

    Merry Christmas.

    Regards Thomas

  11. clueless_me
    January 13th, 2013 at 17:26 | #11

    @Thomas Jespersen

    Cheers Thomas,
    thank you so much for the reply. I have not have had the time yet to actually implement it, but I shall let you know how it went. Now at least I have a good idea of what to do next.

    So thanks again

  12. Uli Egg
    July 23rd, 2013 at 17:31 | #12

    Hi Thomas,
    I really enjoyed your phantastic tutorial for GPIO!
    I followed it step by step.
    But the last step I got an error:
    After entering directory ~/GPIO/wiringPi/gpio
    and ordering make
    I got a fatal error:
    piFace.h: File not found
    What’s wrong? Everything else was doing well!

  13. July 24th, 2013 at 11:53 | #13

    @Uli Egg
    Hi Uli.
    Hmm, I don’t know why this happens but it might be due to some changes in the wiringPi library.
    I have some suggestions for things you can try:

    Please make sure that you have already installed the wiringPi library as shown in the guide

    cd wiringPi/wiringPi
    sudo make install

    1. You can try to install the devLib by executing the following commands:

    cd wiringPi/devLib
    sudo make install

    And then try to make the gpio library again.

    2. If that doesn’t work you can also try to copy the piFace.h file that can be found in the devLib folder.
    And then try to make the gpio library again.

    I hope this works out for you.

    Thomas Jespersen

  1. March 28th, 2013 at 09:06 | #1
  2. June 4th, 2013 at 21:22 | #2