Traffic Lights

Teachers - want real traffic lights in your classroom?

Real Traffic Lights!

GPIO Support Services is pleased to announce that we have been given a set of real traffic lights that we can demonstrate in schools and colleges. This includes a complete traffic signal (Red, Amber  & Green). With this children can write the code for the Traffic Light Project and see it working in action on  full size hardware. If you would like us to come along to your school with these (no cost to the school & we are DBS checked) please get in touch.

A word of warning – real traffic lights are not small! – you will need a room with lots of space.

Welcome to the starter project for the gPiO box, as this is the first project we’ve included lots of detail.

There are a number of expanding text boxes like the one below providing you with additional information such as curriculum links,  videos that take you through building the model step by step and  code examples for Scratch & Python.

Click box on right for Curriculum Links

KS2 Computing POS:

• design and write programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts

• use sequence, selection, and repetition in programs; work with variables and various forms of input and output; generate appropriate inputs and predicted outputs to test programs


• pupils should work in a range of relevant contexts [for example, the home, school, leisure, culture, enterprise, industry and the wider environment].

• understand and use electrical systems in their products [for example, series circuits incorporating switches, bulbs, buzzers and motors]

• apply their understanding of computing to program, monitor and control their products.


This is the starter project for the gPiO box.




To program a set of traffic lights.

Learning objectives

You will be able to:

  • set up and use a Raspberry Pi and gPiO interface.
  • connect electrical components (LEDs) to the output terminals of the gPiO interface.
  • program Scratch to switch outputs on and off in a prescribed sequence.
  • use loops in your script to repeat actions many times.

Design Brief

Using three LEDs, create a traffic light model and write a program to control a set of traffic lights in the following sequence:

red > red + amber > green > amber > and so on.

You will need a processing device (computer), an interface to connect the components to the computer and a power supply.

Hot tip: Traffic lights use this sequence


red > red + amber > green > amber > and so on.

Making the model

You can also watch the video


Connecting to the gPiO Box

The traffic light model consists of three LEDs. Connect the:

· Red LED to output 1

· Amber LED to output 2

· Green LED to output 3

If you are unsure how to do this watch the Making Connections section of the In The Classroom video.

Scripts & Code

In addition to the build instructions for projects on this site, we have also provided examples of the scripts (Scratch) and code (Python) for many of these projects. The examples are shown below as images and are also available for download in the Code Library.

What is the Code Library?

The Code Library is a collection of examples of code – and other resources – used to support the projects on this site. In additional to code and script examples it contains media files, such as pedestrian crossing beeps and other useful files. The Code Library is part of the Resources section of the site.

If your Raspberry Pi has an internet connection you can download the files directly, alternatively use a memory stick to copy the files to your Pi.

When using Scratch, pin connection numbers are dependent on whether you are using ScratchGPIO or GPIOServer. See the box below for more information and use this document to determine the correct pins to use.

Why two versions of Scratch?

Some History…

Until fairly recently, the ‘official’ version of Scratch from the Raspberry Pi Foundation could not control the GPIO pins of the Raspberry Pi and so could not drive external physical devices.

ScratchGPIO, written by Simon Walters, is a modified version of Scratch that does enable control of the GPIO pins and has been the de facto (and only) standard for controlling the GPIO pins over the last few years.

There is now a new ‘official’ version of Scratch, called GPIO server, that can control the GPIO pins and it is likely that this new version will be adopted by teachers new to the Raspberry Pi.

However, there are significant differences between the old and new versions and many teachers have chosen to stick with ScratchGPIO.

The gPiO box will happily work with either version of Scratch or with any other language used on the Raspberry Pi.

In the Code Library we have provided both versions of the Scratch scripts, prefixed with SG for ScratchGPIO and GS for GPIO Server.

P.S. If this supposed explanation has left you more, rather than less, confused get in touch and we’ll go through it with you.


If you use Flowol(TM) in your school you can download this flowchart from the Code Library. Version 4 and above of Flowol can be used with the gPiO Box, so that you can use this flowchart to control physical devices directly. For more information on using Flowol with the gPiO please contact us.

Traffic Light Flow Chart Animation


This is the ScratchGPIO version of this project, you can download this script from the Code Library.

Scratch gPiO traffic light ws


This is the GPIOServer version of this project, you can download this script from the Code Library.



The Python Code below uses gpiozero, a new Python Library for the Raspberry Pi. The code is part of a worked set of Python examples, each one building on the last, in which students are asked to add simple lines of code.

The file Traffic, in the Code Library. contains all of these examples.

# The traffic lights are connected to different pins on the Raspberry Pi
# We could send commands using the number of the pin each time; however
# it is easier if we assign a 'friendly' name to make it easier to send
# commands to the correct light.
r_light = LED(17) # Red LED - Connected to GPIO Box Pin 1
y_light = LED(18) # Yellow LED - Connected to GPIO Box Pin 2
g_light = LED(27) # Green LED - Connected to GPIO Box Pin 3

while True:

    # We turn on a light by taking the 'friendly' name of the light
    # and adding .on() to the end.
    # Can you write the code to turn the red light on?
    # Save your code and give it a try!


The gPiO box now works with the micro:bit, You can see a short video of this on our Facebook page.

The code below shows the Traffic Light sequence using the micro:bit Block Editor.

Traffic Light code for micro:bit


The suggested next project is the Automated Lighthouse.