ESP32 MicroPython Solenoid Lock
This tutorial instructs you how use ESP32 with MicroPython and a solenoid lock to lock or unlock doors, cabinets, or drawers. We will learn:
- How to connect a solenoid lock to a ESP32.
- How to program the ESP32 with MicroPython to control the solenoid lock.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 Starter Kit (ESP32 included) | |
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Solenoid Lock
The solenoid lock, also known as the electric strike lock, is used to lock or unlock things like cabinets, drawers, and doors.
Pinout
The solenoid lock has two wires.
- Attach the red wire with the plus (+) sign to the 12V DC power source.
- Attach the black wire with the minus (-) sign to the ground (GND) of the DC power source.

How It Works
- When the solenoid lock receives power, the locking bar extends to lock the door.
- When the solenoid lock is not powered, the locking bar pulls back to unlock the door.
※ NOTE THAT:
The solenoid lock typically needs a 12V, 24V, or 48V power supply. Don't connect the solenoid lock directly to the ESP32 pin; instead, use a relay to connect it to the ESP32.
If the solenoid lock is connected to a relay set to normally open:
- When the relay is open, the door stays unlocked.
- When the relay is closed, the door stays locked.
You can connect an ESP32 to the solenoid lock through the relay and program the ESP32 to control the lock. If you're new to relay or MicroPython programming for the ESP32, I recommend checking out the below tutorial:
Wiring Diagram
- How to connect ESP32 and solenoid lock using breadboard (powered via USB cable)

This image is created using Fritzing. Click to enlarge image
The diagram below shows the same connections without a breadboard:

This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and solenoid lock using breadboard (powered via Vin pin)

This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and solenoid lock using screw terminal block breakout board (powered via USB cable)

- How to connect ESP32 and solenoid lock using screw terminal block breakout board (powered via Vin pin)

ESP32 MicroPython Code
This code will lock and unlock the door every five seconds.
Detailed Instructions
Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Confirm that MicroPython firmware is loaded on your ESP32 board.
- If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
- Connect the ESP32 board to the solenoid lock according to the provided diagram.
- Connect the ESP32 board to your computer with a USB cable.
- Open Thonny IDE on your computer.
- In Thonny IDE, go to Tools Options.
- Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
- Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
- Copy the provided MicroPython code and paste it into Thonny's editor.
- Save the code to your ESP32 by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device.
- Name the file main.py.
- Click the green Run button (or press F5) to execute the script.
- Check the position of the lock tongue.
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 ESP32 pin is HIGH (value(1)), 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.
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 ESP32 MicroPython - Water/Liquid Valve tutorial. The wiring for a solenoid lock is in the ESP32 MicroPython - MOSFET Module tutorial.