C12 · Lesson 7 · Digital inputs and pull-up resistors
Reading a button, and why a pin needs a pull-up
An input pin reports whether the voltage on it is high or low, nothing more. Wire a button from the pin to ground and press it: the pin reads 0. Release it, and the pin is connected to nothing at all. It floats, and a floating CMOS input drifts with whatever charge and noise reach it, reading 0 one moment and 1 the next. The fix is a resistor to the supply, a pull-up, that holds the pin high until the button pulls it low. Almost every button, switch and open-drain signal in embedded systems is wired this way, so "pressed" usually reads as 0.
- The program copies the button on P4 to the LED on P0: LED on while the pin reads 0. The button is pressed from 0.2 s to 0.6 s.
- As written, P4's internal pull-up is off. Run it: before and after the press the LED flickers at random, because the released pin floats.
- Change
PULLUPto 1 and run again: now the pin reads 1 until the press, 0 during it, and the LED follows the button exactly.
Predict
A button connects P4 to ground, and P4 has a pull-up. What does P4 read while the button is pressed, and while it's released?
0 while pressed (the button connects the pin straight to ground) and 1 while released (the pull-up holds it at 3.3 V). The logic is active low: the interesting state reads as 0. Without the pull-up, the released state would be floating, reading anything.
Name it
Inputs and thresholds
A CMOS input draws almost no current, so it reads whatever voltage reaches it. This board reads below = 0.3 × 3.3 = 0.99 V as 0 and above = 0.7 × 3.3 = 2.31 V as 1; between them the result isn't guaranteed. A floating input sits wherever stray charge leaves it, often in that middle band.
Pull-ups
A resistor R from the pin to 3.3 V holds an unconnected pin high. When a button pulls the pin to ground, current flows through the resistor for as long as it's pressed. A smaller R gives a stronger, faster, more noise-proof pull-up but draws more current; a larger one saves power but rises slowly, because the pin and wiring capacitance C charge through it:
This board's internal pull-ups are about 40 kΩ: fine for a button, too weak for fast signals. Pull-downs work the same way to ground.
Reading a pin
| C | GPIO_PULLUP |= 1 << 4; then (GPIO_IN >> 4) & 1 |
| MicroPython | b = Pin(4, Pin.IN, Pin.PULL_UP) then b.value() |
Worked example
A button pulls a 10 kΩ pull-up to ground. What current flows while it's pressed, and how much power is wasted?
Show the solution
- Current while pressed. The pull-up has the whole 3.3 V across it:
- Power. only while pressed.
- A battery device. For one held pressed for long periods, a 100 kΩ pull-up cuts that tenfold.
Answer 0.33 mA flows while the button is pressed, wasting 1.1 mW.
Worked example
A 10 kΩ pull-up charges 20 pF of pin and wire. How long after release does the pin read 1?
Show the solution
- Rise time with 10 kΩ.
- With the internal pull-up. The internal 40 kΩ takes about 1 µs.
- Does it matter? That's no problem for a button, but it is a problem for a 1 MHz bus.
Answer about 0.24 µs with the 10 kΩ pull-up, and about 1 µs with the internal 40 kΩ.
Worked example
Eight active-low buttons with pull-ups sit on GPIO bits 0 to 7, and GPIO_IN reads 0xF3. Which buttons are pressed?
Show the solution
- Write it in binary.
- Read it active-low. A pressed button reads 0, so bits 2 and 3 are pressed and the rest are released.
- Invert it in code.
~GPIO_IN & 0xFFturns it into 0x0C, a 1 for each pressed button, which is easier to test.
Answer the buttons on bits 2 and 3 are pressed.
Check
A button pulls a 10 kΩ pull-up to ground. What current flows while it's held?
Write units the way you normally would: 1.5 µA, 1500 nA and 0.0015 mA are the same answer.
Check
A 10 kΩ pull-up charges 10 pF. How long until the pin crosses (70% of the supply)?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
On a 3.3 V CMOS input, a threshold is 0.3 × the supply. What voltage is that?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
GPIO_IN reads 0011. What does (GPIO_IN >> 4) & 1 give, the level of P4?
Check
A 47 kΩ pull-up's button is held for 1 hours a day. How much charge does the pull-up draw each day?
Write units the way you normally would: 1.5 mAh, 1500 µAh and 0.0015 Ah are the same answer.
Check
A pin with no pull-up has a button to ground. While released, what does a program read?
Check
A button to ground is on P4, an LED on P0. Light the LED while the button is pressed, and only then.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
The same in C: light the LED on P0 while the button on P4 (to ground) is pressed.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
A button pulls a 10 kΩ pull-up on a 3.3 V pin to ground.
(a)
What current flows while it's pressed?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
(b)
How much power is wasted while it's pressed?
Check
A 47 kΩ pull-up charges 10 pF of pin and wire after a button is released; the pin reads 1 above 2.3 V.
(a)
How long until it reads 1?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
What is the time constant?
Explain why
Why does a button to ground need a pull-up resistor, and why not just wire the pin straight to 3.3 V instead?
With the button released, a pin wired only to the button connects to nothing, and a CMOS input draws almost no current, so its voltage floats wherever leakage and noise put it and the reading is unpredictable. A pull-up resistor gives it a definite level: with no current flowing, there's no voltage across the resistor, so the pin sits at 3.3 V and reads 1. Wiring the pin straight to 3.3 V would also hold it high, but pressing the button would then short the supply to ground through the button. The resistor limits that current to 3.3 V/R while letting the button win and pull the pin to 0.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.