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.
Predict: Read the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.
Try: Try it on the board and compare the result with your prediction.
Investigate: Work out why it happens, one step at a time.
Modify: Change the problem on purpose and see what happens.
Practise: A few on your own, to check you have it.
Make: Make your own problem using what you have learned. Expect a few tries; that is normal.
WidthMore contexts, same idea.
Make your prediction first. Help opens once you've locked it in.
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
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.
held: 3V→button→pin 0The button joins pin 0 straight to 3.3 V.
held: reads 13.3 V reads as 1.
up: pin 0→10 kΩ→GNDThe button is open; the pull-down joins pin 0 to GND.
up: reads 00 V reads as 0.
Now: while the button is held, pin 0 is joined to both. Why does 3V win?
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.
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
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.
I can explain what digital read gives for 3.3 V and 0 V.
I can explain why a button needs a pull-down (or pull-up) resistor.
I can put a program in order that reacts to a button.
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
Predict: students who chose 3.3 are mixing up the voltage and the reading.
Worked example: ask what pin 0 is joined to, in each state, before revealing the lines.
Vary: watch the faces when the heart flickers by itself.
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
A student thinks pin 0 reads 3.3. Ask: Digital read gives one of two numbers. Which? Fix: 1 for 3.3 V, 0 for 0 V.
A student thinks the pull-down stops the button working. Ask: Which is a stronger join to pin 0: the button or a 10 kΩ resistor? Fix: The button: it is a direct join.
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).
press it: Committing to the reading while the button is held tests the new move: an input pin reports the voltage as 1 or 0, not the volts or a press count.
Try: hold and let go: Holding and letting go settles the prediction and shows both readings; the heart is the program acting on them.
Worked example: two paths to pin 0: Tracing what pin 0 is joined to in each state models the move: held, 3V wins through the button; up, the pull-down joins it to GND. Sam's 'nothing means 0' is planted after the method.
Vary: no pull-down: Taking the pull-down away shows Sam's mistake on the board: a floating pin reads at random. Putting it back shows what the resistor is for.
Build it in Tinkercad: Building the button circuit in Tinkercad with the same program checks the idea in a fuller simulator.
inputs: Reading pins from what they are joined to, including a pull-up (the mirror image), and the program that reacts.
the doorbell program: Putting the program in order connects the reading to an action: read the pin, decide, show.
Now you can
Explain what digital read gives for 3.3 V and 0 V.
Explain why a button needs a pull-down (or pull-up) resistor.