I want to show you how I used a simple keypad and an ESP32 board to control an LED. This might seem like a small project, but it's actually the first step towards building something much bigger, like a door access system or a way to turn on a main light. The LED is just a handy indicator. You could easily connect a relay in its place to control almost anything you can imagine.
In this guide, I'll walk you through the entire process I followed. I'll show you how I connected all the wires and the code I wrote to make everything work together. We'll start with just getting the keypad to recognize button presses and then build up to a system that only reacts to a secret code.
I used a component kit that was sent to me for this project, and I'll also share my honest thoughts on it as we go. Some parts were great, while others were a bit frustrating.
This video is sponsored by Altium Develop.
Kit Overview and Thoughts
I used an IoT starter kit for this project from EEmentor that was sent to me to review, and I want to share what I found inside. The kit comes with a lot of useful components like a full-size breadboard, a good selection of jumper wires, and many sensors including a keypad, an RFID reader, and relays. I really liked the quality of the breadboard and the pre-cut jumper wires, which save a lot of time when building prototypes.
However, I also noticed some problems with the kit's planning. For example, it includes an ESP32 board with a mini-USB port, but the provided cable is an older style that doesn't fit. There's also a camera module, but the ESP32 board has no connector to plug it into, making the camera unusable with the kit. It feels like some items were just added without thinking about how they would work together.
So, while the kit offers good value for the number of parts you get, be prepared that you might need to buy extra cables or components to use everything. It's a solid starting point, but it has a few frustrating oversights.
Connecting the Keypad
Now, let's get into how I connected the keypad to the ESP32. The keypad module has eight pins on a connector. If you look closely, these eight pins are actually for four rows and four columns of buttons. When you press a button, it connects a specific row to a specific column, and the ESP32 can figure out which key you pressed based on that combination.
I decided to connect all eight pins directly to digital pins on my ESP32 board. I used the pins 19, 18, 5, and 17 for the rows, and pins 16, 4, 0, and 2 for the columns. I plugged the wires in directly, but you could also use a breadboard if you want. At first, I tried using an expansion board from the kit, but I was getting a bad connection, so going directly to the ESP32 worked much more reliably.
Once everything was wired up, the physical connection was complete. The next step was to write the code that would tell the ESP32 how to listen to these rows and columns and identify each key press, which I'll explain in the next part.
The Basic Keypad Code
With the keypad connected, I moved on to the code. I used a special keypad library for the Arduino software, which makes reading the buttons much simpler. In the code, I first defined the layout of my keypad, which is a grid of four rows and four columns, and then I mapped out all the characters like '1', '2', '3', 'A' and so on, just as they appear on the pad.
Then, I told the library which ESP32 pins I had used for the rows and which ones I had used for the columns. The main part of the code is a loop that constantly checks if a key is being pressed. Whenever I press a button, the code figures out which one it is and sends that character to the serial monitor on my computer screen.
This first piece of code is just for testing. It lets me see that every button press is correctly detected, which confirms that all my wiring is correct before I add more features like controlling the LED with a secret code.
The full code is available below.
#include <Keypad.h>
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {19, 18, 5, 17}; // GPIO19, GPIO18, GPIO5, GPIO17 connect to the row pins
byte pin_column[COLUMN_NUM] = {16, 4, 0, 2}; // GPIO16, GPIO4, GPIO0, GPIO2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
Serial.begin(9600);
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.println(key);
}
}
Adding the LED and Security Code
Now I wanted to make the project more useful by adding an LED that only turns on with a secret code. I connected a simple LED to pin 22 on the ESP32, making sure to use a resistor to protect it. In the code, I created a list of allowed codes, like "1234" and "5678", so that the system would only respond to these specific number sequences.
I also added a new part to the code that keeps track of the numbers I'm pressing. When I press the '#' key, it acts like an enter button. The code takes the numbers I've just entered and checks them against the list of allowed codes. If there's a match, it toggles the LED, turning it on if it was off, or off if it was on.
To make it easier to correct mistakes, I programmed the '*' key to work as a clear button. If I start typing a code and mess up, I can press '*' to erase my input and start over.
The full code for the advanced example is below.
#include <Keypad.h>
#define ROW_NUM 4 // four rows
#define COLUMN_NUM 4 // four columns
#define LED_PIN 22 // LED connected to GPIO22
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {19, 18, 5, 17}; // GPIO19, GPIO18, GPIO5, GPIO17 connect to the row pins
byte pin_column[COLUMN_NUM] = {16, 4, 0, 2}; // GPIO16, GPIO4, GPIO0, GPIO2 connect to the column pins
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// Access control variables
const String allowedCodes[] = {"1234", "5678", "9999"}; // List of allowed 4-digit codes
String enteredCode = ""; // Store the entered code
bool ledState = false; // Track LED state (off by default)
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW); // Start with LED off
// Set debounce time to reduce multiple key registrations (in milliseconds)
keypad.setDebounceTime(50); // Adjust this value if needed (default is 10ms)
Serial.println("Keypad Access Control System Ready");
Serial.println("Enter 4-digit code and press # to toggle LED");
Serial.println("Press * to clear entry");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("Key pressed: ");
Serial.println(key);
if (key == '#') {
// Check the entered code
if (enteredCode.length() == 4) {
if (checkCode(enteredCode)) {
// Valid code - toggle LED
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
Serial.print("Access GRANTED! LED is now ");
Serial.println(ledState ? "ON" : "OFF");
} else {
Serial.println("Access DENIED! Invalid code.");
}
} else {
Serial.println("Error: Please enter exactly 4 digits.");
}
enteredCode = ""; // Clear the entered code
}
else if (key == '*') {
// Clear the entered code
enteredCode = "";
Serial.println("Entry cleared.");
}
else {
// Add digit to entered code (limit to 4 digits)
if (enteredCode.length() < 4) {
enteredCode += key;
Serial.print("Entered: ");
for (int i = 0; i < enteredCode.length(); i++) {
Serial.print("*"); // Display asterisks for security
}
Serial.println();
} else {
Serial.println("Max 4 digits reached. Press # to submit or * to clear.");
}
}
}
}
// Function to check if entered code matches any allowed code
bool checkCode(String code) {
int numCodes = sizeof(allowedCodes) / sizeof(allowedCodes[0]);
for (int i = 0; i < numCodes; i++) {
if (code == allowedCodes[i]) {
return true;
}
}
return false;
}
Demo
Now for the fun part, seeing it all work. After uploading the final code, I open the serial monitor to watch the key presses. When I type 1-2-3-4 and then press the '#' key, you can see the message "Access GRANTED!" appear, and the LED immediately turns on. It's a satisfying confirmation that the code was accepted.
To turn the LED off, I can enter another valid code, like 5-6-7-8, followed by '#'. The LED turns off right away. If I make a mistake while entering the numbers, I just press the '*' key to clear my input and start over. It works reliably, and it feels just like using a real access control panel, but built from scratch on my workbench.
Conclusion and Kit Verdict
So, that's how I built a simple keypad-controlled system with an ESP32. You now know how to connect the keypad, write the code to read the buttons, and expand it to control an output like an LED with a secret passcode. This basic setup is a great foundation for more advanced projects, like controlling a door lock or a light with a relay.
As for the starter kit I used, I think it offers good value for the price because of the large number of components you get, especially the nice breadboard and the variety of sensors. But it's important to know that you might face some hurdles, like the incompatible USB cable or the camera module that you can't use with the provided board. You'll likely need to buy a few extra parts to use everything in the box.
Overall, it was a useful kit for this project and for learning. If you're starting out, a kit like this can be a great way to begin, as long as you're aware you might need to supplement it with a couple of your own components to make all the pieces work together.
I hope this guide was helpful and showed you how accessible these electronics projects can be. If you enjoyed following along and want to see me build and review more kits like this one, be sure to subscribe to my channel. I'd also love to hear your thoughts or project ideas down in the comments, and if you're feeling supportive, you can check out the links below to help fuel future projects.
Thanks for reading, and I'll see you in the next one.
Gear and Components Used in the Article
- ESP32 Dev Module - https://s.click.aliexpress.com/e/_c3o6V7Oz
- 4×4 Membrane Keypad - https://s.click.aliexpress.com/e/_c43EqllL
- LEDs - https://s.click.aliexpress.com/e/_c3eVShzx
- Resistors - https://s.click.aliexpress.com/e/_c4bWFn6H
- Breadboard Jumper Cables - https://s.click.aliexpress.com/e/_c31PjJkD
- Breadboards - https://s.click.aliexpress.com/e/_c3viKyqp
- Bench Power Supply - https://s.click.aliexpress.com/e/_c2xpoHD3
- Soldering Station - https://s.click.aliexpress.com/e/_c35ymMfb
- Multimeter - https://s.click.aliexpress.com/e/_c3UjPXwd