Button-Controlled LED (Arduino)
Light an LED while a push button is held down. Learn digital input with the built-in pull-up resistor.
Beginner15 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
button_led.ino
// Turn an LED on while a push button is held.
// INPUT_PULLUP means the pin reads HIGH normally and LOW when pressed —
// no external resistor needed.
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // pressed
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Steps
- Wire the button: one leg to D2, the opposite-corner leg to GND.
- Wire the LED on D13 through a 220Ω resistor, cathode to GND.
- Upload the sketch.
- Hold the button — the LED lights while it's pressed.