micro:bit flashlight
A pushbutton on ring 1 of a micro:bit lights a lamp on ring 0 and a heart on the LED matrix while it is held. The whole build, a BBC micro:bit and 3 more parts, 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $21. On Mokxi they're free.
What you'll learn
- INPUT_PULLUP so a button needs no resistor
- Reading a button that is LOW when pressed
- Drawing pictures on the micro:bit LED matrix
- Checking the state of several buttons
- Making a click with tone()
- Difficulty: Beginner
- Time: About 15 minutes
- Board: BBC micro:bit V2
- Parts: 5
How it works
A pushbutton on pin 1 is wired to ground and uses the internal pull-up, so it reads LOW while pressed. No resistor is needed for the button. Pin 0 drives a red LED through a 220 ohm resistor.
While you hold the button, the LED lights and the 5 by 5 LED matrix shows a heart. The board's own buttons A and B change the picture to a happy or sad face, and each press clicks the speaker. With nothing pressed, it shows a neutral face.
Parts list
5 parts, plus the jumper wires. They retail for about $21; on Mokxi they're free.
- 1 × BBC micro:bit V2
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 220 Ω pin 1 | BBC micro:bit V2 pin 0 |
| Pushbutton pin 1a | BBC micro:bit V2 pin 1 |
| LED, red pin C | BBC micro:bit V2 pin GND |
| Pushbutton pin 2a | BBC micro:bit V2 pin GND |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: A and B change the picture; a pushbutton on pin 1 lights an LED.
//
// Buttons A and B are on the board, wired to P0.14 and P0.23 with pull-ups,
// so they read LOW while held; buttonA() and buttonB() read them for you.
// Each press also clicks the speaker on P0.00: tone() is a plain square wave
// on the pin, which is all the speaker needs.
//
// Pin 1 is an ordinary input with INPUT_PULLUP, so a pushbutton from it to
// GND needs no resistor. Pin 0 drives an LED through 220 ohms to GND while
// that button is held, and the matrix shows a heart.
const int LED_PIN = 0;
const int BUTTON_PIN = 1;
const char *NEUTRAL = ". . . . ."
". # . # ."
". . . . ."
". . . . ."
"# # # # #";
const char *HAPPY = ". . . . ."
". # . # ."
". . . . ."
"# . . . #"
". # # # .";
const char *SAD = ". . . . ."
". # . # ."
". . . . ."
". # # # ."
"# . . . #";
const char *HEART = ". # . # ."
"# # # # #"
"# # # # #"
". # # # ."
". . # . .";
bool wasA = false;
bool wasB = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
showLeds(NEUTRAL);
}
void loop() {
bool a = buttonA();
bool b = buttonB();
bool held = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_PIN, held ? HIGH : LOW);
if (held) {
showLeds(HEART);
} else if (a) {
showLeds(HAPPY);
} else if (b) {
showLeds(SAD);
} else {
showLeds(NEUTRAL);
}
// A short note on the way down, not while the button is held.
if (a && !wasA) {
tone(SPEAKER, 880, 60);
}
if (b && !wasB) {
tone(SPEAKER, 440, 60);
}
wasA = a;
wasB = b;
delay(10);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes pin 0 an output for the LED and pin 1 an input with the internal pull-up for the button. Then it shows the neutral picture on the matrix.
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
showLeds(NEUTRAL);
}loop sketch.ino
Reads buttons A and B and the pin 1 button. The LED follows the pin 1 button. The matrix shows a heart while it is held, otherwise happy for A, sad for B or neutral. A short tone plays when A or B is newly pressed.
void loop() {
bool a = buttonA();
bool b = buttonB();
bool held = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_PIN, held ? HIGH : LOW);
if (held) {
showLeds(HEART);
} else if (a) {
showLeds(HAPPY);
} else if (b) {
showLeds(SAD);
} else {
showLeds(NEUTRAL);
}
// A short note on the way down, not while the button is held.
if (a && !wasA) {
tone(SPEAKER, 880, 60);
}
if (b && !wasB) {
tone(SPEAKER, 440, 60);
}
wasA = a;
wasB = b;
delay(10);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED does not light This project
Check the LED direction: anode to the resistor from pin 0, cathode to ground. - The heart never shows This project
The button joins pin 1 to ground. A wire on the wrong row of the breadboard will not close it. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.