ESP32 S3 - Keypad - Solenoid Lock

This tutorial shows you how to combine a keypad and a solenoid lock with the ESP32 S3 to build a password-controlled entry system. When a user enters the correct password on the keypad, the ESP32 S3 activates the solenoid lock to grant access.

The tutorial also covers a timed unlock variant that turns the solenoid lock on for a set period and then turns it off automatically — without using the delay() function. The code supports multiple passwords so you can register more than one valid entry code.

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 solenoid lock when correct
  3. A timed unlock variant that holds the lock open for 5 seconds and then re-locks automatically
  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×Jumper Wires
1×Solenoid Lock
1×12V Power Adapter
1×Optionally, DC Power Jack
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, 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 or cabinet 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

Do you want the wiring to stay in place? A Screw Terminal Block for ESP32 S3 solves this. Plug the ESP32 S3 into it and screw the wires in. 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

Another choice is a GPIO Breakout Board for ESP32 S3. Each GPIO has a signal pin with 3.3V and GND pins next to it, as shown below:

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 - Turn Solenoid Lock ON if the Password is Correct

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. No additional library beyond the Keypad library is required for this variant.

/* * 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-solenoid-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 = "1234"; // change your password here const String password_2 = "56789"; // change your password here const String password_3 = "901234"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed pinMode(RELAY_PIN, OUTPUT); // initialize pin as an output. digitalWrite(RELAY_PIN, HIGH); // lock the solenoid lock } 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 => unlock"); digitalWrite(RELAY_PIN, LOW); } 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. Press keys 7124 then press # on the keypad.
  2. Press keys 1234 then press # on the keypad.
  3. Open the Serial Monitor and observe the results alongside the physical state of the solenoid lock.
  4. 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:14:02] Key pressed: 7 [2026-06-16 09:14:03] Key pressed: 1 [2026-06-16 09:14:04] Key pressed: 2 [2026-06-16 09:14:04] Key pressed: 4 [2026-06-16 09:14:05] The password is incorrect, try again [2026-06-16 09:14:12] Key pressed: 1 [2026-06-16 09:14:13] Key pressed: 2 [2026-06-16 09:14:13] Key pressed: 3 [2026-06-16 09:14:14] Key pressed: 4 [2026-06-16 09:14:15] The password is correct => unlock
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 solenoid lock is activated; regardless of the result, the input string is then cleared ready for the next attempt.

ESP32 S3 Code - Turn Solenoid Lock ON for a Timed Period

The following code unlocks the solenoid lock for 5 seconds when the correct password is entered, then re-locks automatically. The timed behaviour is implemented using the ezOutput library so the ESP32 S3 remains responsive to new key presses during the unlock window.

/* * 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-solenoid-lock */ #include <Keypad.h> #include <ezOutput.h> #define UNLOCK_TIME 5000 // in milliseconds #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 ); ezOutput relay(RELAY_PIN); const String password_1 = "1234"; // change your password here const String password_2 = "56789"; // change your password here const String password_3 = "901234"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum password size is 32, change if needed relay.high(); // lock the solenoid lock } void loop() { relay.loop(); // MUST call the loop() function first 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, turning ON relay"); relay.high(); // set high before making a low pulse relay.pulse(UNLOCK_TIME); // deactivate the solenoid lock during UNLOCK_TIME duration } 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 } } }

The above code uses the ezOutput library, which makes it easy to manage timed outputs in a non-blocking manner. You can find installation instructions in the ezOutput Library Guide.

Application and Project Ideas

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

  1. Office cabinet lock: Secure a filing cabinet or server rack with a numeric PIN that only authorised staff know.
  2. Home door lock: Replace a traditional key lock with a keypad-controlled solenoid bolt on a front or back door.
  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: Change the password remotely by updating 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 unlock a solenoid lock with a keypad password on 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 30 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: Add a web interface over WiFi so an administrator can change passwords and view the access log from a browser.
  6. Advanced: Integrate an OLED display to show a prompt for password entry, a success message, and a countdown timer during the unlock window.

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