ESP32 S3 - Keypad Door Lock

This tutorial shows you how to build a password-controlled door lock system using a keypad and solenoid lock with the ESP32 S3. When the user enters the correct password on the keypad, the ESP32 S3 activates the solenoid lock to unlock the door, keeps it open for 20 seconds, and then automatically relocks it.

The ESP32 S3 code supports multiple passwords, so you can register more than one valid entry code for different users or scenarios.

What you'll build:

  1. A circuit connecting a 3x4 keypad, relay, and solenoid lock to the ESP32 S3
  2. A sketch that reads a password from the keypad and unlocks the door when the correct password is entered
  3. An auto-relock mechanism that secures the door again after 20 seconds
  4. A foundation for access-control and smart-lock projects using the ESP32 S3

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×Alternatively, ESP32 S3 SuperMini Dev Module
1×Alternatively, ESP32 S3 Uno-form Board
1×Alternatively, ESP32 S3 Camera Board
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×Keypad 3x4
1×Relay
1×Solenoid Lock
1×12V Power Adapter
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for ESP32 S3
1×Recommended: Breakout Expansion Board for ESP32 S3
1×Recommended: Power Splitter for ESP32 S3

Or you can buy the following kits:

1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
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 Keypad and Solenoid Lock

A keypad gives users a familiar and secure way to enter numeric or alphanumeric passwords directly into an embedded system without any external device. A solenoid lock converts an electrical signal from the ESP32 S3 (via a relay) into a mechanical bolt movement, making it the ideal actuator for physical access control. Together, they form a self-contained, low-cost door lock that requires no smartphone or network connection.

Key Specifications

We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:

Wiring Diagram

Connect the keypad, relay, and solenoid lock to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, especially on the high-voltage side of the relay that drives the solenoid lock.

The wiring diagram between ESP32 S3 keypad solenoid lock

This image is created using Fritzing. Click to enlarge image

A screw terminal keeps each wire tight, even when the board shakes. Try a Screw Terminal Block for ESP32 S3: the ESP32 S3 sits on it, and each pin gets its own screw terminal. The pins do not change. Just connect each wire to the matching screw terminal:

The wiring diagram between ESP32 S3 keypad solenoid lock Screw Terminal

This image is created using Fritzing. Click to enlarge image

Or plug the ESP32 S3 into a GPIO Breakout Board for ESP32 S3. The same wiring then looks like this:

The wiring diagram between ESP32 S3 keypad solenoid lock GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

The solenoid lock runs on 12 V DC and must be powered through the relay, not directly from the ESP32 S3's GPIO pins. Keep the 12 V wiring physically separated from the low-voltage keypad wiring, and ensure all screw terminals on the relay are tightened before energising the circuit.

Component Pin ESP32 S3 Pin
Keypad Row 1 GPIO17
Keypad Row 2 GPIO16
Keypad Row 3 GPIO15
Keypad Row 4 GPIO7
Keypad Col 1 GPIO6
Keypad Col 2 GPIO5
Keypad Col 3 GPIO4
Relay DC- GND
Relay DC+ 5V
Relay IN GPIO18
Solenoid Lock + Relay NO
Solenoid Lock - 12V GND

ESP32 S3 Code - Door Lock System with Password using Keypad and Solenoid Lock

The following code reads key presses from the keypad on the ESP32 S3, accumulates them into an input string, and activates the solenoid lock via the relay when the entered password matches one of the pre-defined passwords. When unlocked, the door stays open for 20 seconds and then the ESP32 S3 automatically closes the relay to relock it.

/* * 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-keypad-door-lock */ #include <Keypad.h> #define RELAY_PIN 18 // ESP32-S3 pin GPIO18 connected to the relay #define ROW_NUM 4 // keypad four rows #define COLUMN_NUM 3 // keypad three columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; byte pin_rows[ROW_NUM] = {17, 16, 15, 7}; // ESP32-S3 pin: GPIO17, GPIO16, GPIO15, GPIO7 connected to the row pins byte pin_column[COLUMN_NUM] = {6, 5, 4}; // ESP32-S3 pin: GPIO6, GPIO5, GPIO4 connected to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password_1 = "1234567"; // change your password here const String password_2 = "987654"; // change your password here const String password_3 = "55555"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum input characters is 33, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, LOW); // lock the door } void loop() { char key = keypad.getKey(); if (key){ Serial.println(key); if(key == '*') { input_password = ""; // reset the input password } else if(key == '#') { if(input_password == password_1 || input_password == password_2 || input_password == password_3) { Serial.println("The password is correct, unlocking the door in 20 seconds"); digitalWrite(RELAY_PIN, HIGH); // unlock the door for 20 seconds delay(20000); digitalWrite(RELAY_PIN, LOW); // lock the door } else { Serial.println("The password is incorrect, try again"); } input_password = ""; // reset the input password } else { input_password += key; // append new character to input password string } } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components following the wiring diagram shown above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Click the Libraries icon on the left bar of Arduino IDE.
  7. Search "keypad", then find the Keypad library by Mark Stanley and Alexander Brevig.
  8. Click Install to install the Keypad library.
  • Search for DIYables_Keypad created by DIYables.io and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Library Manager
