← Circuits with the micro:bit

A button is an input

A pin set as an input reads the voltage on it: joined to 3V it reads 1, joined to GND it reads 0. A button joins pin 0 to 3V only while it is held, so a pull-down resistor holds the pin at 0 when it is up. Without one the pin floats and reads 0 or 1 at random.

The micro:bit has buttons A and B built in. Wire your own button to a pin, and it can be a doorbell, a game controller or a sensor on a door. How does a pin know it has been pressed?

Until now pin 0 was an output: code set it to 0 V or 3.3 V. A pin can also be an input: it reads the voltage on it. Digital read gives 1 for 3.3 V and 0 for 0 V. The micro:bit here runs: forever, if digital read pin P0 = 1, show a heart, else clear the screen.

WidthMore contexts, same idea.
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

Press it

The button joins 3V to pin 0 while you hold it. While you hold it, digital read pin P0 gives…

TryTry it on the board and compare the result with your prediction.

Try: hold and let go

Try it yourselfYour edits are saved in this browser.

Hold the button. What does pin 0 read, and what does the micro:bit show?

Use the board. This ticks when you get there.

Let go. And now?

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: two paths to pin 0

Worked exampleBefore each step, say what you think comes next.
  1. held: 3V→button→pin 0\text{held: } 3\text{V} \to \text{button} \to \text{pin 0}The button joins pin 0 straight to 3.3 V.

Now: while the button is held, pin 0 is joined to both. Why does 3V win?

While the button is held, pin 0 is joined to 3V through the button and to GND through the 10 kΩ pull-down. Why does it read 1?

Sam's doorbell sometimes rings when nobody has pressed it. Click the line where Sam's reasoning first goes wrong.

ModifyChange the problem on purpose and see what happens.

Vary: no pull-down

Try it yourselfYour edits are saved in this browser.

Remove the pull-down. Don't touch the button: watch pin 0 and the micro:bit.

Use the board. This ticks when you get there.

Put the pull-down back. Is the reading steady again?

Use the board. This ticks when you get there.

A game counts button presses with no pull-down. What will the score do?

Modify TypicalChange the problem on purpose and see what happens.

Build it in Tinkercad

PractiseA few on your own, to check you have it.

Inputs

Pin 1 is joined straight to GND. Digital read pin P1 gives…

Another way to wire it: the button joins pin 0 to GND, and a 10 kΩ pull-up joins pin 0 to 3V. What does pin 0 read while the button is held?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

The doorbell program

Then build it in Tinkercad, or on a real micro:bit with a button and a resistor on a breadboard.

Put the program in order so the micro:bit shows a heart while the button is held, and nothing otherwise.

Pieces (click to add)
    Your program

      For teachers

      Students hold and release a button wired from 3V to pin 0, see digital read give 1 and 0, trace what pin 0 is joined to in each state, remove the pull-down resistor to see the pin float, and put the reading-and-reacting program in order. About 40 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Before class

      Model: digital read gives 1 for 3.3 V and 0 for 0 V; a floating pin is shown reading at random every half second. Real floating pins are less tidy (they drift with nearby wires and fingers). A real micro:bit may already turn on a weak internal pull-down when it reads a pin (MakeCode's set pull pin block changes it), so Sam's doorbell may behave on a micro:bit; the external resistor makes the reading definite, and most other boards (Arduino in the next unit) need one. The micro:bit's own buttons A and B are already wired with resistors.

      If you have kits: a breadboard, a pushbutton, a 10 kΩ resistor and three jumper wires to an edge connector breakout.

      Running the lesson

      1. Predict: students who chose 3.3 are mixing up the voltage and the reading.
      2. Worked example: ask what pin 0 is joined to, in each state, before revealing the lines.
      3. Vary: watch the faces when the heart flickers by itself.
      4. Practise, pull-up: draw both side by side.

      The hard part (on purpose)

      'Joined to nothing means 0' feels obvious. Let the floating pin surprise them on the board, then ask what the resistor gives the pin.

      When students get stuck

      Stretch and support

      Stretch: How could code ignore a reading that only lasts a moment, so a noisy pin doesn't count as a press?

      Support: Two pictures side by side, held and up, with a highlighter for what pin 0 is joined to.

      Where it leads

      Next: Sensors that vary (analog read gives a number, not just 0 or 1).

      Arduino unit: the same idea as pinMode(pin, INPUT_PULLUP).

      Sources

      What each step is for

      Now you can
      Next lesson4. Sensors that varyAnalog read turns the voltage on a pin, 0 to 3.3 V, into a number from 0 to 1023. A knob or a light sensor in a divider changes that voltage, so the number changes. A program compares the number with a threshold to decide, as a night light does.