Sub topic 5
Fixing a Floating Input with an External Pull-Down Resistor
Assume you know the basic Arduino setup() and loop() structure, can use a breadboard, and understand that a resistor limits current. This chapter uses a typical 5 V Arduino board, such as an Uno.
The pull-down idea
A digital input should be connected to a known voltage when the button is not pressed. A pull-down resistor is a resistor that connects the input pin to ground, or 0 V. It makes the input read LOW when the button is open.
The button connects the same input pin to 5 V. When you press the button, the pin reads HIGH.
| Button state | Pin connection | `digitalRead()` result |
|---|---|---|
| Released | Pin connected to ground through resistor | `LOW` |
| Pressed | Pin connected directly to 5 V through button | `HIGH` |
Use a resistor such as 10 kΩ. When the button is pressed, the resistor connects 5 V to ground, so it also limits the current through that path:
I = V / R = 5 V / 10,000 Ω = 0.5 mA
This small current is safe for the circuit and keeps the input voltage firmly defined.
Wiring the circuit
Use digital pin 2 as the input. Connect the parts in these groups:
- Connect one side of the push button to the Arduino's 5V pin.
- Connect the other side of the button to digital pin 2.
- Connect a 10 kΩ resistor from digital pin 2 to GND.
- Connect an LED, with its required series resistor, to pin 13 if your board does not already have a built-in LED.
The resistor is connected from the signal to ground, not in series with the button. The button overrides the pull-down by connecting the signal to 5 V when pressed.
Configuring and reading the pin
Set the pin mode to INPUT. This tells the Arduino not to drive the pin's voltage. The pin only measures the voltage supplied by the button and resistor.
digitalRead(buttonPin) returns either HIGH or LOW. The following sketch turns on the built-in LED while the button is held down.
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);
}
}When the button is released, the pull-down resistor supplies a path to ground. The input voltage is close to 0 V, so the result is LOW. When the button is pressed, the input voltage is close to 5 V, so the result is HIGH.
Common wiring errors
A breadboard button often has two pairs of internally connected legs. If you place it in the wrong direction, pressing it may not change the circuit. Place a typical four-leg button across the breadboard's center gap so pressing it connects the two sides.
Check these symptoms:
- The input always reads
LOW: the button may not connect to 5 V, or the resistor may be connected to the wrong row. - The input always reads
HIGH: pin 2 may be shorted to 5 V, or the resistor may be missing. - The input changes randomly when released: the resistor is not actually connected between pin 2 and GND.
- The LED acts unpredictably: check that Arduino GND, the resistor, and the button share the correct breadboard connections.
An external pull-down is useful when you want pressed to mean HIGH, or when your circuit needs a particular resistor value. Later, an internal pull-up can reduce the number of external parts, but it reverses the logic.
Exercise
With the pull-down circuit, what should the input read when the button is released, and why?
Show answer
It should read LOW because the 10 kΩ resistor connects the input pin to GND while the open button does not connect it to 5 V.