Raspberry Pi - Electromagnetic Lock
This tutorial instructs you how to use Raspberry Pi to control the electromagnetic lock, which is also known as EM lock, magnetic lock, or maglock.
Another alternative to the electromagnetic lock is the solenoid lock. For more information, please refer to the Raspberry Pi - Solenoid 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 Electromagnetic Lock
The Electromagnetic Lock Pinout
An electromagnetic lock is composed of two parts:
- An electromagnet, which has two pins
- An armature plate

How It Works
- When the electromagnet is powered, a current passing through it generates a magnetic flux that causes the armature plate to be drawn towards the electromagnet, resulting in a locking action.
- Conversely, when the electromagnet is not powered, there is no magnetic flux and the armature plate does not attract to the electromagnet, thus leading to an unlocking action.
※ NOTE THAT:
The electromagnetic lock typically requires 12V, 24V or 48V for operation. Therefore, it CANNOT be connected directly to a Raspberry Pi pin. A relay must be used to connect it to the Raspberry Pi pin.
If the electromagnetic lock is linked to a relay (in the normally open mode):
- When the relay is not activated, the door is unlocked
- When the relay is activated, the door is locked
Connecting Raspberry Pi to a relay allows us to program it to control an electromagnetic lock. To find out more about relays, please refer to the Raspberry Pi - Relay tutorial.
For installation, the armature plate is to be affixed to the door/window (the part that moves), and the electromagnet is to be attached to the door frame (the part that stays in place). When the door is closed, the two components will be touching.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Code for Controlling EM Lock
The code below makes the electromagnetic lock to be locked and unlocked every 2 seconds.
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file electromagnetic_lock.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Bring the armature plate close to the electromagnet.
- Check out the attraction between the armature plate and the electromagnet.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
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 Raspberry Pi pin is HIGH (GPIO.output(pin, GPIO.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 −.
No tutorial uses both a relay and a MOSFET module for an electromagnetic lock yet. The closest is the Raspberry Pi - 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 Raspberry Pi - MOSFET Module tutorial.