Playing with Raspberry Pi: Traffic Lights, Part 2

In a previous post in this series, we looked at writing Python code to control a set of Low Voltage Labs Traffic Light LEDs for the Raspberry Pi. If you haven’t read that article yet, now would be a good time to do so as this one builds on what we achieved there.

This time, we’ll look at using an environment variable to set whether the lights should follow the UK or USA pattern (UK: red, red & amber, green, amber, red, USA: red, green, amber, red). Start by getting the latest code from GitHub:

git clone
cd pitrafficlights

Set an environment variable “TRAFFIC_LIGHT_COUNTRY” to UK, and start the lights:


The lights should cycle through the UK pattern where red is followed by red and amber before switching to green…

Next, change over to the USA pattern:


The lights should now cycle through the USA pattern where a red light is followed by green…

Try changing to an unsupported setting:


The lights won’t come on and instead you’ll see this on the console:

TRAFFIC_LIGHT_COUNTRY should be set to UK or USA

How It Works

The basic structure of the code remains unchanged from the previous post: set up the GPIO pins, clean up on exit, sit in an infinite while loop that turns the lights on and off in a pattern with sleeps after each step.

We now check to see if there’s an environment variable called TRAFFIC_LIGHT_COUNTRY set, and if so that its’ value is one of UK or USA. If it is, the variable “pattern” is set to “uk” or “usa”. If it isn’t, we error and quit:

if ('TRAFFIC_LIGHT_COUNTRY' in os.environ) and (os.environ['TRAFFIC_LIGHT_COUNTRY'] in ['UK', 'USA']):
pattern = os.environ['TRAFFIC_LIGHT_COUNTRY'].lower()
print('TRAFFIC_LIGHT_COUNTRY should be set to UK or USA')

Now in the main code loop, we add a step for the UK pattern only, where we turn on both the red and amber lights before switching to green:

# Loop forever
while True:
# Red
GPIO.output(9, True)

# Red and amber for UK only
if (pattern == 'uk'):
GPIO.output(10, True)
  # Green
GPIO.output(9, False)
GPIO.output(10, False)
GPIO.output(11, True)

# Amber, longer in US than UK
GPIO.output(11, False)
GPIO.output(10, True)
if (pattern == 'uk'):

# Amber off (red comes on at top of loop)
GPIO.output(10, False)

Also note that after the green light, we show the amber light for a longer time in the US configuration than we do for the UK one.

Next Steps

In this article, we made a simple change to the code to run the lights in different patterns depending on the value of an environment variable. In the next post (now available here), we’ll look at another way we can structure the code by building a finite state machine. Following that we’ll take a look at deploying the code to more than one Pi using containers.

I’d love to hear what you’re up to with the Raspberry Pi — find me on Twitter or via the responses here. If you enjoyed this article, please hit the ❤ and share it far and wide!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.