#include <ezButton.h>
#define BUTTON_PIN 22 // ESP32 pin GPIO22 connected to button's pin
#define RELAY_PIN 27 // ESP32 pin GPIO27 connected to relay's pin
ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 22;
// variables will change
int relay_state = LOW; // the current state of the relay
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT); // set Relay pin to output mode
button.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
button.loop(); // MUST call loop() function first
if (button.isPressed()) {
Serial.println("The button is pressed");
relay_state = !relay_state; // Toggle the state of relay
digitalWrite(RELAY_PIN, relay_state);
}
}