Use the Traffic HAT with GPIO Zero

By Russell Barnes. Posted

As one of the boards that GPIO Zero supports, the Traffic HAT is a perfect way to show off the library...

The Traffic HAT is a great little kit: a GPIO-mounted add‑on for the Raspberry Pi that makes learning how to program for physical computing a little easier and bit more fun, along with nice big components that can also help you learn the basics of soldering.

The full article can be found in The MagPi 40

With the release of GPIO Zero, the Traffic HAT is now easier than ever to program, whether you want to use it manually or make use of the handy Traffic HAT function built into GPIO Zero. Follow along with us as we teach you how to make the most of both the HAT and GPIO Zero.

You'll need

Traffic HAT

GPIO Zero

Soldering iron (optional)

STEP-01 Prepare the Traffic HAT

You can buy the Traffic HAT pre-soldered so that you don’t have to worry about it, but it’s a great beginner kit for learning how to solder. You only need to install the main features you’ll be using: the three LEDs, the push-button switch, and the buzzer.

Make sure to follow the outline guides on the board as to how to place them. The buzzer’s positive side is indicated on the component, and the board also has a guide showing which side it should be attached to. The LEDs should be soldered so the flat edge of the bulb meets the flat edge of the outline.

STEP-02 Install the Traffic HAT

Make sure your Raspberry Pi is turned off. Slot the HAT over the GPIO pins, with the board itself lying across the Raspberry Pi. This way, it shouldn’t be poking over the edge like a plank and will look like a normal addition to the Pi.

Once that’s done, you can turn on your Raspberry Pi. Once booted up into Raspbian, you’ll need to install GPIO Zero. This can be done by using the following commands in the terminal:

$ sudo apt-get install python-pip python-w1thermsensor python-spidev
$ sudo pip install gpiozero

STEP-03 Basic GPIO Zero

The LEDs have a GPIO number attached, as do the button and buzzer. We can use that in conjunction with GPIO Zero to activate the LEDs manually in the GPIO Zero style. Open up a new Python script in IDLE and start it with the following code, so it knows what functions to use:

from gpiozero import LED

Then write in the following so the red LED, connected to GPIO 24, lights up:

led = LED(24)
led.on

Press F5 to run it and see the results. Buzzer and Button are the other functions that you can import for the Traffic HAT.

STEP-04 Dedicated GPIO Zero

A slightly easier way of programming the Traffic HAT with GPIO Zero is by using the actual Traffic HAT function built into Zero. You can make use of it by changing the first import line to:

from gpiozero import TrafficHat

This makes use of buzzer, button, and lights functions to manage those respective parts of the HAT. The lights are then described as green, amber, and red in the code, for when you want to activate them. We’ll now create a little script to perform a traffic light sequence in Python.

STEP-05 Setting up

Looking at the code on this page, we have a simple setup by importing TrafficHAT and time. We’ll need the latter to simulate the kind of delay you normally get on real traffic lights. Set the Traffic HAT code to be known as the variable th and we’re ready to begin our loop.

Create a simple while loop that will continuously run. Start it by having the green light show, and use another while loop to stop the code until the button is pressed. It will stay green forever unless you press the button or interrupt the program.

STEP-06 Light timing

The bit of code after the button is pressed may seem a little complicated at first glance, but under closer inspection it should be fairly straightforward. It emulates the way traffic lights work at a pelican crossing, activating the amber light for a few seconds before giving a steady red light.

When the red light appears, the buzzer will beep for a few seconds before the red light is turned off and the amber appears, this time flashing itself, before reverting back to green. At this point, the while loop starts again, waiting for a button prompt.

Code listing

TrafficLights.py

from gpiozero import TrafficHat
from time import sleep

th = TrafficHat()
 
try:
    while True:
        # Traffic light code
        # First, turn the green LED on
        th.lights.green.on()
        print "Press the button to stop the lights!"

        # Next, we want to wait until the button is pressed
        while(th.button.is_pressed == False):
            #While not pressed do nothing
            pass
        
        # Button has been pressed!
        th.lights.green.off()
        # Amber on for a couple of seconds
        th.lights.amber.on()
        sleep(2)
        th.lights.amber.off()
        # Turn the red on
        th.lights.red.on()
        # Buzz the buzzer 20 times with 0.1 second intervals
        th.buzzer.blink(0.1,0.1,20,False)
        sleep(1)
        th.lights.red.off()
        # Red off and blink amber 4 times with 0.5 second intervals
        th.lights.amber.blink(0.5,0.5,4,False)
 
except KeyboardInterrupt:
    exit()

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.