Arduino Nano ESP32 - Electromagnetic Lock

The electromagnetic lock (also known as magnetic lock, maglock, or EM lock) is an important component of the door lock system. This tutorial provides instructions on how to use Arduino Nano ESP32 to control the electromagnetic lock.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Electromagnetic Lock

Electromagnetic Lock Pinout

The electromagnetic Lock is composed of two components:

  • Armature plate: this part is attached on the moving part of the door
  • Electromagnet: this part is attached on the door frame. it has two wires, which are connected to power source.

When the door is closed, two components are in contact with each other.

Electromagnetic Lock Pinout

How Electromagnetic Lock Works

  • When the electromagnet is powered ⇒ the electromagnet attracts to armature plate ⇒ lock
  • When the electromagnet is NOT powered ⇒ the electromagnet does NOT attract to armature plate ⇒ unlock

The electromagnetic lock uses high voltage (12V, 24V or 48V...) power supply. Therefore, we need to use a relay in between the electromagnetic lock and Arduino Nano ESP32 pin. See Arduino Nano ESP32 - Relay tutorial.

If we connect the electromagnetic lock to a relay in normally open mode and use an Arduino Nano ESP32 pin to control the relay:

  • When the Arduino Nano ESP32 pin is LOW ⇒ the relay is open ⇒ door is unlocked
  • When the Arduino Nano ESP32 pin is HIGH ⇒ the relay is closed ⇒ door is locked

Wiring Diagram between Electromagnetic Lock and ES32

The wiring diagram between Arduino Nano ESP32 and Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

The below code lock/unlock the door every 5 seconds

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

Detailed Instructions

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
  • Put the armature plate close to electromagnet.
  • See the attraction between armature plate and electromagnet.

Video Tutorial

Arduino Nano ESP32 - Button Controls Electromagnetic Lock

See Arduino Nano ESP32 - Button Controls Electromagnetic Lock tutorial