Micro:bitArduinoMakeCodePythonC++ExplorersBuilders

Police and traffic lights: Micro:bit or Arduino

Start with one blinking LED, make police lights flash, then build a traffic light that listens to a pedestrian button. The same project works on the Micro:bit and on the Arduino.

QR code to this pageScan to open
Your board:

1What you need

A Micro:bit on a breakout board with red, yellow and green LEDs and a push button on a breadboard
  • Micro:bit and a USB cable
  • Breakout board for the Micro:bit (pin:bit)
  • Arduino UNO (R3 or WiFi Rev2) and its USB cable
  • A laptop with the Arduino IDE
  • 4 LEDs: red, yellow, green and blue
  • 4 resistors of 220 Ω (red, red, brown, gold)
  • 1 push button, a breadboard and 7 jumper wires

The blue LED needs almost 3 V, and the Micro:bit gives about 3.3 V, so blue looks dim with 220 Ω. A 100 Ω resistor makes it brighter, if you have one.

Other Arduino boards work with the same pins. In the Arduino IDE choose Arduino Uno for the UNO R3, Arduino Uno WiFi Rev2 for the WiFi board (the first time: Boards Manager, install "Arduino megaAVR Boards"), or Arduino Nano for a Nano (pins D2 to D7 and GND).

An LED with its long leg (plus) and short leg (minus), a 220 ohm resistor, and the loop from a pin through the resistor and the LED to GND

An LED only lights when the current goes in at the long leg and out at the short leg. The resistor keeps the current small. When the program turns the pin on, the LED lights; off, and it is dark.

The Micro:bit in the breakout board on a breadboard: pin 1 to the red LED, pin 2 yellow, pin 8 green, pin 13 blue, pin 14 to the push button, gnd to the minus rail The Arduino UNO next to a breadboard: pin 2 to the red LED, pin 3 yellow, pin 4 green, pin 5 blue, pin 7 to the push button, GND to the minus rail

Build it bit by bit: for level 1 you only need the red LED. Add the blue one for level 2, yellow and green for level 3, and the button for level 4. Each wire goes to the left end of a resistor; the LED's long leg sits in the column of the resistor's right end.

4Try it on screen

The lights lab runs every level, step by step: watch which pins are 1 and which are 0. In level 4, press the button.

Open the lights lab in a new tab

5Program it, level by level

In MakeCode, the pin blocks are under Advanced, then Pins. Go as far as you like: every level works on its own. Click a level to open it.

Start the MakeCode tutorial all 4 levels, step by step

In the Arduino IDE: paste the code (or open the sketch file), choose your board and port, then Upload. Go as far as you like: every level works on its own. Click a level to open it.

Level 1: One LEDon, off, wait

forever
digital write pin P1 to 1
pause (ms) 500
digital write pin P1 to 0
pause (ms) 500
const int redLedPin = 2;

void setup() {
  pinMode(redLedPin, OUTPUT);
}

void loop() {
  digitalWrite(redLedPin, HIGH);   // on
  delay(500);
  digitalWrite(redLedPin, LOW);    // off
  delay(500);
}

Sketch file: level1_blink.ino

A program sets the pin, then waits. Without the pauses the LED would switch thousands of times a second, and you would only see a dim glow.

Your turn: make it blink faster. How short can the pause be before you stop seeing the blinking?

Level 2: Police lightstwo LEDs take turns

forever
digital write pin P1 to 1
digital write pin P13 to 0
pause (ms) 300
digital write pin P1 to 0
digital write pin P13 to 1
pause (ms) 300
const int redLedPin = 2;
const int blueLedPin = 5;

void setup() {
  pinMode(redLedPin, OUTPUT);
  pinMode(blueLedPin, OUTPUT);
}

void loop() {
  digitalWrite(redLedPin, HIGH);
  digitalWrite(blueLedPin, LOW);
  delay(300);
  digitalWrite(redLedPin, LOW);
  digitalWrite(blueLedPin, HIGH);
  delay(300);
}

Sketch file: level2_police_lights.ino

Each step sets both LEDs: one on, the other off. Then a short wait, and the other way round.

Your turn: a double flash, like real police cars: red on, off, on, off, then blue the same.

Level 3: Traffic lightsa fixed order

function lights red yellow green
digital write pin P1 to red
digital write pin P2 to yellow
digital write pin P8 to green
forever
call lights 1 0 0
pause (ms) 3000
call lights 1 1 0
pause (ms) 1000
call lights 0 0 1
pause (ms) 3000
call lights 0 1 0
pause (ms) 1000
const int redLedPin = 2;
const int yellowLedPin = 3;
const int greenLedPin = 4;

// Switch the three lights at once: true is on, false is off.
void setLights(bool red, bool yellow, bool green) {
  digitalWrite(redLedPin, red);
  digitalWrite(yellowLedPin, yellow);
  digitalWrite(greenLedPin, green);
}

void setup() {
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
}

void loop() {
  setLights(true, false, false);   // red: stop
  delay(3000);
  setLights(true, true, false);    // red and yellow: get ready
  delay(1000);
  setLights(false, false, true);   // green: go
  delay(3000);
  setLights(false, true, false);   // yellow: stop if you can
  delay(1000);
}

Sketch file: level3_traffic_lights.ino

The order in Sweden: red, red and yellow, green, yellow, and back to red. A function switches all three lights in one line, so the main loop reads like the order itself.

Your turn: time a real traffic light near you and copy its times.

Level 4: Pedestrian buttoninput

on start
set pull pin P14 to up
forever
call lights 0 0 1
while digital read pin P14 = 1
pause (ms) 20
pause (ms) 1000
call lights 0 1 0
pause (ms) 1000
call lights 1 0 0
pause (ms) 4000
call lights 1 1 0
pause (ms) 1000
const int redLedPin = 2;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int buttonPin = 7;

void setLights(bool red, bool yellow, bool green) {
  digitalWrite(redLedPin, red);
  digitalWrite(yellowLedPin, yellow);
  digitalWrite(greenLedPin, green);
}

void setup() {
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);   // HIGH when not pressed, LOW when pressed: no extra resistor needed
}

void loop() {
  setLights(false, false, true);      // green for the cars
  while (digitalRead(buttonPin) == HIGH) {
    delay(20);                        // wait for the button
  }
  delay(1000);                        // the cars get a moment
  setLights(false, true, false);      // yellow
  delay(1000);
  setLights(true, false, false);      // red: pedestrians cross
  delay(4000);
  setLights(true, true, false);       // red and yellow
  delay(1000);
}

Sketch file: level4_pedestrian_button.ino

The cars keep green until someone presses the button. Pull up makes the pin read 1 while the button is not pressed; pressing it connects the pin to GND, and it reads 0. Keep the level 3 function.

Your turn: make the pedestrians wait at least 10 seconds between two red phases.

Next step, real Python: traffic_lights.py is level 4 in MicroPython. Open it in python.microbit.org and download it to the Micro:bit.

6Challenges

Challenges

  • Emergency: a second button stops all cars: red, then the blue LED flashes, then back to normal.
  • Night mode: the yellow light blinks slowly, the others stay off.
  • Countdown: show how many seconds the pedestrians have left, on the Micro:bit's LEDs or in the Arduino's Serial Monitor.

Explore further

  • A crossing: two traffic lights that never show green at the same time (6 LEDs).
  • Without waiting: on the Arduino, use millis() instead of delay(), so the button works at any moment.
  • Sound: a ticking sound for blind pedestrians while they may cross.

The emergency challenge, solved, for your board. Try it yourself first; if you are really stuck, ask your teacher for the code.

Poster to print (PDF)

Poster to print (PDF)