int ledPin = 13; // LED connected to digital pin 13
int buttonPin = 2; // Button connected to digital pin 2
int buttonState = 0; // Variable to store button state
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// INPUT_PULLUP uses internal resistor; button reads LOW when pressed
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // Button pressed
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
}