All articles

    Pull-up and Pull-down Resistors: Why Your Button Reads Random Values

    The floating pin problem in microcontrollers, pull-up and pull-down resistors and how to choose their value with numbers, INPUT_PULLUP on Arduino and ESP32, open-drain outputs, calculating I2C resistors, the MOSFET gate resistor, and the ESP32's boot pins.

    Updated 6 min read

    You wired a button between an Arduino pin and 5 V, and the reading flips randomly between 1 and 0 even when you don't touch it? That isn't a problem with the button; it's a floating pin, and one resistor fixes it. You'll meet the same idea in I2C, MOSFETs and many sensors, so it's worth understanding well.

    The problem: the floating pin

    When the button isn't pressed, the pin isn't connected to anything. And a microcontroller input has a very high resistance (megohms and more), so a tiny current is enough to change its voltage. Any nearby noise changes the reading: 50 Hz mains in the house wiring, your hand nearby, or the neighbouring pin as it switches.

    Try it yourself: leave a pin unconnected, and read its value in the Serial Monitor while you bring your finger close. You'll see it flip between 0 and 1 for no reason.

    The fix: tie it down with a resistor

    Pull-up

    A resistor between the pin and the supply, and the button between the pin and ground:

    • Button not pressed: the resistor pulls the pin HIGH.
    • Button pressed: the pin is connected straight to ground, so it's LOW.
    A 10 kΩ resistor to 5 V: not pressed the reading is HIGH, pressed it's LOW
    A 10 kΩ resistor to 5 V: not pressed the reading is HIGH, pressed it's LOW

    So the logic is inverted: pressed = LOW. That's completely normal and everyone uses it.

    Why doesn't the resistor create a short when the button is pressed? Because it limits the current: with 10 kΩ at 5 V, the current = 5 ÷ 10000 = only half a milliamp, which is Ohm's law.

    Pull-down

    The opposite: the resistor to ground, and the button to the supply. Pressed = HIGH. The logic is easier to read, but it needs an external resistor on the Uno, which has no internal pull-down.

    The easiest: the internal resistor

    Most microcontrollers have a pull-up resistor inside:

    pinMode(2, INPUT_PULLUP);
    // with the button between pin 2 and ground
    if (digitalRead(2) == LOW) { /* pressed */ }

    On the Uno it's about 20–50 kΩ. The ESP32 has internal pull-ups and pull-downs (about 45 kΩ), chosen with INPUT_PULLUP or INPUT_PULLDOWN, except on pins 34 to 39, which are input-only and have no internal resistors at all.

    The internal resistor is weak (a large value), so it's excellent for a button next to the board, but with a long wire or a noisy environment (motors, relays) use a smaller external resistor.

    The resistor value

    • 10 kΩ is the usual value for buttons.
    • Smaller (say 1 kΩ) → more resistant to noise, but draws more current when the button is pressed (5 mA with 1 kΩ).
    • Larger (say 100 kΩ) → less current, but weaker against noise, especially with a long wire.
    ResistorCurrent when pressed (5 V)Suitable for
    1 kΩ5 mAA long wire in a noisy environment
    4.7 kΩabout 1 mAI2C, and buttons on a metre or two of wire
    10 kΩhalf a mAMost buttons
    47–100 kΩunder 0.1 mABattery projects where the button stays pressed

    In a battery project, if the button can stay pressed for hours (like a closed door switch), that current difference shows up in battery life.

    Open-drain outputs

    Many sensors and chips have an open-drain (or open-collector) output: a transistor inside that can only pull the line to ground, and can't drive it HIGH by itself. Without a pull-up, the line floats when the transistor turns off. Examples:

    • The data line of the DS18B20 sensor: it needs 4.7 kΩ to the supply.
    • The alert pin (INT or ALERT) on many sensors.
    • The output of the LM393 comparator.

    The advantage is that you can connect several open-drain outputs to the same line, and any one of them can pull it low.

    I2C needs pull-ups

    The SDA and SCL lines in I2C are open-drain, so they need pull-up resistors (usually 4.7 kΩ), because devices can only pull the line low. Most modules come with these resistors fitted, and if you connect many modules, the resistors end up in parallel and the combined value drops.

    The value is set by two limits:

    • The minimum value: so the chip can pull the line low with at most 3 mA. At 5 V that's about 1.5 kΩ, and at 3.3 V about 1 kΩ.
    • The maximum value: so the line rises fast enough despite the wiring capacitance. At 100 kHz with 200 pF of bus capacitance, about 5.9 kΩ.

    So 4.7 kΩ sits right in the middle. And if you have 4 modules each with 4.7 kΩ, the equivalent is about 1.2 kΩ, below the limit at 5 V, so remove the resistors from some of them. The I2C pull-up calculator works it out for a given speed and wire length, and bus problems are covered in the I2C troubleshooting article.

    A pull-down on a MOSFET gate

    The gate of a MOSFET is insulated, so if it's left floating (while the microcontroller is still starting up or disconnected), the MOSFET may half turn on and heat up, or the motor may move by itself. A 10 kΩ resistor from gate to ground ensures it stays off until the code decides.

    The ESP32's boot pins

    On pins such as GPIO 0, 2, 12 and 15, their state at power-up decides what the chip does. Ready-made boards have the right pull-ups and pull-downs on them, so if you attach a sensor or resistor that pulls them the other way, the board may not boot. Details are in the getting started with the ESP32 article.

    Common mistakes

    • No pull-up at all: the reading is random, and changes when you bring your hand close.
    • The button to the supply with INPUT_PULLUP: the reading is always HIGH, pressed or not.
    • A very small resistor (say 100 Ω): 50 mA when the button is pressed, and the resistor heats up.
    • Many I2C modules each with its own resistors: the bus becomes too heavy and communication fails.

    FAQ

    Can I enable a pull-up and a pull-down on the same pin at once? No, that makes a voltage divider and the pin sits in the middle, which is the worst case for a digital reading.

    Do unused pins need a pull-up? Not necessarily, but it's better to make them OUTPUT or INPUT_PULLUP rather than leaving them floating, especially in battery projects: a floating pin can draw a small extra current inside the chip.

    Next step

    Even with a pull-up, a mechanical button bounces at the moment it's pressed. The fix is in the buttons and debounce article.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.