Sub topic 5
Fixing It with an External Pull-Down Resistor
This chapter assumes you can write a basic sketch with setup() and loop(), and that you know Ohm's law well enough to compute a current from a voltage and a resistance. You should also be able to place parts on a breadboard and drive an LED with digitalWrite. The previous chapter showed that an input pin connected to nothing reads unpredictable values. Here you fix that with an external pull-down resistor and wire a button around it.
The problem a pull-down solves
A push button is a switch with two states. Pressed, it connects its two terminals. Released, it connects nothing. If you wire the button between the input pin and 5V, the pin has a defined voltage only while the button is pressed. While the button is released, the pin is connected to nothing and floats. A floating pin picks up electrical noise from nearby wires, your hand, and mains hum, so digitalRead can return HIGH or LOW at random.
A pull-down resistor gives the pin a path to GND when the button is released. The resistor "pulls" the pin's voltage down to 0 V, so the pin reads LOW. When the button is pressed, it connects the pin directly to 5V, and the pin reads HIGH. The pin now has a defined level in both states.
The circuit
The wiring has three connections:
- Button terminal A goes to the 5V rail.
- Button terminal B goes to digital pin 2.
- A resistor goes from digital pin 2 to GND.
The pin 2 node therefore has exactly three things attached: the button, the resistor, and the pin itself. The pin is an input, so it draws almost no current. That makes the resistor and the button behave as a simple voltage divider with the pin tapping the middle.
What the pin sees in each state
The table lists the voltage at the pin node and the value digitalRead returns.
| Button state | Path to the pin node | Voltage at pin | `digitalRead` |
|---|---|---|---|
| Released | Only the resistor to GND | About 0 V | `LOW` |
| Pressed | Button to 5V, and resistor to GND | About 5 V | `HIGH` |
When the button is pressed, the pin node is connected to 5V through the button, whose resistance is close to zero. The resistor still connects the node to GND, so a small current flows from 5V through the button and the resistor to GND. This is the cost of the pull-down. With a 10 kΩ resistor the current is:
I = V / R = 5 V / 10 000 Ω = 0.5 mAThat is small enough to ignore for a button on USB power. It matters only if the device runs from a battery and the button is held for long periods.
Choosing the resistor value
The value has to satisfy two opposing needs.
- Low enough to beat noise. The resistor must hold the pin firmly at 0 V against stray currents. A very large value, such as 10 MΩ, is weak and lets noise push the pin around. The resistor also forms an RC low-pass filter with the pin's stray capacitance, so a very large value makes the edges slow.
- High enough to waste little current. A very small value, such as 100 Ω, draws 50 mA when the button is pressed. That heats the resistor and wastes power, and it is close to the limit you want for a pin's supply path.
The standard choice is 10 kΩ. Values from 4.7 kΩ to 100 kΩ also work in typical hobby circuits. The AVR pin leakage current is on the order of 1 µA at most, which across 10 kΩ produces only 10 mV. That is far below the roughly 1.5 V the chip needs to see to call something LOW, so the pull-down wins easily.
The sketch
This sketch lights the LED on pin 13 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) == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}Three points are worth noting:
- The pin mode is plain
INPUT. The pull-down is in the circuit, so the sketch does not need to enable anything inside the chip. UsingINPUT_PULLUPhere would fight the external resistor, as explained below. - The logic is direct. HIGH means pressed and LOW means released. This is called active-high logic, and it reads naturally in code.
- The sketch reads the level, not the event. The LED follows the button as long as it is held. Counting presses or toggling needs the techniques from later chapters.
The if and else can be shortened to digitalWrite(ledPin, digitalRead(buttonPin));, because HIGH and LOW are the values 1 and 0. The longer form is clearer while you are learning.
Why not use INPUT_PULLUP with this circuit
INPUT_PULLUP turns on an internal resistor of roughly 20 kΩ to 50 kΩ between the pin and 5V. If you set it while the external pull-down is also wired, the two resistors form a voltage divider with the pin in the middle. With a 10 kΩ pull-down and a 30 kΩ internal pull-up, the released voltage is:
V = 5 V × 10 kΩ / (10 kΩ + 30 kΩ) = 1.25 VThat is below the LOW threshold but close to it, and it varies from chip to chip. A larger pull-down, such as 47 kΩ, would put the pin in the undefined zone. Use one pull method per pin, not both.
Wiring mistakes and their symptoms
Most failures with this circuit come from a short list of errors.
| Mistake | Symptom | Fix |
|---|---|---|
| Resistor omitted | The LED flickers or changes when you move your hand near the wires | Add the resistor from the pin to GND |
| Resistor placed between the button and 5V instead of the pin and GND | The pin still floats when released, so reads are random | Move the resistor to the pin node |
| Button wired from pin directly to GND with a pull-down present | The pin reads LOW both ways, and nothing ever lights | Connect the button to 5V, not GND |
| Button turned 90 degrees on the breadboard | The pin reads HIGH constantly, or LOW constantly | See below |
| Very small resistor, such as 220 Ω | The button works, but the resistor gets warm | Use 10 kΩ |
The four-leg button trap
Common tactile buttons have four legs. Each pair of legs on the same side is connected inside the part, and pressing the button joins the two sides. If you rotate the button on the breadboard so that both of your wires land on the same internally connected pair, the button is shorted permanently. The pin then reads HIGH all the time, whatever you do with the button. If the pin reads constant HIGH, rotate the button a quarter turn and test again. A multimeter on continuity mode between two legs, with the button released, shows which legs are paired.
Checking the circuit without code
You can confirm the wiring before you rely on the sketch. Measure the voltage between pin 2 and GND with a multimeter. It should read near 0 V with the button released and near 5 V while it is pressed. If you have no meter, upload a sketch that prints the reading:
const int buttonPin = 2;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
Serial.println(digitalRead(buttonPin));
delay(200);
}Open the Serial Monitor at 9600 baud. With the button released you should see a steady column of 0. While you hold the button, the column changes to 1. If the released column mixes 0 and 1, the pin is still floating, so recheck the resistor's connection to the pin node and to GND.
Pull-down compared with pull-up
An external pull-up resistor is the mirror image: the resistor goes to 5V and the button goes to GND. Both designs give the pin a defined level. The practical differences are:
- A pull-down gives active-high logic, where pressed is HIGH. A pull-up gives active-low logic, where pressed is LOW.
- A pull-down needs a part you place yourself. The Arduino's internal pull-up needs no extra part, but there is no internal pull-down on an Uno, so a pull-down always means an external resistor on this board.
- Some other boards, including certain ARM-based ones, do offer a configurable internal pull-down. Check the board's documentation before assuming either way.
Because the AVR has the internal pull-up and no internal pull-down, most Arduino projects use the pull-up approach. The pull-down remains useful when you want pressed to read as HIGH, or when you connect a module whose output idles at 0 V.
Summary of the pattern
The pull-down pattern has four rules:
- Connect the resistor from the pin node to GND.
- Connect the button from the pin node to 5V.
- Use
INPUTas the pin mode. - Read HIGH as pressed.
Pressed and released then each produce a stable, defined voltage, and the floating-input problem is gone. The remaining issue is that a mechanical button does not switch cleanly. It produces a burst of rapid transitions, which later chapters on bounce handle in software.
Exercise
You wire a button between pin 2 and 5V, add a 10 kΩ resistor from pin 2 to GND, and set pinMode(2, INPUT_PULLUP). The pin reads about 1.25 V when the button is released, and the readings are unreliable. What is the cause, and what is the fix?
Show answer
The internal pull-up (about 30 kΩ) and the external 10 kΩ pull-down form a voltage divider, which holds the pin near 1.25 V. This is close to the LOW threshold and varies from chip to chip. Change the mode to INPUT so only the external pull-down is active, then read HIGH as pressed.
Exercise
With a 10 kΩ pull-down and a 5V supply, how much current flows through the resistor while the button is held? Would a 1 kΩ resistor be a better choice, and why or why not?
Show answer
I = 5 V / 10 000 Ω = 0.5 mA. A 1 kΩ resistor would draw 5 mA, ten times more, and gain nothing because 10 kΩ already holds the pin firmly at LOW. The larger value saves power, so 10 kΩ is the better choice.