---
title: Digital inputs and pull-up resistors
heading: Reading a button, and why a pin needs a pull-up
eyebrow: C12 · Lesson 7 · Digital inputs and pull-up resistors
xp: 40
---

::: hook
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.
:::

::: play
- 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 `PULLUP` to 1 and run again: now the pin reads 1 until the press, 0 during it, and the LED follows the button exactly.
:::
:::

```sim board
lang: c
attach: led P0 | yellow
attach: button P4 | press 0.2..0.6 s
scope: P4, P0
run: 0.8 s
code:
#include "board.h"

#define PULLUP 0          // 1 turns on P4's internal pull-up

int main(void) {
    GPIO_DIR = 1 << 0;                 // P0 out (the LED), P4 in (the button)
    if (PULLUP) GPIO_PULLUP = 1 << 4;
    while (1) {
        int pressed = !((GPIO_IN >> 4) & 1);   // pressed pulls the pin to 0
        if (pressed) GPIO_SET = 1 << 0;
        else GPIO_CLR = 1 << 0;
    }
}
```

::: 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?

- [ ] 1 pressed, 0 released
- [x] 0 pressed, 1 released
- [ ] 0 pressed, and anything released
- [ ] 1 both times

::: reveal
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.
:::
:::

::: nameit
## Inputs and thresholds

A CMOS input draws almost no current, so it reads whatever voltage reaches it. This board reads below $V_{IL}$ = 0.3 × 3.3 = 0.99 V as 0 and above $V_{IH}$ = 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 $3.3/R$ 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:

$$t_{rise} = RC\ln\frac{1}{1 - V_{IH}/V_{DD}} = 1.2\,RC$$

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()` |
:::

::: example
## A button pulls a 10 kΩ pull-up to ground. What current flows while it's pressed, and how much power is wasted?

::: solution
1. **Current while pressed.** The pull-up has the whole 3.3 V across it:
   $$\begin{aligned} I &= \frac{3.3\ \text{V}}{10\,000\ \Omega} \\ &= 0.33\ \text{mA} \end{aligned}$$
2. **Power.**
   $$\begin{aligned} P &= \frac{3.3^2}{10\,000}\ \text{W} \\ &= 1.1\ \text{mW} \end{aligned}$$
   only while pressed.
3. **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.
:::
:::

::: example
## A 10 kΩ pull-up charges 20 pF of pin and wire. How long after release does the pin read 1?

::: solution
1. **Rise time with 10 kΩ.**
   $$\begin{aligned} t &= 1.2 RC \\ &= 1.2 \times 10^4 \times 20 \times 10^{-12}\ \text{s} \\ &= 0.24\ \mu\text{s} \end{aligned}$$
2. **With the internal pull-up.** The internal 40 kΩ takes about 1 µs.
3. **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Ω.
:::
:::

::: example
## Eight active-low buttons with pull-ups sit on GPIO bits 0 to 7, and GPIO_IN reads 0xF3. Which buttons are pressed?

::: solution
1. **Write it in binary.**
   $$\text{0xF3} = 1111\ 0011$$
2. **Read it active-low.** A pressed button reads 0, so bits 2 and 3 are pressed and the rest are released.
3. **Invert it in code.** `~GPIO_IN & 0xFF` turns 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
given: R = 4.7 | 10 | 47 | 100
answer: 3.3/(R*1e3)*1e6
unit: µA
tolerance: 1%

## A button pulls a {R} kΩ pull-up to ground. What current flows while it's held?

::: right
Right. $3.3/R$ = {answer} µA.
:::

::: misses
- [any] Not yet. Ohm's law across the resistor.
:::
:::

::: check
given: R = 2.2 | 10 | 40; C = 10 | 30 | 100
answer: 1.2*R*1e3*C*1e-12*1e9
unit: ns
tolerance: 2%

## A {R} kΩ pull-up charges {C} pF. How long until the pin crosses $V_{IH}$ (70% of the supply)?

::: right
Right. $RC\ln(1/0.3)$ = {answer} ns.
:::

::: misses
- [R*1e3*C*1e-12*1e9] One time constant only reaches 63%; 70% takes $\ln(1/0.3) = 1.2$ time constants.
- [any] Not yet. $t = RC\ln(1/(1 - 0.7))$.
:::
:::

::: check
given: f = 0.2 | 0.25 | 0.3 | 0.35 | 0.65 | 0.7 | 0.75 | 0.8
answer: f*3.3
unit: V
tolerance: 0.5%

## On a 3.3 V CMOS input, a threshold is {f} × the supply. What voltage is that?

::: right
Right. {answer} V.
:::

::: misses
- [any] Not yet. {f} × 3.3 V.
:::
:::

::: check
given: v = 0x0010 | 0x0011 | 0x2010 | 0x0001 | 0xFFEF; n = 4
answer: floor(v/2^n) - 2*floor(v/2^(n + 1))
tolerance: 0

## GPIO_IN reads {v:hex4}. What does `(GPIO_IN >> 4) & 1` give, the level of P4?

::: right
Right. {answer}.
:::

::: misses
- [any] Not yet. Shift right 4 and keep bit 0.
:::
:::

::: check
given: R = 10 | 47 | 100; h = 1 | 8 | 24
answer: 3.3/(R*1e3)*h*1000
unit: mAh
tolerance: 1%

## A {R} kΩ pull-up's button is held for {h} hours a day. How much charge does the pull-up draw each day?

::: right
Right. $3.3/R \times h$ = {answer} mAh.
:::

::: misses
- [any] Not yet. Current times hours.
:::
:::

::: check
kind: choice

## A pin with no pull-up has a button to ground. While released, what does a program read?

- [ ] Always 1
- [ ] Always 0
- [x] Unpredictable values that can change from read to read
- [ ] The last value it read

::: misses
- [1] Only with a pull-up.
- [2] Only while pressed, or with a pull-down.
- [4] A floating input has nothing holding its level.
:::
:::

::: check
kind: micropython
attach: led P0; button P4 | press 0.2..0.5 s
run: 0.7
expect: first(P0) = 0.2 s ± 2%; edges(P0) = 2; level(P0) = 0

## A button to ground is on P4, an LED on P0. Light the LED while the button is pressed, and only then.

```micropython starter
from machine import Pin

