/*
* ESP32 Blink LED with Push Button - Simple Code
* When the button is pressed, the LED blinks rapidly.
*/
#define LED_PIN 18 // GPIO 13 connected to LED's anode
#define BUTTON_PIN 19 // GPIO 12 connected to one side of the button
int buttonState = 0; // Variable to store the button state
void setup() {
// Set the LED pin as an output
pinMode(LED_PIN, OUTPUT);
// Set the button pin as an input with internal pull-up resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// Read the state of the button
// When pressed, the pin reads LOW due to INPUT_PULLUP and grounding
buttonState = digitalRead(BUTTON_PIN);
// Check if the button is pressed (state is LOW)
if (buttonState == LOW) {
// If pressed, blink the LED quickly
digitalWrite(LED_PIN, HIGH); // Turn LED ON
delay(100); // Wait for 100 milliseconds
digitalWrite(LED_PIN, LOW); // Turn LED OFF
delay(100); // Wait for 100 milliseconds
} else {
// If the button is not pressed, turn the LED off
digitalWrite(LED_PIN, LOW);
}
}