ESP8266 - Electromagnetic Lock

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

An alternative to the electromagnetic lock is the solenoid lock. Please refer to the ESP8266 - Solenoid Lock tutorial for more information.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

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

The electromagnetic lock is commonly used to secure a door. It is typically operated with a switch, fingerprint reader, RFID/NFC reader, keypad, or application on a PC/mobile device to control access to a door, building gate, etc.

The Electromagnetic Lock Pinout

An electromagnetic lock consists 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 attracted to 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 creating an unlocking action.

※ NOTE THAT:

The electromagnetic lock typically requires 12V, 24V or 48V power supply. Consequently, it CANNOT be connected directly to an ESP8266 pin. A relay must be used for this purpose.

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

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

Connecting ESP8266 to a relay enables us to program it to control the electromagnetic lock. To find out more about relays, please refer to ESP8266 - 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 stationary part). When the door is shut, the two components are in contact.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and electromagnetic lock

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 Code for Controlling EM Lock

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

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-electromagnetic-lock */ #define RELAY_PIN D8 // The ESP8266 pin D8 connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin D8 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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the ESP8266.
  • Bring the armature plate close to the electromagnet.
  • Check out the attraction between the armature plate and the electromagnet.

Video Tutorial

ESP8266 - Button Controls Electromagnetic Lock

Check out the ESP8266 - Button Controls Electromagnetic Lock tutorial.