Skip to content

💻 Coding & Logic

Traffic lights are state machines

Run a pedestrian crossing as a "state machine", tune how long each light lasts, and add a push button. Many machines around you work this way.

⏱️ 30 min🎂 Ages 9–12, 13–15📈 medium
  • Computational thinking
  • Problem solving
  • Measuring & data

🧰 You will need

  • 2 × Piece of thin card (cereal box)
  • 1 × Pencils and crayons
  • 1 × Scissors
  • 1 × Stopwatch (a phone works)

🚦 A machine that is always in one “state”

A traffic light is always in exactly one state: cars go, cars slow down, people walk, or “hurry up, the walk is ending”. It moves from one state to the next when something happens: a timer runs out, or someone presses the button. Engineers call this a state machine. Washing machines, lifts, microwaves and game characters all work the same way.

🎮 Try it on the screen

Watch the state diagram light up as the crossing changes. Change how long each state lasts. Then switch on Wait for the button and press to cross.

🧍🚶🚗

State machine

  1. 1. Cars go
  2. 2. Cars slow down
  3. 3. People walk
  4. 4. Hurry up!

State: Cars go · waiting for someone to press the button

🛠️ Time a real crossing

  1. With a grown-up, stand safely back from a real pedestrian crossing (never step into the road).
  2. Use a stopwatch to time each state: how long is green for cars? How long is the walk sign on? How long is the flashing “hurry” part?
  3. Does the light change straight away when someone presses the button? Or does it wait? Write down what you notice.
  4. At home, cut out red, amber and green circles from card. Draw the state diagram on paper: a bubble for each state, with arrows showing what makes it change.
  5. Act it out: one person is the light (holding up the circles), one is the timer, one presses the button.

💡 What happened?

A state machine has:

  • States: what the machine is doing now.
  • Transitions: arrows from one state to the next.
  • Triggers: what makes it move (a timer, a button, a sensor).

Because it can only be in one state at a time, it can never show green to cars and walk to people. That’s why engineers use state machines for safety: you can check every arrow on the diagram.

Did it work?