How to set the level of a digital input and which resistor value to choose
A CMOS digital input has an extremely high impedance: if it is not connected to anything it is left floating and its level is decided by any electrostatic charge, the capacitance of the cable or the noise of a nearby motor. The result is readings that change on their own and, on top of that, extra current draw, because the input stage sits halfway between 0 and 1. A resistor to the supply (pull-up) or to ground (pull-down) sets a resting level; the push-button, the sensor or the open-drain output only has to force the opposite level.
With a pull-up, the input reads 1 at rest and 0 when pressed (active-low logic); with a pull-down, the other way round. The pull-up is the most common choice: the button goes to ground, which is usually available everywhere, and almost every microcontroller has internal pull-ups.
The resistor value is bounded on two sides:
With a pull-down, the leakage condition is \(n\,I_{\text{leak}}\,R \le V_{IL}\). The rise time \(t_r\) is the time the voltage takes to go from 10 to 90 % (a little over two time constants). In practice, the real upper limit is noise: an input with 1 MΩ picks up interference even if it meets the formula. That is why usual values lie between 4.7 kΩ and 47 kΩ, and 10 kΩ is the default choice for push-buttons.
A strong pull-up is a low value (1 to 4.7 kΩ): it draws more current but gives fast edges and good immunity, useful on long cables and buses. A weak pull-up (47 kΩ to 100 kΩ, or the internal ones) draws little, but is only suitable for short connections on the board. In noisy environments or with cables longer than a few centimetres, an external resistor of a few kΩ is advisable and, if there are push-buttons, a debounce filter.
Almost every microcontroller can enable a pull-up (and many a pull-down too) on each pin. They are weak and their value varies a lot between units:
| Family | Internal pull-up | Internal pull-down | How to enable it |
|---|---|---|---|
| AVR (ATmega328P, Arduino Uno) | 20 to 50 kΩ | No | pinMode(pin, INPUT_PULLUP) |
| ESP32 | about 45 kΩ | about 45 kΩ | INPUT_PULLUP / INPUT_PULLDOWN |
| STM32 | about 40 kΩ | about 40 kΩ | PUPDR register or GPIO_PULLUP in HAL |
| RP2040 (Raspberry Pi Pico) | 50 to 80 kΩ | 50 to 80 kΩ | gpio_pull_up() / gpio_pull_down() |
| PIC (ports with WPU) | weak, tens of kΩ | Depends on model | WPUx registers |
Indicative values from the datasheets; some pins (for example the input-only pins of the ESP32) have no internal resistors.
An open-drain output (open-collector if it is bipolar) only has the transistor that pulls the line to ground: it can drive a 0, but for a 1 it needs an external pull-up. This allows two very useful things: connecting several outputs to the same line without them fighting (any of them can pull it to 0, which forms a wired-AND function) and returning the pull-up to a voltage different from the chip's own, which works as level shifting. Reset lines, shared interrupts, the 1-Wire bus and the I²C bus all work this way.
The SDA and SCL lines of the I²C bus are open-drain and each needs one pull-up (not one per device). The minimum resistance is set by the current the outputs can sink without exceeding the maximum low level; the maximum, by the rise time the specification allows with the bus capacitance.
The factor 0.8473 is \(\ln(0.7/0.3)\): the rise is measured between 30 and 70 % of VCC. Each device adds about 10 pF and each centimetre of track or cable around 1 pF; commercial modules often already carry their own pull-ups (usually 4.7 or 10 kΩ), and when several are connected they end up in parallel.
| Mode | Frequency | Maximum tr | Maximum Cb | IOL at VOL = 0.4 V |
|---|---|---|---|---|
| Standard | 100 kHz | 1000 ns | 400 pF | 3 mA |
| Fast | 400 kHz | 300 ns | 400 pF | 3 mA |
| Fast plus | 1 MHz | 120 ns | 550 pF | 20 mA |
If the minimum resistance turns out higher than the maximum, the bus has too much capacitance for that speed: drop to a slower mode, shorten the wiring or use an I²C repeater or bus extender.
Related: push-button debouncing, logic level shifting, transistor as a switch and Ohm's law.