Sub topic 6
Fixing a Floating Input with an External Pull-Up Resistor
Prerequisites
This chapter assumes you know the basic Arduino sketch structure, voltage, current, resistance, Ohm’s law, breadboard wiring, and how digitalWrite() controls an LED. A digital input reads one of two logic levels: HIGH or LOW. The input must have a defined voltage when the button is both released and pressed.
The external pull-up circuit
An external pull-up resistor connects the input pin to the positive supply voltage. The button connects the same input pin to ground.
Use a resistor such as 10 kΩ:
| Button state | Pin connection | Reading |
|---|---|---|
| Released | Resistor pulls pin toward 5 V | `HIGH` |
| Pressed | Button connects pin to GND | `LOW` |
When the button is released, the switch is open. The resistor gives the input pin a path to 5 V, so the pin reads HIGH.
When the button is pressed, the switch closes. The pin connects directly to ground and reads LOW. The resistor remains between 5 V and ground, limiting the current to a small value. With a 5 V supply and a 10 kΩ resistor, Ohm’s law gives approximately:
I = V / R = 5 V / 10,000 Ω = 0.5 mA
This is safe for the circuit and keeps the input from floating.
Wiring and reading the pin
Connect one end of the resistor to 5 V and the other end to digital pin 2. Connect one terminal of the button to digital pin 2 and the other terminal to GND. On a four-terminal tactile button, place the button across the breadboard’s center gap. Terminals on the same side are usually already connected internally.
Configure the pin as INPUT, because the external resistor provides the pull-up voltage.
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
Serial.println("pressed");
} else {
digitalWrite(ledPin, LOW);
Serial.println("released");
}
delay(100);
}The test for LOW may look backwards at first. The button is active when it connects the input to ground, so this circuit uses active-low logic. The physical action “pressed” corresponds to the electrical value LOW.
Common wiring errors
A missing resistor leaves the input floating while the button is released. The Serial Monitor may then show changing values even when nothing is touching the button.
A button connected between the input and 5 V belongs to a pull-down circuit, not a pull-up circuit. That arrangement reverses the expected readings and can create an unintended short if combined with the wrong resistor placement.
Do not connect the input directly to both 5 V and GND through a closed switch. The resistor must limit the current in the pressed state.
External pull-ups are useful when you need a particular resistor value, a stronger or weaker pull, or compatibility with circuits outside the Arduino. For a simple button, the Arduino’s internal pull-up can often replace the external resistor, but the active-low behavior remains the same.
Exercise
With an external pull-up resistor, what value does digitalRead(buttonPin) return while the button is pressed, and why?
Show answer
It returns LOW because the pressed button connects the input pin to ground.