Tuesday, November 27, 2012

Raspberry PI, GPIO and blinking LEDs

My Raspberry PI is the revision B model with 512 MB of RAM. Currently, I am running Raspbian Wheezy 2012-09-18 version, which I keep up-to-date using

$ sudo apt-get update

$ sudo apt-get upgrade

This process, which I execute on about a two week basis,  usually takes at least half an hour to complete, so go have a coffee, rake the lawn, do the dishes, then come back.

Now for our blinking LEDS:

There are several ways to accomplish the feat of blinking a LED on the PI. The reason I chose the approach below is because it doesn’t need to run as ‘root’, which is a good thing, especially when you are running it in conjuction with a we server, which is what I will be doing.

Two software packages actually are needed to get things to work (that is, if you are programming in Python and not C). First, you need the C library and then you need the Python library that talks to the C library, something that is called a ‘wrapper’ in software parlance. The Python library conveniently insulates us from all that nasty low level C language, you know, the stuff adored by real geeks.

As to how to do all this, I read this webpage, which is a pretty good start:


The thing to do there is NOT to overlook the phrase ‘First follow the instructions found here to install WiringPi’, which is exactly what I did, and then caused a whole heap of unnecesssary trouble. You see, that caused me to skip the C portion. So install the C portion of WiringPi first, using Gordon’s excellent instructions.

Next, follow Sebastian’s set of instructions for the Python wrapper.

If everything installs properly, you should be able to run this application in Python:

import wiringpi
from time import sleep
import os
os.system("gpio export 18 out")
os.system("gpio export 24 out")
while True:
    print wiringpi.digitalRead(18)
    print "18 on"
    print wiringpi.digitalRead(24)
    print "24 off"
    print wiringpi.digitalRead(18)
    print "18 off"
    print wiringpi.digitalRead(24)
    print "24 on"
With the proper setup, this will cause 2 LEDs to blink. What is the proper setup you ask? It is this:

With the Raspberry Pi’s micro USB power connector situated on the top left and the General Purpose Input Output (GPIO) pins on the right hand side of the board, go to pin 6 (physical pin 6). To get to pin 6, start at the upper right hand side of the board and count down 6. That pin is pin 18 in the above setup. Trust me. If you want to know more, go here. For pin 24, the pin to go to is pin 9. Now get yourself some wire, 2 LEDs and 2 220 or 270 Ohm resistors, doesn’t really matter too much. Put an LED in series with a resistor, hook one end up to pin 6 and the other to pin 3 (ground). Repeat for the next LED/resistor pair: pin 9 and the other end to pin 3. Now run the above program. It helps to have a bread board and some connecting wires: an old hard drive ribbon cable is great.

My contribution to the overall progress of technology in this case is embedding the lines os.system(“gpio export 18 out”) (the same for 24). These 2 lines make pins 18 and 24 available to the current user, e.g. pi. In theory, if you script has a proper ending it should call the line os.system(“gpio unexport 18”) (the same for 24), in order to restore the system to the way it was, so another user can use those pins.

No comments: