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 examples of code for the Raspberry Pi, micro:bit and other hardware platforms.

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 these projects. The code for this project is shown below, and you can find a complete list of the code for all of the projects in the Code Library.

For the  Raspberry Pi


This is the Scratch 1.4 version of this project.



Scratch 2

This is the Scratch 2 version of the project.

  • Please note that this requires a  Raspberry Pi version 2 or 3.
  • In addition the code needs a Raspberry Pi with a Raspbian image dated 21/6/2017 or later. There is more information on this here.
Traffic Light Scratch Code

Traffic Light Sequence using Scratch 2



The image below shows the Traffic Light code running in EduBlocks. For more information on EduBlocks click here

Edublocks Image

Edublocks – comparison of Block & Python Views


If you use Flowol(TM) in your school you can use the gPiO box with Flowol to control physical devices directly. For more information on using Flowol with the gPiO Box, please contact us.

Traffic Light Flow Chart Animation

For the micro:bit

Blocks and Javascript

  • The code for this project is shown below.
  • Use the icons in the centre of the top blue bar to switch between Blocks and Javascript.



The suggested next project is the Automated Lighthouse.