Arduino Nano 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 provides instructions on how to use Arduino Nano ESP32 to control the solenoid lock.
An alternative to the Solenoid Lock is Electromagnetic Lock. You can learn more in Arduino Nano ESP32 - Electromagnetic Lock tutorial
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of 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

How It Works
- When the Solenoid Lock is powered, the lock tongue (strike) is extended ⇒ the door is locked
- When the Solenoid Lock is NOT powered, the lock tongue (strike) is retracted ⇒ the door is unlocked
※ NOTE THAT:
The solenoid lock usually uses 12V, 24V or 48V power supply. Therefore, we CANNOT connect the solenoid lock directly to Arduino Nano ESP32 pin. We have to connect it to Arduino Nano ESP32 pin via a relay
If we connect the solenoid lock to a relay (normally open mode):
- When relay is open, door is unlocked
- When relay is closed, door is locked
By connecting Arduino Nano ESP32 to the relay, we can program for Arduino Nano ESP32 to control the solenoid lock. Learn more about relay in Arduino Nano ESP32 - Relay tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
If you do not use a breadboard, wire it like this:

This image is created using Fritzing. Click to enlarge image
Pushing jumper wires into the headers is quick, but the wires can slip out. In this case, use a Screw Terminal Expansion Board for Arduino Nano. The Arduino Nano ESP32 plugs into it, and each pin gets a screw terminal. Each wire goes to the screw terminal of the same pin, as in the image below.

This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code
The below code lock/unlock the door every 5 seconds
Detailed Instructions
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32 board and its corresponding COM port.
- Copy the above code and paste it to Arduino IDE
- Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
- See the the lock tongue's state
Relay vs MOSFET Module for the Solenoid Lock
This tutorial uses a relay to switch the solenoid lock. A 12V solenoid lock runs on DC, so a MOSFET module can also switch it.
| Relay | MOSFET Module | |
|---|---|---|
| Lock control | lock or unlock | lock or unlock |
| Switching sound | a click every time | no sound |
| Switching time | slow (milliseconds) | very fast |
| Wear | the contacts get old with use | no moving parts to wear |
| Works with | AC and DC locks | DC locks only |
| Flyback diode | most relay modules do not need one | must add one (1N4007 or 1N5819) |
| Isolation | yes | yes, with a PC817 optocoupler |
| Choose it for | a simple, common setup | a silent lock, or a door that opens many times a day |
A solenoid lock is only locked or unlocked, so the MOSFET module does not give extra control here. Its gains are: no click, and nothing wears out. Both turn on when the pin is HIGH, so the code stays the same. Do not keep the lock unlocked for a long time, or it gets hot. This is the same with a relay. The 3.3V pin of the Arduino Nano ESP32 is enough to drive the module.
A solenoid valve works the same way as a solenoid lock. To see how to wire and code one with both a relay and a MOSFET module, see the Arduino Nano ESP32 - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the Arduino Nano ESP32 - MOSFET Module tutorial.