GPIOZero Tutorial 2 – Programming Traffic Lights

Hello again, and welcome to another Raspberry Pi tutorial! Today, you are going to learn how to wire up and program a set of traffic lights using gpiozero on the pi!

For this tutorial, you will need a breadboard, 3 LEDs in traffic light colours (red, yellow and green), 3 resistors (most likely 330 ohm) , an  active buzzer (some buzzers require resistors, so be sure to check yours – if you are using buzzers from the CamJam edukit you will be fine!) You will lastly need 4 male to male jumper wires and 5 male to female jumper wires. Make sure that your buzzer is an ‘active’ buzzer, as an ‘active’ buzzer only requires current of any type to go through it in order to make a sound. ‘Passive’ buzzers will require an oscillating current which is not provided from the Pi. Here is a circuit diagram for the traffic lights:

traffic lights

Again, like the last tutorial, you can use different pins to the ones I have used, but you must remember to use generic GPIO pins (and not any of the power, ground etc) for the positive ends of the components. The jumper wire connected to the ground leg of each of the components can be connected to any ground pin. I have chosen GPIO pins which make will make the python code compatible with all models of Raspberry Pi, from the A right up to the Pi2 Model B. For the traffic lights I have chosen these GPIO pins (BCM) :

Red LED: 24

Yellow LED: 10:

Green LED: 9

Buzzer: 3

Now that we know what pins our components are going to be using, let’s crack on with some gpiozero code! Like before, we are going to use a ‘while’ loop to run our program forever, however the ‘while True:’ line we will be using in the code can be replaced by a ‘for’ loop, which specifies how many times the traffic lights will be run. I will show you how to do both!

 

Here is the method that will cause the program to loop forever

from gpiozero import LED, Buzzer
import time

red = LED(24) # telling the pi which GPIO pin we have connected each component to
yellow = LED(10)
green = LED(9)
buzzer = Buzzer(3)



while True:

    red.on
    buzzer.on
    time.sleep(3)
    buzzer.off
    yellow.on
    red.off
    time.sleep(0.5)
    yellow.off
    green.on
    time.sleep(10)
    green.off

Here is the method where the traffic lights will run for a set amount of loops:

from gpiozero import LED, Buzzer
import time

red = LED(24) # telling the pi which GPIO pin we have connected each component to
yellow = LED(10)
green = LED(9)
buzzer = Buzzer(3)



for x in range(10) # this program will run 10 times, change the number accordingly
    red.on
    buzzer.on
    time.sleep(3)
    buzzer.off
    yellow.on
    red.off
    time.sleep(0.5)
    yellow.off
    green.on
    time.sleep(10)
    green.off

Thanks for reading guys, tweet me your working traffic lights @RPi_Yaz14 ! See you next time!

-Yasmin

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s