PiLights

PiLights

So i was a little restless one evening  and decided to play with some LEDs (i had them sitting around along with the resistors for quite a while but have been preoccupied with the control of my PiBot) but of course just having some LEDs turn on or off when i say is a little boring so i made it a bit more interesting.

 

The Hardware

Connect the wires as shown in the diagram above, connecting the positive legs of the LEDs as follows;

  • Green to 24
  • Yellow to 25
  • Red to 23

ensure you have resistors connected in series with the LEDs to protect them from being overloaded. The pins on the raspberry pi output 3.3V when set to true and therefore some simple calculations can be used based on the LEDs you have for the resistors you need. Simply google it as there are calculators available online as well as good descriptions of how to do it.

(i may add a short explanation here when i have time)


The Code

 

#import the GPIO library, the time function and set the GPIO numbering to BCM which is the raspberry pi standard
import RPi.GPIO as io
import time
io.setmode(io.BCM)
 
#set the relevant pins (you dont have to use these ones but make sure you adjust the code if you use different ones)
green_pin = 24
yellow_pin = 25
red_pin = 23
 
#set the pins all to outputs
io.setup(green_pin, io.OUT)
io.setup(yellow_pin, io.OUT)
io.setup(red_pin, io.OUT)
 
#this passage sets the sequence for the Traffic Lights (i chose the pellican style flashing ones to add a little more complexity)
while True:
        #set the green light on
        io.output(green_pin, True)
        #use the time.sleep function to make the program wait 3 seconds before continuing
        time.sleep(3)
        io.output(green_pin, False)
        io.output(yellow_pin, True)
        time.sleep(1)
        io.output(yellow_pin, False)
        io.output(red_pin, True)
        time.sleep(3)
        io.output(red_pin,False)
        io.output(yellow_pin, True)
        time.sleep(0.2)
        io.output(yellow_pin, False)
        time.sleep(0.2)
        io.output(yellow_pin, True)
        time.sleep(0.2)
        io.output(yellow_pin, False)
        time.sleep(0.2)
        io.output(yellow_pin, True)
        time.sleep(0.2)
        io.output(yellow_pin, False)
 
Enjoy!