A micro:bit traffic light is a simple project: you control three LEDs (red, amber and green) from the micro:bit's LED matrix or from external LEDs on pins 0, 1 and 2, and loop through the sequence red → red and amber together → green → amber → red. The easiest version needs no extra parts at all, because the micro:bit's built-in 5x5 display can show coloured patterns using the MakeCode blocks. If you want a physical model with real bulbs, add three LEDs, three resistors and some jumper wires or a breadboard.
This guide uses Microsoft MakeCode for micro:bit (makecode.microbit.org), the official editor, and works on both the web editor and the desktop app. Menu and block names can differ slightly between editor versions, and the simulator looks a little different on phones and tablets, so if a label is not exactly as described, look for the closest match.
What You'll Need
- A micro:bit (V1 or V2) and a USB cable to connect it to your computer
- A computer with a modern browser, or the MakeCode app for micro:bit
- Optional, for a physical model: three LEDs (red, amber/yellow, green)
- Optional: three resistors (about 220 Ω, or the value your LED supplier recommends) to protect the LEDs
- Optional: a breadboard and jumper wires, or a commercially made traffic light add-on board
- Optional: a battery pack if you want the model to run untethered
The electronics steps assume low-voltage LED work on the micro:bit's own pins. If you plan to switch anything from mains power, stop and use a qualified electrician; the micro:bit pins cannot drive mains loads.
Step-by-Step
Part 1: The no-parts version (display only)
- Open makecode.microbit.org in your browser and click New Project. Give it a name such as traffic-light.
- Because the built-in display is a single colour (the LEDs are red on most boards), you show each state as a pattern or a letter. A common approach is a large block of lights for "stop", a small dot for "get ready" and a circle or arrow for "go". From the Basic toolbox, drag
show ledsinto theon startblock and click squares to draw a red-like full block. - Drag a
foreverloop from the Basic toolbox onto the canvas, belowon start. Everything inside it repeats endlessly. - Inside
forever, add ashow ledsblock that draws your "stop" pattern, then apauseblock from Basic set to 3000 ms (3 seconds). - Add a second
show ledsfor the "get ready" pattern with a 1000 ms pause, then a third for "go" with a 3000 ms pause, and finish with a fourth "get ready" pattern and a 1000 ms pause. You now have a full cycle: stop, ready, go, ready, stop. - Click the simulator's play button to watch it run. Adjust pauses until the timing feels realistic. Real junctions often give the amber phase about a quarter of the time of red and green; for a classroom model, 3 seconds red, 1 second amber, 3 seconds green and 1 second amber is easy to follow.
- Connect the micro:bit with the USB cable. It appears as a drive called MICROBIT. Click Download, then copy the downloaded
.hexfile onto that drive. The yellow light on the back flashes while it copies, then the program starts automatically.
Part 2: The physical version with LEDs
- Wire each LED to its own pin. A typical layout is red LED to pin 0, amber to pin 1 and green to pin 2, with each LED's cathode (short leg) going to the GND pin and its anode (long leg) going through a resistor to the pin. Use the micro:bit's edge connector with an adapter board, or a breakout board, so you have proper labelled pins.
- In MakeCode, open Advanced and choose Pins. If you cannot see it, click the
...(more) button in the toolbox. - In
on start, adddigital write pin P0 to 0,digital write pin P1 to 0anddigital write pin P2 to 0so everything is off at the beginning. This prevents a random LED glowing before the loop starts. - Use a
foreverloop and add one block per phase. For the red phase:digital write pin P0 to 1,digital write pin P1 to 0,digital write pin P2 to 0, thenpause 3000. - For the red-and-amber phase: P0 on, P1 on, P2 off, then
pause 1000. - For the green phase: P0 off, P1 off, P2 on, then
pause 3000. - For the amber phase: P0 off, P1 on, P2 off, then
pause 1000. The loop then returns to the red phase. - Download the program the same way as before. If an LED stays dark, check its polarity and the resistor seating. Never connect an LED directly across a pin and GND without a resistor; the pin can be damaged.
Common Mistakes
- Putting blocks beside the loop instead of inside. Blocks that sit outside
foreverrun only once. Drop them clearly inside the loop's C-shape. - Forgetting to turn the previous light off. Setting P2 to 1 without setting P0 back to 0 leaves two lights on at once. Every phase must set all three pins, not just the one that changes.
- Reversed LED legs. The long leg (anode) goes toward the pin, the short leg toward GND. A backwards LED simply will not light.
- Timing in the wrong units. The
pauseblock is in milliseconds; 3 seconds is 3000, not 3. - Download failing because the file stays in the Downloads folder. You have to copy the
.hexfile to the MICROBIT drive, not just download it.
Tips
- Use a variable such as
stateto track which phase you are in, so you can later add a pedestrian button that interrupts the cycle. - For a button-added version, use
on button A pressedto light the red LED and hold it for a set time before resuming. - Test on the simulator first: it shows pin states and the display, so you catch logic errors before plugging anything in.
- Label your jumper wires with tape if you are building a reusable classroom kit; it saves swapped-pin debugging later.
- Wrap your code in a single
foreverloop rather than repeating the sequence, so changing timing means changing one place.
Troubleshooting
Nothing happens after download. Unplug and replug the USB cable, make sure the cable carries data (some are charge-only), and confirm the file copied successfully. Press the reset button on the back of the board.
The program runs but the wrong LED lights. Your pin numbers do not match the wiring. Swap the pin numbers in the Pins blocks or move the wire to the matching pin, then download again.
A single LED is dim or flickers. Check the resistor value and the breadboard seating. Also confirm you are not driving more than one LED from the same pin.
The sequence is too fast or too slow. Change the pause values, remembering that 1000 equals one second.
The editor will not load or upload. Update your browser, try a different USB port, and use the official editor at makecode.microbit.org. Clearing browser data can sign you out and remove locally saved projects, so download a copy of your .hex first.
If none of that works, rebuild the program from scratch with a single phase in the loop and confirm it runs, then add one phase at a time.
Frequently Asked Questions
Can I build a traffic light with no extra parts?
Yes. The built-in 5x5 display can show patterns for each phase, so you only need the micro:bit and a USB cable.
Which pins should I use for the LEDs?
Any of the large edge-connector pins work, but pins 0, 1 and 2 are the most commonly used and easiest to reach with a breakout board.
Do I need resistors?
Yes for bare LEDs. A resistor in series limits current and protects both the LED and the micro:bit pin. Add-on boards with built-in LEDs usually already include them.
Can I add a pedestrian button?
Yes. Use on button A pressed to switch to a red-light hold, then continue the loop. Buttons A and B are built into the board, so no wiring is needed.
Will it run without a computer?
Yes, once downloaded. Power it from a battery pack connected to the micro:bit's battery connector; the program stays in memory and starts on power-up.
Can it control real traffic lights?
No. The micro:bit drives low-voltage signals only. Anything connected to mains power must be handled by a licensed electrician using proper isolation and switching equipment.