Digital inputs · Lesson
12 min read
Buttons and pull-up resistors
Why an unconnected input 'floats', and how INPUT_PULLUP fixes it.
What You Will Learn
- Explain a floating input
- Wire a button with INPUT_PULLUP
- Read inverted logic (pressed = LOW)
An input pin that isn't connected to anything is 'floating': it picks up electrical noise and reads random HIGH/LOW values. A pull-up resistor gently holds it HIGH until something stronger (a button to GND) pulls it LOW.
The Uno has built-in pull-up resistors (about 20–50 kΩ). pinMode(2, INPUT_PULLUP) turns one on — then wire the button between pin 2 and GND.
Arduino C++
pinMode(2, INPUT_PULLUP);
...
if (digitalRead(2) == LOW) {
// pressed!
}Button controls an LEDarduino-uno
Note: Press the button on the canvas (or hold it) and watch the LED and serial output. Then change INPUT_PULLUP to INPUT and see the floating warning.
On the ESP32, GPIO34–39 have no internal pull-ups. Use an external 10 kΩ resistor there.
Check Your Understanding
Test what you learned in this lesson