// Pin Definitions
const int LED_PIN = 2; // GPIO 2 for the LED
const int BUTTON_PIN = 4; // GPIO 4 for the Switch
void setup() {
// Initialize the LED pin as an output
pinMode(LED_PIN, OUTPUT);
// Initialize the Switch pin as an input
// We use INPUT (not INPUT_PULLUP) because you are using an external pull-up resistor
pinMode(BUTTON_PIN, INPUT);
// Ensure the LED starts in the OFF state
digitalWrite(LED_PIN, LOW);
}
void loop() {
// Read the state of the switch
int buttonState = digitalRead(BUTTON_PIN);
// With an external pull-up resistor, the pin reads HIGH by default.
// When the switch is pressed, it connects to GND, pulling the logic to LOW.
if (buttonState == LOW) {
digitalWrite(LED_PIN, HIGH); // Turn the LED ON
delay(2000); // Keep it ON for 2000 milliseconds (2 seconds)
digitalWrite(LED_PIN, LOW); // Turn the LED OFF
// Tiny extra delay to help debounce the switch mechanical bounce
delay(200);
}
}