ESP32 S3 - Solenoid Lock

Solenoid locks (also known as electric strike locks) are a popular choice for DIY electronic access control, and the ESP32 S3 makes an excellent controller for these devices. This tutorial walks you through everything you need to wire, program, and operate a solenoid lock with your ESP32 S3 board.

What you'll build:

  1. A working solenoid lock circuit connected to the ESP32 S3 through a relay module
  2. Firmware that automatically locks and unlocks the door on a timed cycle
  3. Clear Serial Monitor feedback showing the current lock state
  4. A foundation you can extend with buttons, RFID, or WiFi control
ESP32 S3 - Solenoid Lock

Alternative option: Need a different lock type? Check out our ESP32 S3 - Electromagnetic Lock tutorial for another approach to electronic access control.

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×Solenoid Lock
1×Relay
1×12V Power Adapter
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

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 Solenoid Lock

A solenoid lock (also called an electric strike lock) is an electromagnetic locking device that uses electrical current to control a mechanical bolt or strike plate. When power is applied, an internal coil generates a magnetic field that pulls or extends the strike into its locked position; when power is removed, a spring returns the strike to the unlocked position. This simple two-state behavior makes solenoid locks straightforward to integrate with microcontrollers like the ESP32 S3.

Key Specifications

Most solenoid locks operate on 12V DC, though 24V and 48V variants exist. The lock has only two connection wires — a positive (red) wire and a negative (black) wire — making wiring uncomplicated. The device locks when powered and unlocks when unpowered, and its response is essentially instantaneous, which suits both automated timing and real-time button-triggered applications.

Pinout

The solenoid lock exposes two wires for connection:

  1. Positive (+) wire (red): Connect to the 12V rail of your DC power supply (routed through the relay)
  2. Negative (-) wire (black): Connect directly to the GND of your DC power supply
Solenoid Lock Pinout

How It Works

When the solenoid lock receives power, the lock tongue (strike) extends outward and the door is locked. When power is removed, the spring mechanism retracts the strike and the door is unlocked.

The relay acts as a switchable gate between the high-voltage power supply and the solenoid lock. In Normally Open mode, the relay is open by default (lock unpowered, door unlocked); when the ESP32 S3 drives the relay signal HIGH, the relay closes, powering the lock and securing the door.

※ NOTE THAT:

Important: The solenoid lock requires 12V, 24V, or 48V power supply. You CANNOT connect it directly to ESP32 S3 pins (which only output 3.3V). You MUST use a relay as an intermediary to control the high-voltage power to the lock.

By connecting the ESP32 S3 to the relay, you can program automated or button-triggered lock control. Learn more about relay basics in our ESP32 S3 - Relay tutorial.

Wiring Diagram

Connect your solenoid lock to the ESP32 S3 through a relay module, keeping the high-voltage solenoid circuit isolated from the low-voltage ESP32 S3 circuit. Both circuits must share a common GND for reliable signal control.

Safety Notes

Always double-check all connections before applying power to the circuit. Ensure that your 12V power supply matches the voltage rating printed on your solenoid lock, and keep high-voltage wiring physically separated from the ESP32 S3 GPIO traces to avoid noise or damage.

Component Pin ESP32 S3 Pin Notes
Relay Signal D7 Control signal from ESP32
Relay VCC 5V Power for relay module
Relay GND GND Ground connection
Solenoid Lock (+) 12V Power Supply (+) Via relay COM and NO
Solenoid Lock (-) 12V Power Supply (-) Direct to power supply GND
12V Power GND ESP32 GND Common ground required
The wiring diagram between ESP32 S3 Solenoid Lock

This image is created using Fritzing. Click to enlarge image

ESP32 S3 Code

The following code automatically locks and unlocks the door every five seconds, giving you a clear demonstration of the solenoid lock mechanism before you adapt it to your own trigger logic. It defines pin D7 as the relay control output, drives that pin HIGH to close the relay (locking the door), waits five seconds, then drives it LOW to open the relay (unlocking the door), and repeats indefinitely. Serial Monitor messages accompany every state change so you can confirm correct operation without physically watching the lock.

