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 instructs you how to use ESP32 to control the electromagnetic lock.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
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) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to 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 ESP32 pin. See ESP32 - Relay tutorial.

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

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

Wiring Diagram between Electromagnetic Lock and ES32

ESP32 Electromagnetic Lock Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

The below code lock/unlock the door every 5 seconds

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-electromagnetic-lock */ #define RELAY_PIN 16 // ESP32 pin GPIO16 connected to the electromagnetic lock via the relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin GPIO16 as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); // lock the door delay(4000); digitalWrite(RELAY_PIN, LOW); // unlock the door delay(4000); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to 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

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

ESP32 - Button Controls Electromagnetic Lock

See ESP32 - Button Controls Electromagnetic Lock tutorial

※ OUR MESSAGES