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
So that's the hardware side sorted, with the final result looking like below..
![]() |
| The Raspberry PI Traffic Lights set-up |
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
RED_LIGHT_A = 11
YELLOW_LIGHT_A = 12
GREEN_LIGHT_A = 13
RED_LIGHT_B = 15
YELLOW_LIGHT_B = 16
GREEN_LIGHT_B = 18
ON = GPIO.HIGH
OFF = GPIO.LOW
GPIO.setmode(GPIO.BOARD)
GPIO.setup(RED_LIGHT_A, GPIO.OUT)
GPIO.setup(YELLOW_LIGHT_A, GPIO.OUT)
GPIO.setup(GREEN_LIGHT_A, GPIO.OUT)
GPIO.setup(RED_LIGHT_B, GPIO.OUT)
GPIO.setup(YELLOW_LIGHT_B, GPIO.OUT)
GPIO.setup(GREEN_LIGHT_B, GPIO.OUT)
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)
time.sleep(3)
setLights_B(OFF, ON, OFF)
time.sleep(1)
setLights_B(ON, OFF, OFF)
time.sleep(1)
setLights_A(ON, ON, OFF)
time.sleep(1)
setLights_A(OFF,OFF,ON)
time.sleep(3)
setLights_A(OFF, ON, OFF)
time.sleep(1)
setLights_A(ON, OFF, OFF)
time.sleep(1)
setLights_B(ON, ON, OFF)
time.sleep(1)
So the final result of this simple little project..

