Home / Learn / Electronics basics

Pull-up and pull-down resistors: why floating inputs misbehave

New to this? Read from the top. Already know the basics? Jump to Going deeper for intermediate and advanced detail.

A pull-up resistor: 10 kilohms from 5 volts to an input pin with a button to ground. Without it the pin floats and reads random values; with it the reading is a clean high until the button pulls it low
Beginner

Wire a push button straight between a microcontroller pin and 5 V, and the pin will read HIGH when the button is pressed. Release it and, instead of a clean LOW, the reading wanders between HIGH and LOW at random. The pin is floating. A pull-up or pull-down resistor fixes it, and it's one of the first things everyone gets caught by.

Why inputs float

A microcontroller input draws almost no current. With nothing connected, it's like an antenna: it picks up charge from nearby wires, your hand and mains wiring in the walls, so its voltage drifts. The pin reports whatever the drift happens to be.

The fix is a resistor that gently holds the pin at a known level when nothing else is driving it.

Pull-up and pull-down

  • Pull-up: a resistor from the pin to the supply (5 V or 3.3 V), and the button from the pin to 0 V. Released: the resistor holds the pin HIGH. Pressed: the button connects the pin straight to 0 V, so it reads LOW.
  • Pull-down: a resistor from the pin to 0 V, and the button from the pin to the supply. Released reads LOW, pressed reads HIGH.
+5 V10 kΩto pinbuttonPull-up: pressed = LOW +5 Vbuttonto pin10 kΩPull-down: pressed = HIGH
Pull-up (left) and pull-down (right). The resistor holds the pin steady; the button overrides it.

Pull-ups are more common, mainly because most microcontrollers have pull-ups built in. The only oddity is that the logic is inverted: LOW means pressed.

Use the built-in pull-ups

You often don't need an external resistor at all.

Arduino

const int BUTTON = 2;

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);   // turns on the internal pull-up
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {   // LOW = pressed with a pull-up
    Serial.println("pressed");
  }
  delay(50);
}

On the Uno's ATmega328P, the datasheet gives the internal pull-up as 20 to 50 kΩ.

Raspberry Pi Pico (MicroPython)

from machine import Pin
import time

button = Pin(14, Pin.IN, Pin.PULL_UP)

while True:
    if button.value() == 0:      # 0 = pressed with a pull-up
        print("pressed")
    time.sleep_ms(50)

The RP2040 datasheet gives its pull resistors as roughly 50 to 80 kΩ, and the Pico also has pull-downs (Pin.PULL_DOWN). On a Raspberry Pi computer, GPIO Zero's Button turns on a pull-up for you; see GPIO Zero.

Choosing an external resistor

When the button is pressed, current flows through the resistor: 5 V ÷ 10 kΩ = 0.5 mA, which is wasted. A bigger resistor wastes less but is "weaker", so long wires pick up more interference and the pin is slower to recover. As a rule of thumb:

  • 10 kΩ is the usual choice for buttons and switches on short wires.
  • 1 to 4.7 kΩ for long cable runs or electrically noisy places.
  • 100 kΩ or more only on battery projects where every microamp counts, with short wires.

Use an external resistor when the internal one is too weak, when you need a pull-down on a board that has none, or to make sure the pin is at a safe level while the microcontroller is still starting up.

I²C needs pull-ups too

The I²C bus lines (SDA and SCL) are open-drain: devices can only pull them low, so pull-up resistors are what pull them high. Many sensor breakout boards already include pull-ups, often 4.7 kΩ or 10 kΩ. If you connect several boards, those resistors end up in parallel and can become too strong, so check. More in I²C, SPI and UART explained.

Switch bounce

A pull-up gives a clean level, but a mechanical button's contacts bounce for a few milliseconds when pressed, so one press can read as several. Ignore changes for 20 to 50 ms after the first one in software (see millis() vs delay()), or add a small capacitor across the button.

Intermediate and advanced

Going deeper

Calculating the I²C pull-up range

NXP's I²C specification gives limits both ways. The smallest resistor is set by the most current a device must be able to sink while still pulling the line below 0.4 V: Rmin = (VDD − 0.4 V) ÷ 3 mA in standard and fast mode, about 967 Ω at 3.3 V. The largest is set by the rise time with the bus capacitance: Rmax = tr ÷ (0.8473 × Cb). With the 300 ns rise time allowed in fast mode (400 kHz) and 200 pF of bus capacitance, that's about 1.77 kΩ. In standard mode (100 kHz, 1,000 ns), 200 pF allows about 5.9 kΩ. Which is why 4.7 kΩ suits short, 100 kHz buses but may be too weak for a busy 400 kHz bus.

RC debouncing

A capacitor from the pin to 0 V, with the pull-up, makes a low-pass filter. With 10 kΩ and 100 nF, the time constant is 1 ms; on release, the pin takes about 0.7 ms to reach half the supply. That smooths fast bounces. If the edges are then too slow for a clean digital read, feed the signal through a Schmitt-trigger input (many microcontroller pins have hysteresis built in) or a 74HC14.

Leakage on high-value pull-ups

Pin leakage is specified at up to about 1 µA on many microcontrollers. Through a 1 MΩ pull-up, 1 µA drops 1 V, which eats a lot of the noise margin. That's why values above a few hundred kilohms are rarely a good idea even when power is precious.

Strapping pins

Some pins are read at reset to choose a boot mode: on the ESP32, for example, GPIO0 and several others are strapping pins. A pull-up or pull-down on those can stop the board booting. Check the board's pinout before adding one; see the ESP32 pinout.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.