Arduino Nano - Electromagnetic Lock

This tutorial instructs you how to use Arduino Nano to control the electromagnetic lock, which is also known as magnetic lock, maglock, or EM lock.

An alternative for the electromagnetic lock is the solenoid lock. For more information, check out the Arduino Nano - Solenoid Lock tutorial.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

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 is a popular choice for door locking. It is often used in conjunction with a switch, fingerprint reader, RFID/NFC reader, keypad, or PC/mobile application to regulate access to doors, gates, and other entry points.

The Electromagnetic Lock Pinout

An electromagnetic lock comprises 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 results in the armature plate being drawn to the electromagnet, thus creating a locking action.
  • However, when the electromagnet is NOT powered, there is no magnetic flux and the armature plate does NOT attract to the electromagnet, leading to an unlocking action.

※ NOTE THAT:

The electromagnetic lock typically requires 12V, 24V or 48V power supply, so it CANNOT be connected directly to an Arduino Nano pin. A relay must be used to link the lock to the pin.

If we attach the electromagnetic lock to a relay (in the normally open mode):

  • When the relay is not activated, the door will be unlocked
  • When the relay is triggered, the door will be locked

Connecting Arduino Nano to a relay allows us to program it to control an electromagnetic lock. To find out more, take a look at the Arduino Nano - Relay tutorial.

For installation, the armature plate should be affixed to the door/window (the part that moves), and the electromagnet should be attached to the door frame (the part that stays in place). The two components will be in contact when the door is shut.

Wiring Diagram

The wiring diagram between Arduino Nano and electromagnetic lock

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code for Controlling EM Lock

The code below causes the door to be locked and unlocked every 5 seconds.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-electromagnetic-lock */ #define RELAY_PIN 2 // The Arduino Nano pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin A5 as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); // lock the door delay(5000); digitalWrite(RELAY_PIN, LOW); // unlock the door delay(5000); }

Detailed Instructions

  • Copy the above code and open it using the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Place the armature plate near the electromagnet.
  • Check out the attraction between the armature plate and the electromagnet.

Video Tutorial

Arduino Nano - Button Controls Electromagnetic Lock

Check out the tutorial on Arduino Nano - Button Controls Electromagnetic Lock.

※ 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!