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:
- A circuit connecting a 3x4 keypad, relay, and solenoid lock to the ESP32 S3
- A sketch that reads a password from the keypad and unlocks the solenoid lock when correct
- A timed unlock variant that holds the lock open for 5 seconds and then re-locks automatically
- A foundation for access-control and smart-lock projects using the ESP32 S3
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.

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:

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:

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.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the components following the wiring diagram shown above.
- Connect the ESP32 S3 board to your PC using a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the correct ESP32 S3 board and COM port from the Tools menu.
- Click the Libraries icon on the left bar of Arduino IDE.
- Search "keypad", then find the Keypad library by Mark Stanley and Alexander Brevig.
- Click Install to install the Keypad library.
- Search for DIYables_Keypad created by DIYables.io and click the Install button.
- Copy the above code and paste it into Arduino IDE.
- Click the Upload button to compile and upload the code to the ESP32 S3.

- Press keys 7124 then press # on the keypad.
- Press keys 1234 then press # on the keypad.
- Open the Serial Monitor and observe the results alongside the physical state of the solenoid lock.
- 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.
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.
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.
- Office cabinet lock: Secure a filing cabinet or server rack with a numeric PIN that only authorised staff know.
- Home door lock: Replace a traditional key lock with a keypad-controlled solenoid bolt on a front or back door.
- Safe deposit box: Add a solenoid lock to a strongbox and require the correct PIN before the lid can be released.
- Locker room system: Assign individual PINs to each locker and use one ESP32 S3 per unit for stand-alone operation.
- Shared workspace access: Change the password remotely by updating the firmware so only current members can enter.
- 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.
- Beginner: Change the pre-defined password in the code to a six-digit PIN and verify that shorter inputs are always rejected.
- Beginner: Add an LED indicator that lights green for a correct password and red for an incorrect one, giving instant visual feedback.
- Intermediate: Implement a lockout mechanism that disables the keypad for 30 seconds after three consecutive incorrect password attempts.
- Intermediate: Store the authorised passwords in EEPROM or flash so they persist across power cycles and can be updated without reflashing the firmware.
- Advanced: Add a web interface over WiFi so an administrator can change passwords and view the access log from a browser.
- Advanced: Integrate an OLED display to show a prompt for password entry, a success message, and a countdown timer during the unlock window.