#define BUTTON_PIN 25 // ESP32 pin GPIO25, which connected to button
#define LED_PIN 2 // ESP32 pin GPIO2, which connected to led
// variables will change:
int led_state = LOW; // the current state of LED
int button_state; // the current state of button
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
button_state = digitalRead(BUTTON_PIN);
}
void loop() {
button_state = digitalRead(BUTTON_PIN); // read new state
delay(100);
if (button_state == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
digitalWrite(LED_PIN, HIGH);
delay(50);
}else{
digitalWrite(LED_PIN, LOW);
delay(50);
}
}