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.
|Raspberry Pi 4 Model B
|12V Power Adapter
|DC Power Jack
|(Optional) Screw Terminal Adapter for Raspberry Pi
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
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.
This image is created using Fritzing. Click to enlarge image
Raspberry Pi Code for Controlling EM Lock
The code below makes the electromagnetic lock to be locked and unlocked every 2 seconds.
- 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.