/*
* 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-toggle-led
*/
#define BUTTON_PIN 16 // ESP32 pin GPIO16, which connected to button
#define LED_PIN 18 // ESP32 pin GPIO18, which connected to led
// variables will change:
int led_state = LOW; // the current state of LED
int button_state; // the current state of button
int last_button_state; // the previous state of button
void setup() {
Serial.begin(9600); // initialize serial
pinMode(BUTTON_PIN, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(LED_PIN, OUTPUT); // set ESP32 pin to output mode
button_state = digitalRead(BUTTON_PIN);
}
void loop() {
last_button_state = button_state; // save the last state
button_state = digitalRead(BUTTON_PIN); // read new state
if (last_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
led_state = !led_state;
// control LED arccoding to the toggled state
digitalWrite(LED_PIN, led_state);
}
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
r1:1
r1:2
led1:A
led1:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r