Type:
All
Topic:
All
DIYables_Keypad by DIYables.io
The library is designed for Arduino, ESP32, ESP8266... to use with keypad such as 3x4, 4x4 keypad. It also works with Arduino Uno R4 WiFi/Minima More info
1.0.1
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
ESP32S3 Dev Module on COM15
1
  1. Copy the above code and paste it into Arduino IDE.
  2. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Open the Serial Monitor in Arduino IDE.
  2. Press keys 12345 then press # on the keypad.
  3. Press keys 1234567 then press # on the keypad.
  4. Observe the state of the solenoid lock tongue for 20 seconds.
  5. Pro Tip: Use the * key at any time to clear your current input and start entering the password again from scratch.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 responding to password entries in real time. Correct and incorrect attempts are logged so you can verify access control behaviour at a glance.

∞
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-06-16 09:22:01] Key pressed: 1 [2026-06-16 09:22:02] Key pressed: 2 [2026-06-16 09:22:02] Key pressed: 3 [2026-06-16 09:22:03] Key pressed: 4 [2026-06-16 09:22:03] Key pressed: 5 [2026-06-16 09:22:04] The password is incorrect, try again [2026-06-16 09:22:10] Key pressed: 1 [2026-06-16 09:22:10] Key pressed: 2 [2026-06-16 09:22:11] Key pressed: 3 [2026-06-16 09:22:11] Key pressed: 4 [2026-06-16 09:22:12] Key pressed: 5 [2026-06-16 09:22:12] Key pressed: 6 [2026-06-16 09:22:13] Key pressed: 7 [2026-06-16 09:22:13] The password is correct, unlocking the door in 20 seconds
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Code Explanation

Authorised passwords are pre-defined in the ESP32 S3 code. A string variable called the input string accumulates each key that the user presses. Two special keys on the keypad control the flow: * clears the input string so you can restart entry, and # terminates it and triggers a comparison. When # is pressed, the input string is compared against every pre-defined password — if it matches any of them, the relay is activated to unlock the door for 20 seconds and then the door is automatically locked again; regardless of the result, the input string is cleared ready for the next attempt.

※ NOTE THAT:

  • In the above code, to keep things simple we used the delay() function. For a non-blocking approach, consider using millis() instead. See How to use millis() instead of delay().
  • You can add a piezo buzzer to emit a beep each time a key is pressed on the keypad.
  • In the above code, the door relocks after 20 seconds. You can replace this with a door sensor so the door locks as soon as the sensor detects it is closed.

Application and Project Ideas

Once you master password-controlled door locking with the ESP32 S3, you can extend this pattern into a wide range of access control and security projects.

  1. Home front door: Replace a traditional key lock with a keypad-controlled solenoid bolt and set a family PIN for everyday entry.
  2. Office cabinet lock: Secure a filing cabinet or server rack with a numeric PIN that only authorised staff know.
  3. Safe deposit box: Add a solenoid lock to a strongbox and require the correct PIN before the lid can be released.
  4. Locker room system: Assign individual PINs to each locker and use one ESP32 S3 per unit for stand-alone operation.
  5. Shared workspace access: Update the password by reflashing the firmware so only current members can enter.
  6. Prototype security lab: Build a test rig that logs every access attempt to a serial console or SD card for audit purposes.

Video Tutorial

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

Challenge Yourself

Now that you can build a keypad door lock with the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable projects.

  1. Beginner: Change the pre-defined password in the code to a six-digit PIN and verify that shorter inputs are always rejected.
  2. Beginner: Add an LED indicator that lights green for a correct password and red for an incorrect one, giving instant visual feedback.
  3. Intermediate: Implement a lockout mechanism that disables the keypad for 60 seconds after three consecutive incorrect password attempts.
  4. Intermediate: Store the authorised passwords in EEPROM or flash so they persist across power cycles and can be updated without reflashing the firmware.
  5. Advanced: Replace the fixed 20-second unlock window with a door sensor so the door relocks the moment it is physically closed.
  6. Advanced: Add a web interface over WiFi so an administrator can change passwords and view the full access log from a browser.

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