Write a program to interface Button and LED, so that LED blinks/glow when button is pressed.
Last Updated : 31 Jul 2025
Solution:
const int btnpin = 2; // Button = pin 2
const int ledpin = 12; // LED = pin 12
int buttonstate = 0; // Variable to store button state
void setup(){
pinMode(btnpin, INPUT); // Set button pin as input
pinMode(ledpin, OUTPUT); // Set LED pin as output
}
void loop(){
buttonstate = digitalRead(btnpin); // Read the state of the button
if (buttonstate == HIGH) { // If the button is pressed
digitalWrite(ledpin, HIGH); // Turn on the LED
delay(2000); // Wait for 2 seconds
} else {
digitalWrite(ledpin, LOW); // Turn off the LED
}
}
Report an error
Meanwhile you can watch this video
Watch Video