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

1×Raspberry Pi 4 Model B
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) Screw Terminal Adapter for Raspberry Pi
1×(Optional) Power Adapter for Raspberry Pi 4B
1×(Optional) Plastic Case for Raspberry Pi 4B

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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
electromagnetic lock pinout

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

The wiring diagram between Raspberry Pi and electromagnetic lock

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.

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:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file electromagnetic_lock.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-electromagnetic-lock """ import RPi.GPIO as GPIO import time # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin controlled the electromagnetic lock via the relay module RELAY_PIN = 12 # Set the relay pin as an output pin GPIO.setup(RELAY_PIN, GPIO.OUT) try: # Run the loop function indefinitely while True: # Turn the relay ON (HIGH) to lock the door GPIO.output(RELAY_PIN, GPIO.HIGH) time.sleep(2) # Wait for 2 seconds # Turn the relay OFF (LOW) to unlock the door GPIO.output(RELAY_PIN, GPIO.LOW) time.sleep(2) # Wait for 2 seconds except KeyboardInterrupt: # If the user presses Ctrl+C, clean up the GPIO configuration GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 electromagnetic_lock.py
  • 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.

Video Tutorial

Raspberry Pi - Button Controls Electromagnetic Lock

Check out the Raspberry Pi - Button Controls Electromagnetic Lock tutorial.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!