Raspberry Pi Pico - Electromagnetic Lock
In this guide, we will learn how to use the Raspberry Pi Pico to control an electromagnetic lock to lock and unlock your door automatically.

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, also known as a maglock, is widely used to lock doors. It usually works with different devices such as switches, fingerprint scanners, RFID/NFC scanners, keypads, or software on computers and mobile devices to control who can enter doors and buildings.
Pinout
An electromagnetic lock consists of two components.
- One electromagnet with two pins
- One armature plate

How It Works
- When the electromagnet is activated, electric current runs through it, creating a magnetic field that attracts the armature plate to the electromagnet, thus locking it. When the electromagnet is deactivated, it stops generating a magnetic field, so the armure plate is not attracted and the lock opens.
※ NOTE THAT:
The electromagnetic lock usually requires a 12V, 24V, or 48V power supply. Therefore, we cannot connect the electromagnetic lock directly to a Raspberry Pi Pico pin. We need to connect it using a relay to the Raspberry Pi Pico pin.
If we connect the electromagnetic lock to a relay that is set to 'normally open':
- When the relay is open, the door can be opened.
- When the relay is closed, the door cannot be opened.
You can link a Raspberry Pi Pico with a relay to control an electromagnetic lock. To understand more about connecting a relay to a Raspberry Pi Pico, visit Raspberry Pi Pico - Relay tutorial.
To install it, attach the armature plate to the door or window (the part that moves) and mount the electromagnet to the door frame (the part that does not move). Ensure that these two parts touch each other when the door is closed.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Raspberry Pi Pico Code
This code makes the door lock and unlock every 5 seconds.
Detailed Instructions
Please follow these instructions step by step:
- Ensure that Thonny IDE is installed on your computer.
- Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
- If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
- Connect the Raspberry Pi Pico to the electromagnetic lock according to the provided diagram.
- Connect the Raspberry Pi Pico to your computer using a USB cable.
- Launch the Thonny IDE on your computer.
- On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
- In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
- Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Raspberry Pi Pico by:
- Click the Save button, or use Ctrl+S keys.
- In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
- Save the file as main.py
- Click the green Run button (or press F5) to run the script. The script will execute.
- Place the armature plate close to the electromagnet.
- Check out how the armature plate is attracted to the electromagnet.
If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.
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 Pico pin is HIGH (value(1)), 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 Pico - 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 Pico - MOSFET Module tutorial.