← Teacher guide

A button is an input

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.

Circuits with the micro:bit · Reading inputs · about 40 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 6 min · Worked example 10 min · Vary 8 min · Tinkercad 8 min · Practise and Make 8 min

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

What you will seeA question to askThe fix
A student thinks pin 0 reads 3.3.Digital read gives one of two numbers. Which?1 for 3.3 V, 0 for 0 V.
A student thinks the pull-down stops the button working.Which is a stronger join to pin 0: the button or a 10 kΩ resistor?The button: it is a direct join.

Answers to the checks

  1. press it: The button joins 3V to pin 0 while you hold it. While you hold it, digital read pin P0 gives…
    1. Holding the button joins pin 0 to 3.3 V, and digital read reports that as 1.
  2. Hold: Hold the button. What does pin 0 read, and what does the micro:bit show?

    1, and a heart: the button joins pin 0 to 3.3 V.
  3. Let go: Let go. And now?

    0, and the screen clears: the pull-down resistor joins pin 0 to GND.
  4. What is pin 0 joined to?: 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?
    The button joins it straight to 3V; the 10 kΩ only lets a tiny current leak to GND
    The button is a direct join to 3.3 V. The big resistor to GND only lets about 0.3 mA through, so pin 0 stays at 3.3 V.
  5. Find the mistake: Sam's doorbell sometimes rings when nobody has pressed it. Click the line where Sam's reasoning first goes wrong.
    line 2
    Line 2: a pin joined to nothing isn't at 0 V. It floats, picking up electrical noise from nearby wires and your hand, and reads 0 or 1 at random. That is the doorbell ringing by itself.
  6. Float: Remove the pull-down. Don't touch the button: watch pin 0 and the micro:bit.

    Pin 0 reads 0 or 1 at random and the heart flickers: the pin is floating.
  7. Pull it down: Put the pull-down back. Is the reading steady again?

    A steady 0 until you press. The pull-down gives the pin a definite voltage when the button is open.
  8. A press counter: A game counts button presses with no pull-down. What will the score do?
    Go up by itself, as random 1s count as presses
    Each random 0 → 1 looks exactly like a press.
  9. Pin to GND: Pin 1 is joined straight to GND. Digital read pin P1 gives…
    0
    Joined to 0 V, it reads 0.
  10. A pull-up: 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?
    0
    Held: joined to GND, reads 0. Up: the pull-up holds it at 1. The mirror image of a pull-down, and how many boards wire their buttons (Arduino's INPUT_PULLUP).
  11. the doorbell program: Put the program in order so the micro:bit shows a heart while the button is held, and nothing otherwise.
    see the program below
    Forever: read pin P0; if it is 1 show the heart, else clear the screen.
    forever
        if digital read pin P0 = 1 then
            show icon heart
        else
            clear screen
    

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: no pull-down › Float

Open task: any program that does what the step asks.

Vary: no pull-down › Pull it down

Open task: any program that does what the step asks.

Vary: no pull-down › A press counter

Open task: any program that does what the step asks.

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.

Exit ticket

Why does a button wired from 3V to a pin need a resistor from the pin to GND?

So the pin is held at 0 V (reads 0) when the button is up. Without it the pin floats and reads 0 or 1 at random.

What each step is for

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