Ryanteck Traffic HAT - GPIO Zero

Following on from the previous tutorial of blinking an LED using GPIO Zero we're going to expand into creating a traffic light program.

However instead of using the separate modules for the LED And buzzer there is a neater way that we can do it using the TrafficHAT Add-on function.

Begin by adding the following code to setup the TrafficHAT replacing Ryan Walmsley with your name.

#TrafficHAT Program

#TrafficLight By Ryan Walmsley



from gpiozero import TrafficHat

from time import sleep



Next save it as traffichat-trafficlight.py to your home directory (/home/pi as standard). Keep saving along the way.

The main difference here is that instead of importing an LED, Button or Buzzer we are instead importing TrafficHat.

This means that the Red, Green and Amber LEDs are already pre-assigned so instead of having to do the following for the amber LED:

amber = LED(23)



We can just do the following:

#Amber on


Or to blink it multiple times in a row like the previous tutorial:

#Amber blink


As of this building the rest of the program is very simple.

We want to begin by outlining the basis of the program. Add the following to the bottom of your current program.


	while True:

		#TrafficLight Code



except KeyboardInterrupt:


This basis of code will forever run the program until Ctrl-c is pressed. Once Ctrl-c is pressed it will close the program correctly making all the LEDs and buzzer turn off.

Next all of the following code will go underneath the #TrafficLight Code line instead of at the bottom and should be indented to the same level

Now we want to do the following for our program:

  1. Turn the Green LED On
  2. Wait until the button is pressed
  3. Turn the Green LED Off
  4. Turn the Amber LED On
  5. Wait 2 seconds
  6. Turn the Amber LED Off
  7. Turn the Red LED On
  8. Buzz or "Blink" the buzzer 15 times with a 0.1 second delay.
  9. Sleep for 1 second
  10. Turn the Red LED Off
  11. Blink the Amber LED 6 times with a 0.5 Second Delay.

Let's get started!

To turn the green LED On add the following line to your code


Using the GPIO Zero library has made this really easy to do. We just start with the object for the TrafficHAT which is th , address the green' LED and then use the green LED's on function.

Next we want to wait until the button is pressed. We use a neat little bit of code to do this.

while(th.button.is_pressed == False):

			#While not pressed do nothing


		#Button has been pressed!

Let's go through what this bit of code does: First it checks that while the button is not pressed is correct. We do this by checking to see if the is_pressed variable of the button is False. When the button is not pressed it will be False hence the statement being True. When the button is pressed the TrafficHAT Will return True and making the statement false.

While the statement is true the python program runs the pass command which just does nothing. However if we don't put pass there then the program will not work.

Once the button is pressed the while loop is exited and the program will continue.

Next we want to turn the Green LED Off. We do this very much the same as we turned it on however instead of using on we use off'


And then turn the amber LED On. You can do this by modifying the code for the Green LED.

Next we then want to make the program wait or sleep for 2 seconds. We do this using the following code:


You can also change the 2 for any number of seconds for the delay.

Now the amber LED has been on for 2 seconds we want to then turn it off and then turn on the red LED.

Now the Red LED is turned on we then want to buzz the buzzer. To do this we use the blink function.

Here's the line of code:


As you can see we've set some variables for the blink function. They are: The on time: The amount of time the buzzer or LED is on for. In this case 0.1 Seconds The off time: The amount of time the buzzer or LED is then off for. In this case also 0.1 Seconds. N - Iterations: The amount of times the buzzer or LED blinks or buzzes. In this case it will loop 15 times. Threaded / Background: The final variable is weather it blinks or buzzes in the background. Say that you wanted it to buzz while your program is still doing something you would set this to True. However we want our program to pause while this happens so we set it to False.

Next we then want to sleep again for 1 second and then turn the red led off.

Finally we then want to repeat the same blink function for the buzzer but instead of using the buzzer we want to change it to amber.

And your program should now be ready to go!

Save it and run the program by opening a terminal window and typing "python3 traffichat-trafficlight.py".

If your program isn't working then you can compare it to our demo code located here:

#TrafficHAT Program

#TrafficLight by Ryan Walmsley



from gpiozero import TrafficHat

from time import sleep

th = TrafficHat()



	while True:

		#TrafficLight Code

		#First turn the Green LED On


		#Next we want to wait until the button is pressed.

		while(th.button.is_pressed == False):

			#While not pressed do nothing


		#Button has been pressed!


		#Amber on for a couple of seconds




		#Turn the red on


		#Buzz the buzzer 20 times with 0.1 second intervals




		#Red off and blink amber 4 times with 0.5 second intervals







except KeyboardInterrupt: