Sub topic 6
Fixing it with an external pull-up resistor
This chapter assumes you can write a basic sketch with setup() and loop(), and that you know Ohm's law and how to place parts on a breadboard. It also assumes you have seen why a floating input reads random values, and you have wired a button with an external pull-down resistor. Here you build the mirror image of that circuit, the external pull-up, and learn why it behaves the way it does.
The idea in one sentence
A pull-up resistor connects the input pin to the supply voltage (5 V on an Uno) through a resistor, so the pin reads HIGH whenever nothing else drives it. The button then connects the pin to ground. Pressing the button pulls the pin down to LOW.
A pull-down circuit does the opposite. The resistor goes to ground, the button goes to 5 V, and a press produces HIGH.
The wiring
You need one push button, one resistor of about 10 kΩ (brown, black, orange), and jumper wires.
- Connect one end of the resistor to the 5 V rail.
- Connect the other end of the resistor to digital pin 2.
- Connect one leg of the button to the same point as pin 2.
- Connect the opposite leg of the button to GND.
The junction between the resistor and the button is the point the pin reads. The pin itself draws almost no current, so it only observes the voltage there.
How the circuit works
The circuit has two states.
Button released. The button is an open switch, so no current flows through the resistor, apart from the tiny leakage into the pin. On an ATmega328P (the Uno's chip) that leakage is at most about 1 µA. Ohm's law gives the voltage lost across the resistor:
V = I × R = 1 µA × 10 000 Ω = 10 mV
The pin sits at about 4.99 V, which is a solid HIGH.
Button pressed. The button connects the junction directly to GND. The pin voltage drops to nearly 0 V, which is LOW. The resistor now carries current from 5 V to ground:
I = V / R = 5 V / 10 000 Ω = 0.5 mA
That is small enough to ignore, even if you hold the button all day. The resistor is also the reason a press is safe. It limits the current that flows from 5 V to ground through the closed button.
Why the pin always sees a valid level
The ATmega328P at 5 V treats an input below 0.3 × Vcc (1.5 V) as LOW and above 0.6 × Vcc (3.0 V) as HIGH. Anything in between is undefined, and digitalRead may return either value. The pull-up keeps the pin near 5 V when released, and the closed button keeps it near 0 V when pressed. Neither state falls in the undefined band, which is what removes the floating behavior.
The sketch
The wiring inverts the meaning of the pin. HIGH means released and LOW means pressed. This sketch lights the built-in LED while the button is held.
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}Note that the pin mode is plain INPUT. The pull-up is a physical part on your breadboard, so the chip does not need to enable anything.
Naming the inverted logic
Writing == LOW to mean "pressed" is easy to misread a week later. A named constant or helper function puts the inversion in one place.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
const int PRESSED = LOW;
bool isPressed() {
return digitalRead(buttonPin) == PRESSED;
}
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
bool pressed = isPressed();
digitalWrite(ledPin, pressed ? HIGH : LOW);
Serial.println(pressed ? "pressed" : "released");
delay(200);
}If you later change to a pull-down circuit, you edit PRESSED and nothing else. The delay(200) only slows the Serial output so the monitor stays readable. A later chapter replaces it with millis-based timing.
Pull-up versus pull-down
| Property | External pull-up | External pull-down |
|---|---|---|
| Resistor connects pin to | Supply (5 V) | GND |
| Button connects pin to | GND | Supply (5 V) |
| Released reads | HIGH | LOW |
| Pressed reads | LOW | HIGH |
| Logic style | Active-low | Active-high |
| Current while pressed | Vcc / R | Vcc / R |
Both circuits fix the floating input equally well. Pull-up is the more common choice in practice. Many chips include internal pull-up resistors but few include internal pull-downs, and many other devices, such as I²C buses and open-drain outputs, are designed around pull-ups. The next chapter shows how INPUT_PULLUP gives you this circuit without the resistor.
Choosing the resistor value
Two forces set the limits on the value.
- Too small (for example 220 Ω). A press draws 5 V / 220 Ω ≈ 23 mA. This wastes power, which matters on batteries, and it needlessly loads the 5 V rail.
- Too large (for example 1 MΩ). The resistor and the pin's stray capacitance form an RC circuit, so the line rises slowly after release. The weak pull also lets electrical noise from nearby wires push the pin into the undefined band, especially with long wires.
A value from 4.7 kΩ to 47 kΩ works for a short button wire on a breadboard. 10 kΩ is the usual default. For long cables or noisy environments, choose a lower value such as 4.7 kΩ or 2.2 kΩ so the pin is held more firmly.
Common errors
Resistor to the wrong rail
If the resistor goes to GND instead of 5 V, you have built a pull-down while the button still connects to GND. The pin is always LOW and never changes. The LED never responds.
No resistor at all
With the button between pin and GND and no resistor, the pin floats while the button is released. It may read LOW or HIGH at random, and your hand near the wire can change it. This is the original floating-input fault.
Button directly across the supply
Button legs on the same side
Most four-legged tactile buttons have legs connected in pairs. Rotated 90 degrees on the breadboard, the two sides are joined permanently, so the circuit reads as always pressed. If the pin reads LOW constantly, turn the button a quarter turn.
Reading the logic backward
The most common software mistake is writing if (digitalRead(buttonPin) == HIGH) and expecting that to mean pressed. With a pull-up it means released. Serial output makes this visible quickly, so print the value if the LED behaves opposite to what you expect.
Wrong voltage on 3.3 V boards
On a 3.3 V board such as an ESP32 or Arduino Nano 33, connect the resistor to 3.3 V, not 5 V. Applying 5 V to a 3.3 V pin can damage it. The resistor must go to the same supply that powers the chip's logic.
Combining with an internal pull-up
Enabling INPUT_PULLUP while also wiring an external pull-up puts two resistors in parallel, for example about 20 kΩ to 50 kΩ inside the chip with your 10 kΩ outside. The combined resistance is slightly lower than 10 kΩ. This works without harm, but it adds nothing. Choose one source of pull-up. Use INPUT with an external resistor, or INPUT_PULLUP with none.
When to prefer the external resistor
The internal pull-up is convenient, but an external resistor is the better choice when you need a specific value. Cases include:
- Long wires or noisy environments, where a stiffer 2.2 kΩ to 4.7 kΩ pull-up holds the line more reliably than the internal resistor's weak 20 kΩ to 50 kΩ.
- Pins that need a known state during reset or startup, before your sketch runs
pinMode. - Boards or chips whose pins have no internal pull-up.
For a short button on a breadboard, either approach reads correctly.
Exercise
You wire a button between pin 2 and GND, and a 10 kΩ resistor between pin 2 and 5 V. The sketch uses pinMode(2, INPUT) and turns the LED on when digitalRead(2) == HIGH. What does the LED do when you press and hold the button, and how do you fix the sketch so that holding the button lights the LED?
Show answer
The LED turns off while the button is held, and it is on while the button is released, because the pull-up makes the released state HIGH and a press pulls the pin LOW. To fix it, change the condition to digitalRead(2) == LOW, or invert the output with digitalWrite(ledPin, digitalRead(2) == LOW ? HIGH : LOW).
Exercise
With a 10 kΩ pull-up and a pressed button, how much current flows through the resistor, and what happens to that current if you replace the resistor with 100 Ω? Why is the larger value a better design for a battery-powered project?
Show answer
With 10 kΩ the current is 5 V / 10 000 Ω = 0.5 mA. With 100 Ω it is 5 V / 100 Ω = 50 mA, which is a hundred times more. The current only flows while the button is pressed, but a stuck or held button would drain a battery quickly at 50 mA, while 0.5 mA is negligible. The 10 kΩ value still holds the pin firmly enough to read HIGH when released.