Overview
A digital input reads HIGH or LOW, but only if something holds it at a defined voltage. A button alone connects the pin to 5 V when pressed and to nothing when released, so the released state "floats" and reads randomly. A pull-down resistor fixes that.
You will need
- Arduino Uno R3
- Momentary pushbutton
- 10 kΩ resistor
- Breadboard and jumper wires
Wiring
| Uno pin | Connects to | Notes |
|---|---|---|
| 5V | Button, one side | |
| 2 | Button, other side | Also goes to the 10 kΩ resistor |
| GND | Other end of the 10 kΩ resistor | Pull-down: pin reads LOW when released |
The sketch
const int BUTTON_PIN = 2;
void setup() {
pinMode(BUTTON_PIN, INPUT); // external pull-down resistor
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
int pressed = digitalRead(BUTTON_PIN); // HIGH while pressed
digitalWrite(LED_BUILTIN, pressed);
}How the code works
With a pull-down the button is active-high: HIGH means pressed. The more common alternative uses the built-in pull-up and no external resistor, which makes the button active-low. That is covered in Pull-ups and debouncing.
What you should observe
- The L LED lights only while the button is held.
- Remove the 10 kΩ resistor and the LED may flicker or light when you merely touch the wires: that is a floating input.