Lesson 25: Pull-down resistor
Why a button needs a resistor
- A digital pin reads 1 when it is near 5 V and 0 when it is near 0 V.
- A button only joins two points while you press it. When it is up, the pin may be joined to nothing: then it floats and reads whatever it read before.
- A pull-down or pull-up resistor gives the pin a clear value when the button is up.
Words for this lesson
| Word | Meaning |
|---|---|
| float | a pin joined to nothing: its value is not clear |
| pull-down | a resistor from the pin to GND: the pin reads 0 until you press |
| pull-up | a resistor from the pin to 5V: the pin reads 1 until you press |
| kΩ | kilo-ohm: 1 kΩ = 1000 Ω |
Two ways to wire a button
| Pull-down | Pull-up | |
|---|---|---|
| resistor | D2 to GND | 5V to D2 |
| button | 5V to D2 | D2 to GND |
| button up | reads 0 | reads 1 |
| button pressed | reads 1 | reads 0 |
- With a pull-down, pressing gives 1. With a pull-up, pressing gives 0: your code must check for
LOW. INPUT_PULLUP(lessons 5 and 11) is a pull-up resistor inside the chip, so you do not need to build one.
How much current?
- Pressed, the 10 kΩ resistor sits between 5 V and 0 V: I = 5 V ÷ 10 000 Ω = 0.5 mA. Very little, so the battery lasts.
- With a wire instead of the resistor, pressing the button would join 5V straight to GND: a short circuit.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
This time the button goes to 5V, not to GND, and the sketch uses plain INPUT. Build it: a button from the 5V row to the D2 row, and a 10 kΩ resistor from D2 down to the GND row. The LED on D13 is already wired; the sketch lights it while the button is pressed. Try it first without the resistor and see what goes wrong.
Clique em Executar para ver a saída aqui.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
Now the other way round: a 10 kΩ resistor from 5V to D2 (a pull-up), and the button from D2 to GND. Build it. Now D2 reads 1 when the button is up and 0 when it is pressed, so change the code: the LED on D13 should still light only while the button is pressed.
Clique em Executar para ver a saída aqui.