Tuesday, 4 December 2012

Rasberry PI Traffic lights

It has been too many years since I done any real electronics (I originally trained as an electronics apprentice for GEC-Marconi Avionics). So, I decided to start on a very simple little project with my new Raspberry PI.

I remembered during my apprenticeship, we were given the task of creating a traffic lights system, but back then we were only allowed to use a breadboard, some LEDs, a few wires, a 555 timer and a few logic gates. No programming required. So using a Raspberry PI for this is overkill to say the least :)

But anyway, I first set-up my raspberry Pi for remote operation (I plan to use it for robotics in the future so don't want to tie it to cables). This was very simple to do. 1st get a battery pack capable of running the Pi off, I'm using a battery from ANKER, which was designed to charge phones and tablets, so it came with a nice micro-USB plug that fits nicely into the PI. It is also capable of providing upto 2A. Although, I found the voltage dropped to 4.4V (below recommended) but I have had no problems with the PI running. Then I installed a USB wifi dongle, I won't go into details here, there are plenty of other blogs/sites/books with details on how to do this.

With the wifi dongle installed, I then only access the raspberry pi via SSH. (I have run vnc server on it, and connected to it that way, but I don't really need it so I don't have it running)

I also got a PI Dish, just to make life easier for playing around with projects like this.

Once that was done, it was then down to adding the traffic light LEDs to the breadboard of the PI dish. Having one set of lights is rather meaningless, so I added 2 sets. I won't go into how to wire an LED to a GPIO pin (it's simple and well plenty of resources around for that). However, here is how I connected the LEDs..

Traffic Lights set A
Red LED to Pin 11
Yellow LED to pin 12
Green LED to pin 13
Traffic Lights set B
Red LED to pin 15
Yellow LED to pin 16
Green LED to pin 18.
And here's the schematic..

So that's the hardware side sorted, with the final result looking like below..

The Raspberry PI Traffic Lights set-up
So next came the programming. I decided to use Python, because 1. it is pre-installed in the Raspian distro I'm using, 2. I was able to install a simple library for accessing the GPIO.
I hadn't really done anything before in python, but it is very simple to use. Below is the code I wrote to create the traffic light sequence:

import RPi.GPIO as GPIO
import time




def setLights_A(red, yellow, green):
    GPIO.output(RED_LIGHT_A, red)
    GPIO.output(YELLOW_LIGHT_A, yellow)
    GPIO.output(GREEN_LIGHT_A, green)

def setLights_B(red, yellow, green):
    GPIO.output(RED_LIGHT_B, red)
    GPIO.output(YELLOW_LIGHT_B, yellow)
    GPIO.output(GREEN_LIGHT_B, green)

while True:

    setLights_A(ON, OFF, OFF)
    setLights_B(OFF, OFF, ON)
    setLights_B(OFF, ON, OFF)
    setLights_B(ON, OFF, OFF)
    setLights_A(ON, ON, OFF)
    setLights_A(OFF, ON, OFF)
    setLights_A(ON, OFF, OFF)
    setLights_B(ON, ON, OFF)

As you can see, the code is very simple.

So the final result of this simple little project..


  1. Geek! ;)

    I suspect my purchase of a Raspberry Pi isn't far off, but haven't really thought through what to do with it yet. (Also need to overcome the 'hardware is a black art' mantra of the true softie)

    Video currently claims to be private. But logo++

  2. Looking good dude. I am seriously impressed just coz you wrote the blog!

  3. @DavidG, Yep I'm a Geek! you should know that by now ;) Video should be public now (1st time I've uploaded to YouTube, forgot to set to public).
    @MountainMan, Hopefully I can keep the blog writing up. Thought I gain alot from other peeps blogs, about time I write one myself :)

  4. Nice! It didn't take long to have it all running.

  5. Video working now.

    Just had the scary thought the Pi probably has more computing power than most of the projects I've worked on, definitely has more memory!

  6. Yay! Yellow light+red just before the green light. I miss that :)
    Now I understand what you meant by "I'm playing with my rasberry pi", I was afraid it's something in the manner of American Pie :)