led = Pin(0, Pin.OUT)
button = Pin(4, Pin.IN)
while True:
    led.value(button.value())
```

```micropython solution
from machine import Pin

led = Pin(0, Pin.OUT)
button = Pin(4, Pin.IN, Pin.PULL_UP)
while True:
    led.value(not button.value())
```

::: right
Right: a pull-up so released reads 1, and `not` because pressed reads 0.
:::
:::

::: check
kind: c
attach: led P0; button P4 | press 0.1..0.3 s
run: 0.4
expect: first(P0) = 0.1 s ± 2%; edges(P0) = 2

## The same in C: light the LED on P0 while the button on P4 (to ground) is pressed.

```c starter
#include "board.h"

int main(void) {
    GPIO_DIR = 1 << 0;
    while (1) {
    }
}
```

```c solution
#include "board.h"

int main(void) {
    GPIO_DIR = 1 << 0;
    GPIO_PULLUP = 1 << 4;
    while (1) {
        if ((GPIO_IN >> 4) & 1) GPIO_CLR = 1 << 0;
        else GPIO_SET = 1 << 0;
    }
}
```

::: right
Right. The pull-up makes released read 1, so the LED is on exactly when the pin reads 0.
:::
:::

::: check
kind: parts
given: R = 4.7 | 10 | 47

## A button pulls a {R} kΩ pull-up on a 3.3 V pin to ground.

::: part
answer: 3.3/R
unit: mA
tolerance: 0.5%

### What current flows while it's pressed?

::: misses
- [any] 3.3 V over the pull-up.
:::
:::

::: part
answer: 3.3^2/R
unit: mW
tolerance: 0.5%

### How much power is wasted while it's pressed?

::: misses
- [any] $V^2/R$.
:::
:::
:::

::: check
kind: parts
given: R = 10 | 47 | 100; C = 10 | 30; v = 1.6 | 2.3

## A {R} kΩ pull-up charges {C} pF of pin and wire after a button is released; the pin reads 1 above {v} V.

::: part
answer: R*1000*C*1e-12*ln(3.3/(3.3 - v))*1e6
unit: µs
tolerance: 0.5%

### How long until it reads 1?

::: misses
- [any] $RC\ln(3.3/(3.3 - V_{th}))$.
:::
:::

::: part
answer: R*1000*C*1e-12*1e6
unit: µs
tolerance: 0.5%

### What is the time constant?

::: misses
- [any] RC.
:::
:::
:::

::: explain
## Why does a button to ground need a pull-up resistor, and why not just wire the pin straight to 3.3 V instead?

::: model
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

::: check
given: R = 4.7 | 10 | 22 | 33 | 47 | 68 | 100
answer: 3.3/(R*1e3)*1e6
unit: µA
tolerance: 1%

## Current through a pressed {R} kΩ pull-up?

::: misses
- [any] $3.3/R$.
:::
:::

::: check
given: R = 5 | 40; C = 15 | 50
answer: 1.2*R*1e3*C*1e-12*1e9
unit: ns
tolerance: 2%

## Rise time to 70% through {R} kΩ into {C} pF?

::: misses
- [any] $1.2RC$.
:::
:::

::: check
given: v = 0x0007 | 0x0010 | 0x0030 | 0x0071 | 0x00A8 | 0x00EF | 0x01FF
answer: floor(v/16) - 2*floor(v/32)
tolerance: 0

## P4's level when GPIO_IN is {v:hex4}?

::: misses
- [any] Bit 4.
:::
:::
:::