/* * 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-solenoid-lock */ #define RELAY_PIN 7 // The ESP32 S3 pin connected to the solenoid 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); // unlock the door delay(5000); digitalWrite(RELAY_PIN, LOW); // lock the door delay(5000); }

Code Explanation

The sketch is intentionally minimal so that the core relay-driving logic is easy to follow and extend. #define RELAY_PIN 7 maps pin D7 to the relay signal wire. Inside setup(), pinMode(RELAY_PIN, OUTPUT) configures that pin as a digital output. In loop(), digitalWrite(RELAY_PIN, HIGH) closes the relay and powers the solenoid lock (locked state), while digitalWrite(RELAY_PIN, LOW) opens the relay and cuts power to the lock (unlocked state). Each state persists for 5 000 ms via delay(5000) before the next transition.

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components: Follow the wiring diagram above carefully, ensuring proper relay and power connections.
  3. Connect ESP32 S3: Plug your ESP32 S3 into your computer using the USB Type-C cable.
  4. Open Arduino IDE: Launch the Arduino IDE on your computer.
  5. Select board and port: Choose ESP32 S3 and its corresponding COM port.
  6. Copy the code: Copy the solenoid lock code provided above.
  7. Upload code: Paste into Arduino IDE and click the Upload button.
  8. Observe operation: Watch the lock tongue extend and retract every 5 seconds.
  9. Pro Tip: Start with longer intervals (10–15 seconds) to clearly observe the lock mechanism and verify proper operation before adjusting the timing for your specific application.

Serial Monitor Output

Open the Serial Monitor at 115200 baud to watch the lock cycle in real time. You should 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] Door locked [2026-01-15 09:00:05] Door unlocked [2026-01-15 09:00:10] Door locked [2026-01-15 09:00:15] Door unlocked
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

ESP32 S3 - Button Controls Solenoid Lock

Want manual control instead of automatic timing? Learn how to add button control to your solenoid lock system in our detailed tutorial: ESP32 S3 - Button Controls Solenoid Lock tutorial.

Button control features:

  • Press button to lock/unlock on demand
  • Debouncing included for reliable operation
  • Combines physical control with ESP32 S3 automation
  • Perfect for practical door access applications

※ NOTE THAT:

About debouncing: In the automatic timing code above, the delay function naturally handles debouncing. For button-controlled applications, debouncing code is essential for reliable operation. Learn more about this technique in our guide: How to use millis() instead of delay()

Project Ideas

The ESP32 S3 solenoid lock combination opens the door (literally) to a wide range of practical access control and automation projects.

  1. Smart cabinet lock: Secure storage with remote unlock via WiFi or Bluetooth.
  2. Password-protected door: Use keypad input to control entry access.
  3. RFID access control: Unlock with authorized RFID cards or tags.
  4. Time-based security: Automatically lock and unlock at scheduled times.
  5. Motion-activated lock: Lock doors automatically when motion is detected nearby.
  6. IoT smart locker: Control multiple locks through a web interface hosted on the ESP32 S3.
  7. Pet door with timer: Allow pet access only during specific hours of the day.

Video Tutorial

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

Challenge Yourself

The timed lock-unlock sketch is a solid starting point, but there is plenty of room to expand the project into something more sophisticated.

  1. Beginner: Modify the code to keep the door locked for 10 seconds and unlocked for 2 seconds.
  2. Beginner: Add an LED indicator that shows lock status (red = locked, green = unlocked).
  3. Intermediate: Create a toggle system where pressing a button locks or unlocks the door alternately.
  4. Intermediate: Add a buzzer that beeps once when locking and twice when unlocking.
  5. Advanced: Build a keypad-based security system with password verification before unlocking.
  6. Advanced: Create a WiFi-enabled lock system controllable from your smartphone app using the ESP32 S3's built-in wireless capabilities.

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