Why a button needs a resistor
A button only makes a connection while it's pressed. When it's released, the input pin isn't connected to anything, so it floats. A microcontroller input draws almost no current, so a floating pin is pushed around by stray electric fields from nearby wires, mains cables and even your hand. It can read HIGH, LOW or flicker between them. Code that reads a floating button sees presses that never happened.
The cure is a resistor that gently holds the pin at a known level when the button is released:
- A pull-up connects the pin to the supply. Released, the pin reads HIGH. The button connects the pin to 0 V, so pressed reads LOW.
- A pull-down connects the pin to 0 V. Released, it reads LOW. The button connects the pin to the supply, so pressed reads HIGH.
When the button is pressed, the resistor is what stops it from being a short circuit across the supply. The current is just V ÷ R: 0.5 mA for 10 kΩ on 5 V.
Logic thresholds
An input doesn't switch at one exact voltage. The datasheet guarantees a LOW at or below one voltage and a HIGH at or above another, and between them the reading is undefined. On the Arduino Uno's ATmega328P at 5 V, LOW is guaranteed up to 0.3 × VCC (1.5 V) and HIGH from 0.6 × VCC (3.0 V). On the Raspberry Pi Pico's RP2040 at 3.3 V, the figures are 0.8 V and 2.0 V.
Choosing the value
Too small, and the resistor wastes current every time the button is pressed, which matters on batteries. Too big, and the pull-up becomes so weak that the pin's own leakage current, up to 1 µA in these datasheets, plus any moisture or dirt on the board, can drag the voltage into the undefined zone. Weak pull-ups also pick up more noise on long wires. Something from 4.7 kΩ to 10 kΩ suits most buttons. I²C buses need stronger pull-ups, often 2.2 kΩ to 4.7 kΩ, because they have to charge the bus capacitance quickly.
Internal pull-ups
Both chips have pull-up resistors built in that you switch on in code: pinMode(2, INPUT_PULLUP) on an Arduino, or Pin(15, Pin.IN, Pin.PULL_UP) in MicroPython on the Pico. They save a part, but they're weak (20 to 50 kΩ on the ATmega328P, 50 to 80 kΩ on the RP2040), so for long wires or noisy places an external 4.7 kΩ to 10 kΩ resistor is more reliable. Remember that with a pull-up, pressed reads LOW, so your code checks for LOW or 0.
Buttons also bounce: the contacts chatter for a few milliseconds when pressed, so code usually waits about 20 ms before believing a change. Our guides to pull-up and pull-down resistors and 3.3 V and 5 V logic levels go further.
How this simulation works
The thresholds, internal pull-up ranges and 1 µA maximum leakage come from the Microchip ATmega328P and Raspberry Pi RP2040 datasheets. The leakage is applied in the worst direction, fighting the resistor, so the simulator shows the worst case the datasheet allows. The internal pull-up is taken at the weak end of its range for the voltage, and its full range for the current. A floating pin is shown as unknown, because in a real circuit it really is. The button is treated as a perfect switch with no bounce.