Built-in project · BBC micro:bit V2

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.

StarterRuns in your browser. Free, and no account needed.

micro:bit flashlight · BBC micro:bit V2live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · BBC micro:bit V2
// 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);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • BBC micro:bit V2 pin 0; Resistor, 220 Ω pin 1
  • BBC micro:bit V2 pin 1; Pushbutton pin 1a
  • Ground: BBC micro:bit V2 pin GND; LED, red pin C; Pushbutton pin 2a
  • Resistor, 220 Ω pin 2; LED, red pin A

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.