[Logo for The C Shore Landing Page]
The C Shore

Making Traffic lights

Table of Contents

Making traffic lights

For this worksheet you'll need a breadboard, three LEDs, a button, and the necessary jumper cables and resistors.

Wiring

To get started, you'll need to place all the components on the breadboard and connect them to the appropriate GPIO pins on the Raspberry Pi.

  • First, you need to understand how each component is connected:

    • A push button requires 1 ground pin and 1 GPIO pin
    • An LED requires 1 ground pin and 1 GPIO pin, with a current limiting resistor

    Each component requires its own individual GPIO pin, but components can share a ground pin. We will use the breadboard to enable this.

  • Place the components on the breadboard and connect them to the Raspberry Pi GPIO pins, according to the following diagram:

GPIO diagram

Note that the row along the long side of the breadboard is
connected to a ground pin on the Raspberry Pi, so all the
components in that row (which is used as a ground rail) are hence
connected to ground.
  • Observe the following table, showing which GPIO pin each component is connected to:
Component GPIO pin
Button 21
Red LED 25
Amber LED 8
Green LED 7

Dive into Python

  • Create a new file by clicking New.

  • Save the new file straight away by clicking Save; name the file trafficlights.py.

Traffic lights

You have three LEDs: red, amber, and green. Perfect for traffic lights! There's even a built-in interface for traffic lights in GPIO Zero.

  • Amend the from gpiozero import... line to replace LED with TrafficLights:

    from gpiozero import Button, TrafficLights
    
  • Replace your led = LED(25) line with the following:

    lights = TrafficLights(25, 8, 7)
    

    The TrafficLights interface takes three GPIO pin numbers, one for each pin: red, amber, and green (in that order).

  • Now amend your while loop to control the TrafficLights object:

    while True:
        button.wait_for_press()
        lights.on()
        button.wait_for_release()
        lights.off()
    

    The TrafficLights interface is very similar to that of an individual LED: you can use on, off, and blink, all of which control all three lights at once.

  • Try the blink example:

    while True:
        lights.blink()
        button.wait_for_press()
        lights.off()
        button.wait_for_release()
    

## Extra: Add a buzzer

A buzzer requires 1 ground pin and 1 GPIO pin

The table above will change to:

Component GPIO pin
Button 21
Red LED 25
Amber LED 8
Green LED 7
Buzzer 15

Now you'll add your buzzer to make some noise.

  • Add TonalBuzzer to the from gpiozero import... line:

    from gpiozero import Button, TrafficLights, TonalBuzzer
    from gpiozero.tones import Tone
    
  • Add a line below your creation of button and lights to add a TonalBuzzer object:

    buzzer = TonalBuzzer(15)
    
  • Try adding a buzzer.play() and buzzer.stop() into your loop:

    while True:
        lights.on()
        buzzer.stop()
        button.wait_for_press()
        lights.off()
        buzzer.play(Tone(200.0))
        button.wait_for_release()