ESP32 C3 Super Mini - Electromagnetic Lock

Learning to control an electromagnetic lock with ESP32 C3 Super Mini 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.

In this tutorial, you'll learn:

ESP32 C3 Super Mini - Electromagnetic Lock

Hardware Preparation

1×ESP32 C3 Super Mini
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 (or maglock) is a locking device that uses electromagnets to hold doors securely closed.

Key Features:

  • Uses magnetic force for locking (no mechanical parts)
  • Requires constant power to stay locked
  • Very strong holding force (typically 280kg to 500kg)
  • Fail-safe design (unlocks when power fails)
  • Commonly used in commercial access control systems

Why It's Great for Beginners:

  • Simple two-wire connection
  • Easy to control with a relay
  • No complex mechanisms or moving parts
  • Works well with ESP32 projects and door automation

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 power source

When the door is closed, 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

Important Notes:

  • Electromagnetic locks require high voltage (12V, 24V, or 48V power supply)
  • ESP32 C3 Super Mini pins output only 3.3V and low current
  • A relay module is required to control the high-voltage power safely

See ESP32 C3 Super Mini - 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

Here's how to connect the electromagnetic lock to your ESP32 C3 Super Mini using a relay module.

Safety Notes:

  • Always disconnect power before wiring
  • Double-check voltage ratings on your electromagnetic lock
  • Ensure the relay is rated for your lock's power requirements
  • Never connect the EM lock directly to ESP32 pins
The wiring diagram between ESP32 C3 Super Mini Electromagnetic Lock

This image is created using Fritzing. Click to enlarge image

Wiring Connections:

Component Pin ESP32 C3 Super Mini 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 C3 Super Mini Code

This code example demonstrates basic electromagnetic lock control by locking and unlocking the door every 5 seconds.

What This Code Does:

  • Initializes D7 as an output pin for relay control
  • Locks the door (HIGH signal) for 5 seconds
  • Unlocks the door (LOW signal) for 5 seconds
  • Repeats the cycle continuously
  • Prints lock status to Serial Monitor
/* * This ESP32 C3 Super Mini code was developed by newbiely.com * * This ESP32 C3 Super Mini code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-c3/esp32-c3-super-mini-electromagnetic-lock */ #define RELAY_PIN D7 // The ESP32 C3 SuperMini 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

  • New to ESP32 C3 Mini? Complete our Getting Started with ESP32 C3 Mini tutorial first to set up your development environment.
  • Setup Arduino IDE: If this is the first time you use ESP32 C3 Super Mini, see how to setup environment for ESP32 C3 Super Mini on Arduino IDE.
  • Upload the Code: Copy the above code and paste it to Arduino IDE.
  • Compile and Upload: Click the Upload button on Arduino IDE to compile and upload code to your ESP32 C3 Super Mini board.
  • Test the Lock: Place the armature plate close to the electromagnet.
  • Observe the Action: Watch the attraction between armature plate and electromagnet change every 5 seconds.
  • Open Serial Monitor: Set baud rate to 9600 to see lock status messages.
  • Pro Tip: Mark the armature plate position on your door frame for easy alignment during installation.

Serial Monitor Output

Open the Serial Monitor and you'll see output like this:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32C3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32C3 Dev Module' on 'COM15')
New Line
9600 baud
The door is locked The door is unlocked The door is locked The door is unlocked The door is locked The door is unlocked
Ln 11, Col 1
ESP32C3 Dev Module on COM15
2

Real-World Applications

Electromagnetic locks controlled by ESP32 C3 Super Mini can be used in many practical projects:

  • Smart Home Entry: Build keypad or RFID-based door access systems
  • Office Access Control: Create employee badge readers with logging
  • Lab or Tutorial Security: Restrict access to sensitive areas with authentication
  • Automated Gates: Control gate locks with remote triggers or sensors
  • Cabinet Security: Lock storage units or display cases electronically
  • IoT Security Systems: Integrate with WiFi for remote lock/unlock via smartphone app

Video Tutorial

Watch the video below for a visual walkthrough of this project.

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

...VIDEO

Advanced Control: Button-Controlled Lock

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

Challenge Yourself

Take your electromagnetic lock project to the next level with these challenges:

  • Easy: Add an LED indicator that lights up when the door is locked
  • Easy: Modify the timing to keep the door unlocked for 10 seconds instead of 5
  • Medium: Add a pushbutton to manually unlock the door on demand
  • Medium: Implement a simple PIN code system using multiple buttons
  • Advanced: Create a web server to control the lock remotely via WiFi
  • Advanced: Add RFID authentication for keyless entry with 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!