Controlling a Solenoid Lock with ESP32

What is a Solenoid Lock?

A solenoid lock is an electromechanical device that uses an electromagnetic force to control the locking mechanism. When powered, the solenoid pulls the bolt in, unlocking the door. It’s widely used in electronic access control systems.

Working Principle of Solenoid Lock with ESP32

The ESP32 sends a signal to a relay module, which then powers the solenoid lock. When the relay is activated, it allows current to flow to the solenoid, unlocking the mechanism. When the relay turns off, the lock returns to its locked state, usually with a spring mechanism.

  • Connect the relay module to the ESP32 and solenoid lock.
  • Upload the code to control the relay using GPIO.
  • Trigger the relay to power the solenoid lock.
  • Use button, password keypad, or wireless trigger (Bluetooth/Wi-Fi) for unlocking.

Formula: Relay Control Logic: If digitalWrite(relayPin, HIGH) → Lock is OPEN If digitalWrite(relayPin, LOW) → Lock is CLOSED

Components Required

  • ESP32 development board
  • Solenoid lock (12V)
  • Relay module (1-channel)
  • 12V DC power supply
  • NPN transistor (if using without relay)
  • Flyback diode (1N4007)
  • Jumper wires
  • Breadboard (optional)

Pin Configuration

  • IN (Relay): Connects to GPIO pin on ESP32 (e.g., GPIO 26)
  • VCC (Relay): Connects to 3.3V/5V on ESP32
  • GND (Relay): Connects to GND on ESP32
  • COM (Relay): Common pin for switching solenoid power
  • NO (Relay): Connects to positive terminal of solenoid

Always use an external power supply for the solenoid (12V) and do not power it directly from ESP32.

Wiring the Solenoid Lock to ESP32

  • Relay IN -> GPIO 26
  • Relay VCC -> 3.3V or 5V
  • Relay GND -> GND
  • Relay NO -> To Solenoid +12V
  • Solenoid GND -> To Power Supply GND

Arduino Code for ESP32 + Solenoid Lock

1#define relayPin 26
2
3void setup() {
4  pinMode(relayPin, OUTPUT);
5  digitalWrite(relayPin, LOW); // Start with lock closed
6  Serial.begin(115200);
7  Serial.println("Solenoid Lock Ready");
8}
9
10void loop() {
11  Serial.println("Unlocking...");
12  digitalWrite(relayPin, HIGH); // Unlock
13  delay(3000);
14  Serial.println("Locking again...");
15  digitalWrite(relayPin, LOW);  // Lock
16  delay(5000);
17}

Code Explanation (Line-by-Line)

  • #define relayPin 26: Defines GPIO 26 as the control pin for the relay.
  • pinMode(relayPin, OUTPUT);: Sets relayPin as output to control the relay.
  • digitalWrite(relayPin, LOW);: Starts with the solenoid in locked state.
  • digitalWrite(relayPin, HIGH);: Energizes relay to open the solenoid lock.
  • delay(3000);: Keeps the lock open for 3 seconds.
  • Serial.println(...);: Prints messages to the Serial Monitor for debugging.

Applications

  • Smart door locking systems
  • RFID/Bluetooth-based door locks
  • Biometric security locks
  • Access control systems
  • Automated cabinets and lockers

Conclusion

Using a solenoid lock with ESP32 enables the creation of smart access control systems. Whether triggered by a button, password, or wirelessly, this setup is ideal for automating doors, lockers, and cabinets in modern IoT security solutions.