/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-button-led
*/
#define BUTTON_PIN 15 // ESP32 pin GIOP16, which connected to button
#define LED_PIN 18 // ESP32 pin GIOP18, which connected to led
// variables will change:
int button_state = 0; // variable for reading the button status
void setup() {
// initialize the LED pin as an output:
pinMode(LED_PIN, OUTPUT);
// initialize the button pin as an pull-up input:
// the pull-up input pin will be HIGH when the button is open and LOW when the button is pressed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// read the state of the button value:
button_state = digitalRead(BUTTON_PIN);
// control LED according to the state of button
if (button_state == LOW) // if button is pressed
digitalWrite(LED_PIN, HIGH); // turn on LED
else // otherwise, button is not pressing
digitalWrite(LED_PIN, LOW); // turn off LED
}