ESP32 - Solenoid Lock

The Solenoid Lock is also known as the Electric Strike Lock. It can be use to lock/unlock cabinet, drawer, door. This tutorial instructs you how to use ESP32 to control the solenoid lock.

An alternative to the Solenoid Lock is Electromagnetic Lock. You can learn more in ESP32 - Electromagnetic Lock tutorial

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Solenoid Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) 5V Power Adapter
1×(Optional) ESP32 Screw Terminal Adapter
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.

Introduction to Solenoid Lock

Pinout

Solenoid Lock includes two wires:

  • Positive (+) wire (red): needs to be connected to 12V of DC power supply
  • Negative (-) wire (black): needs to be connected to GND of DC power supply
Solenoid Lock Pinout

How It Works

  • When the Solenoid Lock is powered, the lock tongue (strike) pulls back (retract) ⇒ the door is unlocked
  • When the Solenoid Lock is NOT powered, the lock tongue (strike) extends ⇒ the door is locked

※ NOTE THAT:

The solenoid lock usually uses 12V, 24V or 48V power supply. Therefore, we CANNOT connect the solenoid lock directly to ESP32 pin. We have to connect it to ESP32 pin via a relay

If we connect the solenoid lock to a relay (normally open mode):

  • When relay is open, door is locked
  • When relay is closed, door is unlocked

By connecting ESP32 to the relay, we can program for ESP32 to control the solenoid lock. Learn more about relay in ESP32 - Relay tutorial.

Wiring Diagram

ESP32 Solenoid Lock Wiring Diagram

This image is created using Fritzing. Click to enlarge image

ESP32 Code

The below code lock/unlock the door every 5 seconds

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-solenoid-lock */ #define RELAY_PIN 16 // ESP32 pin GIOP16, which connects to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // unlock the door delay(5000); digitalWrite(RELAY_PIN, LOW); // lock the door delay(5000); }

Quick Instructions

  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • See the the lock tongue's state

※ NOTE THAT:

In the above code, we used the delay function. Therefore, we do not need to debouncing for button. However, We still provide the code with debouncing just in case you want to do more tasks without using delay function. See How to use millis() instead of delay()

Video Tutorial

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.

※ OUR MESSAGES