ESP32 - Electromagnetic Lock

The electromagnetic lock (EM lock) is widely used to lock the door. It is usually used with switch, fingerprint reader, RFID/NFC reader, keypad, or application on PC/mobille to control access to door, buiding gate ... The electromagnetic lock is also known as magnetic lock, or maglock.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Electromagnetic Lock
1×12V Power Adapter
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Electromagnetic Lock


Electromagnetic Lock includes two components:

  • One electromagnet with two pins
  • One armature plate
  • Electromagnetic Lock Pinout

How It Works

  • When the electromagnet is powered, a current passing through the electromagnet creates a magnetic flux that causes the armature plate to attract to the electromagnet ⇒ a locking action
  • When the electromagnet is NOT powered, there is no a magnetic flux, armature plate does NOT attract to the electromagnet ⇒ a unlocking action


The electromagnetic lock usually uses 12V, 24V or 48V power suppply. Therefore, we CANNOT connect the electromagnetic lock directly to ESP32 pin. We have to connect it to ESP32 pin via a relay

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

  • When relay is open, door is unlocked
  • When relay is closed, door is locked

By connecting ESP32 to the relay, we can program for ESP32 to control the electromagnetic lock. Learn more about relay in ESP32 - Relay tutorial.

For installation, the armature plate is attached to the door/window (moving part), and the electromagnet is attached to the door frame (fixed part). The two components are in contact when the door is closed

Wiring Diagram

ESP32 Electromagnetic Lock Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 Code

The below code lock/unlock the door every 5 seconds

/* * Created by * * This example code is in the public domain * * Tutorial page: */ // constants won't change const int RELAY_PIN = 27; // ESP32 pin GIOP27 connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 3 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(5000); digitalWrite(RELAY_PIN, LOW); // unlock the door delay(5000); }

Quick Steps

  • 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
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Put the armature plate close to electromagnet.
  • See the attraction between armature plate and electromagnet.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

ESP32 - Button Controls Electromagnetic Lock

See ESP32 - Button Controls Electromagnetic Lock tutorial


Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.


  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere