Arduino Nano ESP32 - Electromagnetic Lock
The electromagnetic lock (also known as magnetic lock, maglock, or EM lock) is an important component of the door lock system. This tutorial provides instructions on how to use Arduino Nano ESP32 to control the electromagnetic lock.
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 Electromagnetic Lock
Electromagnetic Lock Pinout
The electromagnetic Lock is composed of two components:
- Armature plate: this part is attached on the moving part of the door
- Electromagnet: this part is attached on the door frame. it has two wires, which are connected to power source.
When the door is closed, two components are in contact with each other.

How Electromagnetic Lock Works
- When the electromagnet is powered ⇒ the electromagnet attracts to armature plate ⇒ lock
- When the electromagnet is NOT powered ⇒ the electromagnet does NOT attract to armature plate ⇒ unlock
The electromagnetic lock uses high voltage (12V, 24V or 48V...) power supply. Therefore, we need to use a relay in between the electromagnetic lock and Arduino Nano ESP32 pin. See Arduino Nano ESP32 - Relay tutorial.
If we connect the electromagnetic lock to a relay in normally open mode and use an Arduino Nano ESP32 pin to control the relay:
- When the Arduino Nano ESP32 pin is LOW ⇒ the relay is open ⇒ door is unlocked
- When the Arduino Nano ESP32 pin is HIGH ⇒ the relay is closed ⇒ door is locked
Wiring Diagram between Electromagnetic Lock and ES32

This image is created using Fritzing. Click to enlarge image
You can also build this circuit without a breadboard. Here is the same wiring:

This image is created using Fritzing. Click to enlarge image
A screw terminal keeps each wire tight, even when the board shakes. A Screw Terminal Expansion Board for Arduino Nano solves this. Plug the Arduino Nano ESP32 into it and screw the wires in. See the diagram below for the screw terminal version.

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
- If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
- 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
- Put the armature plate close to electromagnet.
- See the attraction between armature plate and electromagnet.
Relay vs MOSFET Module for the Electromagnetic Lock
This tutorial uses a relay to switch the electromagnetic lock. A 12V electromagnetic 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 that stays ON for a long time |
An electromagnetic lock stays ON all the time to keep the door locked. The MOSFET module handles this well: its current is small, so it stays cool. Both turn on when the pin is HIGH, so the code stays the same. The lock has a coil inside, so put the diode across it: the stripe side (cathode) to the lock +, the other side to the lock −. The 3.3V pin of the Arduino Nano ESP32 is enough to drive the module.
No tutorial uses both a relay and a MOSFET module for an electromagnetic lock yet. The closest is the Arduino Nano ESP32 - Water/Liquid Valve tutorial, because a valve also has a coil and is only ON or OFF. For the MOSFET module wiring and safety tips, see the Arduino Nano ESP32 - MOSFET Module tutorial.