ESP32 S3 - Electromagnetic Lock

Learning to control an electromagnetic lock with ESP32 S3 is a great introduction to smart door systems and home security projects. This tutorial shows you how to wire and program a maglock using a relay module.

What you'll build:

  1. A circuit connecting an electromagnetic lock to the ESP32 S3 via a relay module
  2. Code that locks and unlocks the door automatically every 5 seconds
  3. Serial Monitor output showing real-time lock status
  4. A foundation for building button- or RFID-based access control systems
ESP32 S3 - Electromagnetic Lock

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Electromagnetic Lock
1×Relay
1×12V Power Adapter
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

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

An electromagnetic lock (also called a maglock) is a locking device that uses an electromagnet to hold a door securely closed. It consists of two parts — an electromagnet mounted on the door frame and a steel armature plate attached to the door itself — that are held together by magnetic force whenever power is applied. Because there are no mechanical parts, maglocks are durable, fast-acting, and commonly found in commercial access control systems.

Key Specifications

Electromagnetic locks typically operate at 12V, 24V, or 48V DC and can produce a holding force between 280 kg and 600 kg depending on the model. They follow a fail-safe design: when power is removed the magnetic field collapses and the door releases, which is an important safety feature in emergency-exit scenarios. The lock is controlled through a simple two-wire connection to its power supply, making it easy to integrate with a relay module driven by the ESP32 S3.

Electromagnetic Lock Components

The electromagnetic lock is composed of two components:

  • Armature plate: Attached on the moving part of the door (the door itself)
  • Electromagnet: Attached on the door frame with two wires connected to the power source

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

Electromagnetic Lock Pinout

How Electromagnetic Lock Works

  • Powered (Locked): When the electromagnet receives power ⇒ it creates a strong magnetic field ⇒ attracts the armature plate ⇒ door is locked
  • Unpowered (Unlocked): When power is cut ⇒ no magnetic field ⇒ armature plate releases ⇒ door is unlocked

Because the ESP32 S3 outputs only 3.3V at low current, it cannot drive a high-voltage electromagnetic lock directly. A relay module bridges the two systems: the ESP32 S3 controls the relay's low-voltage coil, and the relay switches the lock's 12V circuit on and off. See the ESP32 S3 - Relay tutorial for relay basics.

Control Logic with Relay (Normally Open Mode):

  • ESP32 pin LOW ⇒ relay open ⇒ electromagnet unpowered ⇒ door unlocked
  • ESP32 pin HIGH ⇒ relay closed ⇒ electromagnet powered ⇒ door locked

Wiring Diagram between Electromagnetic Lock and ESP32 S3

Connect the electromagnetic lock to your ESP32 S3 using a relay module as the high-voltage switch. The relay isolates the 12V lock circuit from the ESP32 S3's 3.3V logic, protecting the board from damage.

Safety Notes

Always disconnect power from both the ESP32 S3 and the 12V supply before making any wiring changes. Verify that the relay module's contact rating (voltage and current) matches or exceeds the electromagnetic lock's requirements, and never connect the EM lock's high-voltage wires directly to any ESP32 S3 pin.

The wiring diagram between ESP32 S3 Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

Component Pin ESP32 S3 Pin Notes
Relay Signal (IN) D7 Controls relay on/off
Relay VCC 5V Powers the relay module
Relay GND GND Common ground
Relay COM 12V Power + From power adapter
Relay NO EM Lock Wire 1 Normally open connection
EM Lock Wire 2 12V Power - Completes the circuit

ESP32 S3 Code

The following code demonstrates basic electromagnetic lock control by alternating between locked and unlocked states every 5 seconds. It configures pin D7 as an output, drives it HIGH to engage the relay and lock the door, then pulls it LOW to release the relay and unlock the door, repeating the cycle indefinitely while printing the current status to the Serial Monitor.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-electromagnetic-lock */ #define RELAY_PIN 7 // The ESP32 S3 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Setup Arduino IDE: If this is the first time you use ESP32 S3, see how to setup the environment for ESP32 S3 on Arduino IDE.
  3. Upload the Code: Copy the above code and paste it into Arduino IDE.
  4. Compile and Upload: Click the Upload button on Arduino IDE to compile and upload the code to your ESP32 S3 board.
  5. Test the Lock: Place the armature plate close to the electromagnet.
  6. Observe the Action: Watch the attraction between the armature plate and the electromagnet change every 5 seconds.
  7. Open Serial Monitor: Set baud rate to 115200 to see live lock status messages.
  8. Pro Tip: Mark the armature plate position on your door frame for easy alignment during final installation.

Serial Monitor Output

Open the Serial Monitor at 115200 baud after uploading and you will see output similar to the following:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-01-15 09:00:00] The door is locked [2026-01-15 09:00:05] The door is unlocked [2026-01-15 09:00:10] The door is locked [2026-01-15 09:00:15] The door is unlocked
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Real-World Applications

Electromagnetic locks controlled by the ESP32 S3 suit a wide range of security and automation projects, from simple prototypes to production-ready access systems.

  1. Smart Home Entry: Build keypad or RFID-based door access systems.
  2. Office Access Control: Create employee badge readers with event logging.
  3. Lab or Tutorial Security: Restrict access to sensitive areas using authentication.
  4. Automated Gates: Control gate locks with remote triggers or sensor inputs.
  5. Cabinet Security: Lock storage units or display cases electronically.
  6. IoT Security Systems: Integrate with WiFi for remote lock/unlock via a smartphone app.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

...VIDEO https://www.youtube.com/embed/5yvlkAOr9C4

...VIDEO

Advanced Control: Button-Controlled Lock

For a more practical implementation, see the ESP32 S3 - Button Controls Electromagnetic Lock tutorial to learn how to unlock the door with a button press.

Challenge Yourself

The examples above cover the basics; these challenges will push your ESP32 S3 project further and help you build real access control skills.

  1. Beginner: Add an LED indicator that lights up whenever the door is locked.
  2. Beginner: Modify the timing so the door stays unlocked for 10 seconds instead of 5.
  3. Intermediate: Add a pushbutton that manually unlocks the door on demand.
  4. Intermediate: Implement a simple PIN code system using multiple buttons.
  5. Advanced: Create a web server to control the lock remotely over WiFi.
  6. Advanced: Add RFID authentication for keyless entry with support for multiple user cards.

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