ESP32 - Button Solenoid Lock
This tutorial instructs you how to use ESP32 with the button and solenoid lock. In detail, we will learn how to use the button to control solenoid lock without debouncing and with debouncing. Each time button is pressed, unlock the door for 10 seconds and then lock the door.
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 solenoid lock 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 prev_button_state;
int button_state;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH);
button_state = digitalRead(BUTTON_PIN);
}
void loop() {
prev_button_state = button_state;
button_state = digitalRead(BUTTON_PIN);
if (prev_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
digitalWrite(RELAY_PIN, LOW);
delay(10000);
digitalWrite(RELAY_PIN, HIGH);
}
}
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
Press button one time.
See the the lock tongue's state
The above ESP32 code contains line-by-line explanation. Please read the comments in the code!
In practice, we need to debounce for the button. The next part will show how to debounce for the button.
Why need to debounce for the button? ⇒ see ESP32 - Button Debounce tutorial
#include <ezButton.h>
#define BUTTON_PIN 22
#define RELAY_PIN 27
ezButton button(BUTTON_PIN);
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
button.setDebounceTime(50);
digitalWrite(RELAY_PIN, HIGH);
}
void loop() {
button.loop();
if (button.isPressed()) {
Serial.println("The button is pressed");
digitalWrite(RELAY_PIN, LOW);
delay(10000);
digitalWrite(RELAY_PIN, HIGH);
}
}
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 one time.
See the the lock tongue's state
※ NOTE THAT:
Since the above code used the delay() function, we do not need to do debouncing for the button. However, We still provide the code with debouncing in case you modify this code and do not use the delay() function
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.