#define BUTTON_PIN 22 // pin GPIO22 connected to the button's pin
#define RELAY_PIN 27 // pin GPIO27 connected to the relay's pin
void setup() {
Serial.begin(9600); // initialize serial port
pinMode(BUTTON_PIN, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(RELAY_PIN, OUTPUT); // set ESP32 pin to output mode
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // read the button state
if (buttonState == LOW) {
Serial.println("The button is being pressed");
digitalWrite(RELAY_PIN, HIGH); // turn on LED
}
else if (buttonState == HIGH) {
Serial.println("The button is unpressed");
digitalWrite(RELAY_PIN, LOW); // turn off LED
}
}