Circuit Bytes · Episode 72 · Electronics · Digital
"Pull-up and pull-down resistors"
A microcontroller pin reads voltage, high as 1 and low as 0. A button to ground alone leaves the pin floating when it is released, so it reads random 1s and 0s. A pull-up resistor to the supply holds the pin high until the button pulls it to ground, and a pull-down resistor does the opposite.
A pin reads voltage
A digital input on a microcontroller looks at the voltage on its pin and turns it into a number. Near the supply voltage (5 V or 3.3 V) is high, read as 1. Near 0 V (ground) is low, read as 0. (Logic levels were episode 48.) The input itself draws almost no current, which matters in a moment.
The floating problem
Wire a push button from the pin to ground. Pressed, the button connects the pin to ground and it reads 0. But when you let go, the pin is connected to nothing. Because the input draws almost no current, the tiniest stray charge or electrical noise from nearby wires, your hand or the mains can push its voltage up or down. The pin is floating, and it flips between 1 and 0 at random.
The fix: a pull-up resistor
Add a resistor from the pin to the supply. With the button up, the resistor gently holds the pin at the supply voltage, so it reads a steady 1. That's a pull-up resistor. Press the button and the pin connects straight to ground, so it reads 0. This setup is often called active-low: a press reads LOW, so code checks for LOW.
Why a resistor and not a plain wire? A wire from the pin to the supply would connect the supply straight to ground every time the button closed: a short circuit. The resistor limits that current to a small trickle (current = voltage ÷ resistance).
Flip it: a pull-down resistor
Swap the parts: the resistor goes from the pin to ground and the button from the pin to the supply. Now the pin rests at 0, and pressing the button makes it 1. That's a pull-down resistor. Both work; which one you pick depends on the circuit and on what your chip has built in.
Try it yourself
If you have a microcontroller board and a breadboard (episode 67), wire a button from an input pin to ground with no resistor and print the pin's reading. Wave your hand near the loose wire and watch the reading jump around. Then turn on the board's built-in pull-up in code (on Arduino, pinMode(pin, INPUT_PULLUP)) and watch the reading settle on HIGH until you press the button. No board? Look at a schematic of any button circuit online and spot whether its resistor goes to the supply (pull-up) or to ground (pull-down).
Good to know
- A digital input on a microcontroller (CMOS) draws almost no current, so an input with nothing driving it floats and can read high or low unpredictably as it picks up noise and stray charge.
- A pull-up resistor to the supply holds the input at a defined high level; a pull-down resistor to ground holds it low.
- With a pull-up and a button to ground, the pin reads high when the button is up and low when it's pressed (active-low), so code checks for LOW to detect a press.
- Without the resistor, wiring the pin directly to the supply would short the supply to ground when the button closed; the resistor limits that current to V/R.
- Typical external pull-ups are around 4.7 to 10 kilohms. Many microcontrollers have built-in pull-ups that code can switch on (for example Arduino's
INPUT_PULLUP, about 20 to 50 kilohms on the classic Uno's chip). - "Supply" means the logic supply (5 V or 3.3 V).
- Mechanical buttons also bounce for a few milliseconds when pressed, which is a separate problem (debouncing) not covered here.
Answer to the comment question
A 5-kilohm pull-up resistor goes to 5 volts. Press the button. How many milliamps flow? 1 milliamp. With the button pressed, the full 5 V sits across the resistor, so current = voltage ÷ resistance = 5 V ÷ 5,000 Ω = 0.001 A, which is 1 mA. (The input pin itself draws almost nothing.)
Test your knowledge
Quick quiz: "Pull-up and pull-down resistors"
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- SparkFun, "Pull-up Resistors": floating inputs, how a pull-up holds a pin high, and choosing a resistor value
- Arduino documentation, "Digital Input Pull-Up Resistor": a button wired to ground with the built-in pull-up, read as high when released and low when pressed
- Arduino documentation, "Digital Pins": why an input pin with nothing connected reads randomly, and pull-up and pull-down resistors
Video transcript
Why does a button need a resistor to give a clean one or zero?
A microcontroller pin reads voltage. High is one. Low is zero.
Wire a button from the pin to ground. Pressed, it reads zero. Let go, and the pin connects to nothing.
That's called floating. It picks up stray noise and flips between one and zero at random.
The fix: a resistor from the pin to the supply. It gently pulls the pin high. That's a pull-up resistor.
Press the button, and the pin goes to ground: zero. The resistor stops a short circuit.
Swap them, resistor to ground and button to the supply, and it's a pull-down.
A 5-kilohm pull-up goes to 5 volts. Press the button. How many milliamps flow? Comment your answer below!
That's your byte.
More from Circuit Bytes
- EP70c"Electromagnets"
- EP71c"How a speaker makes sound"
- EP73c"Three-way switches"
- EP74c"Surge protectors"