ESP32 - Button Toggle Relay
This tutorial instructs you how to use ESP32 to toggle the a relay's state between ON and OFF when a button is pressed. More specifically:
If the button is pressed, turn on the relay
If the button is pressed again, turn off the relay
The above process is repeated over and over again
The ESP32 codes contains code for a button toggles a relay without and with debouncing
We can extend this tutorial to use button to toggle the state of a solenoid, fan, light bulb, or actuator... by connnecting them to the relay.
Or you can buy the following sensor kits:
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.
We have specific tutorials about relay and button. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more about them at the following links:
This image is created using Fritzing. Click to enlarge image
If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.
#define BUTTON_PIN 22
#define RELAY_PIN 27
int relay_state = LOW;
int button_state;
int last_button_state;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
button_state = digitalRead(BUTTON_PIN);
}
void loop() {
last_button_state = button_state;
button_state = digitalRead(BUTTON_PIN);
if (last_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
relay_state = !relay_state;
digitalWrite(RELAY_PIN, relay_state);
}
}
Do the wiring as above image.
Connect the ESP32 board to your PC via a micro USB cable
Open Arduino IDE on your PC.
Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
Copy the above code and paste it to Arduino IDE.
Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
Keep pressing the button several seconds and then release it.
See the change of relay's state
The above ESP32 code contains line-by-line explanation. Please read the comments in the code!
In the code, relay_state = !relay_state is equivalent to the following code:
if(relay_state == LOW)
relay_state = HIGH;
else
relay_state = LOW;
※ NOTE THAT:
The above code does not have the debounce code for button. Without debouncing for button, the unexpected behaviours can occur. See Why need debounce for button. Debouncing for the button is complicated for beginners. Fortunately, the ezButton library does the button debounce for us.
#include <ezButton.h>
#define BUTTON_PIN 22
#define RELAY_PIN 27
ezButton button(BUTTON_PIN);
int relay_state = LOW;
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
button.setDebounceTime(50);
}
void loop() {
button.loop();
if (button.isPressed()) {
Serial.println("The button is pressed");
relay_state = !relay_state;
digitalWrite(RELAY_PIN, relay_state);
}
}
Install ezButton library. See
How To
Copy the above code and paste it to Arduino IDE.
Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
Press button several times
See the change of relay's state
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.