micro:bit blink
A heart beating on the micro:bit's LED matrix, the board's own blink. The whole build, a BBC micro:bit, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $16. On Mokxi they're free.
What you'll learn
- Drawing a picture on an LED matrix
- Describing a picture as rows of text
- Animating with two frames
- Timing frames with delay
- Difficulty: Beginner
- Time: About 5 minutes
- Board: BBC micro:bit V2
- Parts: 1
How it works
The micro:bit has a grid of 25 LEDs and no single LED to blink. So the sketch blinks the whole grid at once, showing a big heart for half a second and a small heart for half a second.
Each picture is written as five rows of five. A # is a lit LED and a dot is a dark one. The board lights the grid by scanning one row at a time, which is how it drives 25 LEDs from ten pins.
Parts list
1 part, plus the jumper wires. They retail for about $16; on Mokxi they're free.
- 1 × BBC micro:bit V2
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Blink: a heart beating on the LED matrix.
//
// The micro:bit has no single LED to blink, so this blinks all of them at
// once: a big heart, then a small one, twice a second. showLeds() takes the
// picture as five rows of five, # for a lit LED and . for a dark one; the
// spaces are only there so the rows look like the picture.
//
// Underneath, the runtime scans the matrix a row at a time from the SysTick
// interrupt, driving each row's pin high and the columns of its lit LEDs low,
// which is how the real board lights 25 LEDs from ten pins. delay() parks the
// core on WFI between ticks, so this sketch costs the simulator almost nothing.
void setup() {
}
void loop() {
showLeds(". # . # ."
"# # # # #"
"# # # # #"
". # # # ."
". . # . .");
delay(500);
showLeds(". . . . ."
". # . # ."
". # # # ."
". . # . ."
". . . . .");
delay(500);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
It is empty. The LED grid needs no setup before showLeds is called.
void setup() {
}loop sketch.ino
Calls showLeds with the big heart, waits 500 ms with delay, shows the small heart, and waits another 500 ms. Then it repeats, so the heart beats twice a second.
void loop() {
showLeds(". # . # ."
"# # # # #"
"# # # # #"
". # # # ."
". . # . .");
delay(500);
showLeds(". . . . ."
". # . # ."
". # # # ."
". . # . ."
". . . . .");
delay(500);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The picture looks wrong or shifted. This project
Each picture needs exactly five rows of five symbols, using # and a dot. - The heart beats too fast or too slow. This project
Change the two500values indelay. They are in milliseconds. - The pins give odd readings, or damage follows. BBC micro:bit V2
The micro:bit works at 3.3 V and its pins are not 5 V tolerant. The big ring pads 0, 1, 2, 3V and GND are easy to wire with alligator clips. Use a breakout board for more pins. - The LED matrix flickers or an input pin acts wrong. BBC micro:bit V2
Some pins are shared with the LED matrix and buttons. Using the display and analog reads on those pins together can clash. Check the pin map before choosing which pin to use.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.