Introduction: How to Build a Keypad-Controlled LED With an ESP32

About: Part software developer, part maker.

I just built a simple security system using a keypad and an ESP32, where I can turn an LED on and off by entering a secret code.

It's a fantastic beginner project that teaches you the basics of input and output, and you can easily expand it later to control a door lock or a room light with a relay.

I made a full video of the entire build process, which you can watch for a more visual guide. I also used components from an IoT starter kit that was sent to me, and I'll share my honest thoughts on it below.

The video and article are sponsored by Altium Develop!

Supplies

EEmentor IoT Starter Kit - https://shop.eementor.co/product/eementor-iot-esp32-starter-kit/


  1. ESP32 Dev Module - https://s.click.aliexpress.com/e/_c3o6V7Oz
  2. 4×4 Membrane Keypad - https://s.click.aliexpress.com/e/_c43EqllL
  3. LEDs - https://s.click.aliexpress.com/e/_c3eVShzx
  4. Resistors - https://s.click.aliexpress.com/e/_c4bWFn6H
  5. Breadboard Jumper Cables - https://s.click.aliexpress.com/e/_c31PjJkD
  6. Breadboards - https://s.click.aliexpress.com/e/_c3viKyqp
  7. Bench Power Supply - https://s.click.aliexpress.com/e/_c2xpoHD3
  8. Soldering Station - https://s.click.aliexpress.com/e/_c35ymMfb
  9. Multimeter - https://s.click.aliexpress.com/e/_c3UjPXwd

Step 1: Connect the Keypad to the ESP32

The keypad has eight pins—four for the rows and four for the columns.

I connected them directly to digital pins on the ESP32. For the rows, I used pins 19, 18, 5, and 17. For the columns, I used pins 16, 4, 0, and 2.

I found that plugging them in directly worked better than using an expansion board, which sometimes gave me a bad connection.

Step 2: Load the Basic Keypad Test Code

Next, I wrote the code to read the keypad.

I used the Keypad library for Arduino, which makes it easy. The code defines the keypad layout and links the rows and columns to the correct pins.

The main loop just checks for a key press and prints it to the Serial Monitor.

This lets you test that every button works before moving on. You can find the complete code here.

Step 3: Add the LED and Secret Code Logic

Once the keypad was working, I added an LED to pin 22 with a current-limiting resistor.

Then, I upgraded the code. I created a list of allowed secret codes, like "1234" and "5678".

Now, when I type a code and press the '#' key, the code checks if it's correct.

If it is, it toggles the LED on or off. The '*' key works as a clear button if I make a mistake.

The full, complete code is available here.

Step 4: Final Thoughts

After uploading the final code, the system works perfectly. Entering "1234#" turns the LED on, and "5678#" turns it off.

It’s a rewarding project that shows how a few simple components can create an interactive system. For a complete demo and a deeper look at the components I used, please watch my full video on this project.

If you found this guide helpful, I’d really appreciate it if you’d subscribe to my channel for more tutorials like this.

Your support allows me to keep making these projects and reviews!