Pull-up and pull-down resistors
About this lesson
A button wired to an input pin seems simple, until the pin floats: joined to nothing, it reads 0 or 1 at random. On an Arduino Uno R3, follow pin 2 as a 10 kΩ pull-up holds it HIGH, a press pulls it LOW while 0.5 mA flows, and pinMode(2, INPUT_PULLUP) swaps in the chip's own 20 to 50 kΩ resistor. Then flip it into a pull-down. This builds on the lesson “Digital input and logic levels”; the Circuits lesson “Voltage and current dividers” explains why the pressed pin sits at 0 V, and the Hardware lesson “Transistors: the switch that changed everything” shows what sits behind an input pin.
A pin joined to nothing
A button joins pin 2 to ground (GND, 0 volts). With the button open, the pin is connected to nothing at all. We call it floating, and digitalRead returns 0 or 1 at random.
board Arduino Uno R3 (ATmega328P, 5 V) wiring pin 2 -> button -> GND line 1 pinMode(2, INPUT); open pin joined to nothing: floating read 0, 1, 1, 0, ... at random
Read the lesson step by step
A pin joined to nothing
A button joins pin 2 to ground (GND, 0 volts). With the button open, the pin is connected to nothing at all. We call it floating, and digitalRead returns 0 or 1 at random.
board Arduino Uno R3 (ATmega328P, 5 V) wiring pin 2 -> button -> GND line 1 pinMode(2, INPUT); open pin joined to nothing: floating read 0, 1, 1, 0, ... at randomWhy a floating pin drifts
An input pin draws almost no current: at most 1 µA, a millionth of an amp. Engineers call this high impedance. So stray charge from a nearby wire or your finger is enough to swing its voltage up and down.
leakage at most 1 µA into the pin nothing holds the voltage: it drifts at or below 1.5 V reads LOW (0) at or above 3 V reads HIGH (1) in between not guaranteed either wayAdd a pull-up resistor
Add a 10 kΩ resistor from the pin to 5 V: a pull-up. With the button open almost no current flows, so the resistor drops almost no voltage. The pin sits at 5 V and reads HIGH, 1.
pull-up 10 kΩ from pin 2 to 5 V open current: only the pin's leak drop 1 µA x 10 kΩ = 0.01 V at most pin about 5 V -> HIGH (1)Press: the pin goes LOW
Press the button. The pin is now joined straight to ground, so it sits at 0 V and reads LOW, 0. Current flows from 5 V through the resistor and button to ground: 5 V ÷ 10 kΩ = 0.5 mA.
pressed pin joined to GND: 0 V line 3 int v = digitalRead(2); -> v = LOW (0) current 5 V / 10 kΩ = 0.5 mA the resistor takes all 5 VPressed reads LOW
With a pull-up the logic is upside down: open reads HIGH, pressed reads LOW. Engineers call this negative logic. So the code checks for LOW: line 4 lights the LED on pin 13 while v is LOW.
open v = HIGH (1) LED off pressed v = LOW (0) LED on line 4 digitalWrite(13, v == LOW); negative logic: "on" is the LOW levelWhy not a plain wire?
Why not tie the pin to 5 V with a plain wire? Then pressing the button joins 5 V straight to ground: a short circuit, with a huge current. With a 10 kΩ resistor instead, a press draws a safe 0.5 mA.
plain wire + press 5 V straight to GND a short circuit 10 kΩ + press 0.5 mA, safe open: both hold the pin HIGHChoosing the value
Too small wastes current: 1 kΩ draws 5 mA on every press. Too big is weak: with 1 MΩ, the pin's own 1 µA leak could drop 1 V, and noise gets in. 10 kΩ is the usual choice.
1 kΩ pressed 5 mA open drop up to 0.001 V 10 kΩ pressed 0.5 mA open drop up to 0.01 V 1 MΩ pressed 5 µA open drop up to 1 V usual 10 kΩ for a buttonThe pull-up inside the chip
The ATmega328P has a pull-up built into every pin. pinMode(2, INPUT_PULLUP) switches it on, so the outside resistor can go. It is weaker, 20 to 50 kΩ, so a press draws only 0.1 to 0.25 mA.
line 2 pinMode(2, INPUT_PULLUP); inside 20-50 kΩ from pin 2 to 5 V pressed 5 V / 50 kΩ = 0.1 mA ... 5 V / 20 kΩ = 0.25 mA INPUT (line 1) switches it off again // Arduino Uno R3: a push button from pin 2 to GND void setup() { pinMode(2, INPUT_PULLUP); // 20-50 kΩ to 5 V, inside pinMode(13, OUTPUT); // the on-board LED } void loop() { int v = digitalRead(2); // open: HIGH, pressed: LOW digitalWrite(13, v == LOW); // LED on while pressed }Pull-down: the mirror image
Swap them: button to 5 V, resistor to ground. Now the resistor pulls the pin down to 0 V, and a press reads HIGH. The ATmega328P has no pull-downs inside, but an ESP32 does: INPUT_PULLDOWN.
pull-down 10 kΩ from pin 2 to GND open 0 V -> LOW (0) pressed 5 V -> HIGH (1), 0.5 mA flows positive logic: "on" is the HIGH level ESP32: pinMode(pin, INPUT_PULLDOWN);
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A button joins an input pin to GND, with no resistor anywhere. What does digitalRead return while the button is open?
Open, the pin is joined to nothing. It draws almost no current, so stray charge sets its voltage, and the Arduino reference says digitalRead can then return HIGH or LOW, changing randomly.
02With a 10 kΩ pull-up to 5 V and the button pressed, how much current flows through the resistor?
The resistor has the full 5 V across it, so I = V ÷ R = 5 V ÷ 10 kΩ = 0.5 mA.
03You use pinMode(2, INPUT_PULLUP) with a button to GND. Which line runs code only while the button is pressed?
A pull-up holds the open pin HIGH and a press pulls it to ground, so pressed reads LOW. This upside-down convention is called negative logic.
04Why not tie the pin to 5 V with a plain wire instead of a resistor?
With nothing in between, a press shorts the supply to ground and a huge current flows. The resistor limits it to a small, safe 0.5 mA while still holding the open pin HIGH.
05What does the ATmega328P offer inside the chip for an input pin?
Its datasheet gives every I/O pin a pull-up of 20 to 50 kΩ and no pull-down. Boards such as the ESP32 do have internal pull-downs (INPUT_PULLDOWN).
Glossary
- Floating pin
- An input pin joined to nothing. Its voltage drifts with stray charge, so it reads 0 or 1 at random.
- High impedance
- Drawing almost no current. An input pin takes at most about 1 µA, so it barely affects what it measures.
- Pull-up resistor
- A resistor from a pin to the supply voltage. It holds the pin HIGH until something, like a pressed button, pulls it to ground.
- Pull-down resistor
- A resistor from a pin to ground. It holds the pin LOW until something pulls it up to the supply.
- INPUT_PULLUP
- The Arduino pinMode setting that makes a pin an input and switches on the pull-up inside the chip.
- GND (ground)
- The 0 volt reference of the circuit. Current returns to the supply through it.
- Negative logic
- A convention where the active state is LOW: with a pull-up, a pressed button reads LOW.
- Short circuit
- A path with almost no resistance between the supply and ground, which lets a huge current flow.
Notes and sources
- The figure shows the voltage the pin itself sees. A real multimeter has an input resistance of millions of ohms, so touching a floating pin with it pulls the pin a little and changes the reading.
- 1 µA is the datasheet's worst case for input leakage (at 5.5 V). A real pin usually leaks far less, so with 10 kΩ the open pin sits within 0.01 V of 5 V.
- Between 1.5 V and 3 V (0.3 and 0.6 of the 5 V supply) the datasheet does not promise LOW or HIGH. A floating pin often sits there, which is one reason its readings jump.
- On an ESP32 most pins have both an internal pull-up and pull-down, but GPIO34 to GPIO39 are input only and have neither, so they need an outside resistor.
- The pull-up and the pressed button form a voltage divider: the resistor takes all 5 V and the closed button, almost 0 Ω, takes almost none, so the pin sits at 0 V.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B): §14.2.1 Configuring the Pin, Table 30-1 DC characteristics
- Arduino docs: Digital Pins (source)
- Arduino language reference: digitalRead() (source)
- Arduino language reference: pinMode() (source)
- Arduino example: InputPullupSerial (source)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 8: Switches and LEDs (UT Austin)
- Espressif Arduino-ESP32: GPIO API (INPUT_PULLUP, INPUT_PULLDOWN)
- Espressif ESP-IDF: GPIO for ESP32 (pull-ups, pull-downs, GPIO34-39)
Quiz Unlocks after the last step.