Raspberry Pi GPIO Hacking

I’ve been playing with my Raspberry Pi starter kit today. It comes with a clear plastic case for mounting the Pi onto, but as I’ve already got a case I’m just using the breadboard and the components that were supplied with the kit. As well as the breadboard the kit includes the following components:

  • 12 × LEDs (4 each of Red, Yellow and Green)
  • 12 × 330Ω resistors (for the LEDs)
  • 2 × 10kΩ resistors
  • 2 × mini push buttons
  • 10 × male to female jumper wires

The example I was following is for a simple traffic lights system. The hardware components are wired into the Raspberry Pi board using its GPIO pins. These General Purpose Input Output pins can be controlled using software so they provide a simple way to connect to external hardware to the Pi.

In addition to the familiar USB, Ethernet and HDMI ports, the R-Pi offers lower-level interfaces intended to connect more directly with chips and subsystem modules. RPi Low-level peripherals introduction

Once I got the traffic lights wired up and working I started hacking on the software side of things. The example python code didn’t cleanup the GPIO when I pressed ctrl-c, so the lights remained in the position they were in when I interrupted the process. To fix this I installed a signal handler to run the GPIO cleanup code and exit cleanly.

def cleanup_gpio(signal, frame):
    print
    GPIO.cleanup()
    sys.exit(0)

# Install signal handler to cleanup GPIO when user sends SIGINT
signal.signal(signal.SIGINT, cleanup_gpio)

Raspberry Pi plugged into breadboard

The code I’ve been tinkering with is on GitHub. This includes the example C code, which is pretty much untouched other than adding a limit to the number of loops so that the GPIO cleanup code gets run. There’s also the example python code which is what I’ve mainly been playing with.

There is a disco.py file in the repository that flashes the lights in sequence which is essentially a modified version of the traffic lights script.

For my first attempt at Raspberry Pi hacking this was very successful, it’s a real buzz to see hardware and software coming together in something of your own making.

The next stage of this project will involve linking this traffic light system into a web service to create a status indicator.