Logo

ESP32 with Keypad for Password-Based Access Control

What is a Matrix Keypad?

A matrix keypad is an input device used to enter alphanumeric characters or commands. Commonly available in 4x3 or 4x4 configurations, it features rows and columns that form a matrix, allowing detection of key presses using a limited number of I/O pins.

Working of Keypad with ESP32

The matrix keypad is interfaced with the ESP32 using digital GPIO pins. When a key is pressed, it bridges a row and a column. The ESP32 scans through the matrix to detect which key was pressed and compares it to a predefined password. If the password is correct, the system triggers an output (e.g., unlocks a door).

  • Connect the keypad's rows and columns to ESP32 GPIO pins.
  • Define the password in the sketch (e.g., '1234').
  • Read key presses using the Keypad library.
  • Compare input with stored password.
  • If matched, activate an output like LED, buzzer, or relay.
  • If wrong, give feedback (e.g., 'Access Denied').

Formula: Key Mapping = Keypad.getKey() Authentication Logic: If input == storedPassword → Access Granted

Components Required

  • ESP32 Dev Board
  • 4x4 Matrix Keypad
  • Jumper wires
  • Breadboard
  • LED or Relay Module (for output)
  • Buzzer (optional for feedback)
  • Resistors (if needed for pull-up)

Pin Configuration

  • ROW0 - ROW3: Connect to GPIOs (e.g., GPIO 13, 12, 14, 27)
  • COL0 - COL3: Connect to GPIOs (e.g., GPIO 26, 25, 33, 32)
  • Relay/LED: GPIO 4 (output control)

Ensure all keypad connections are reliable. Use external pull-up resistors if ghosting occurs. Always debounce key inputs either via software or capacitor.

Wiring the 4x4 Keypad to ESP32

  • ROW0 -> GPIO 13
  • ROW1 -> GPIO 12
  • ROW2 -> GPIO 14
  • ROW3 -> GPIO 27
  • COL0 -> GPIO 26
  • COL1 -> GPIO 25
  • COL2 -> GPIO 33
  • COL3 -> GPIO 32
  • LED or Relay -> GPIO 4

Arduino Code for ESP32 + 4x4 Keypad

1#include <Keypad.h>
2
3const byte ROWS = 4;
4const byte COLS = 4;
5char keys[ROWS][COLS] = {
6  {'1','2','3','A'},
7  {'4','5','6','B'},
8  {'7','8','9','C'},
9  {'*','0','#','D'}
10};
11
12byte rowPins[ROWS] = {13, 12, 14, 27};
13byte colPins[COLS] = {26, 25, 33, 32};
14
15Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
16
17String password = "1234";
18String input = "";
19
20int relayPin = 4;
21
22void setup() {
23  Serial.begin(115200);
24  pinMode(relayPin, OUTPUT);
25  digitalWrite(relayPin, LOW);
26  Serial.println("Enter 4-digit password:");
27}
28
29void loop() {
30  char key = keypad.getKey();
31  if (key) {
32    Serial.print(key);
33    if (key == '#') {
34      if (input == password) {
35        Serial.println("
36Access Granted");
37        digitalWrite(relayPin, HIGH);
38        delay(3000);
39        digitalWrite(relayPin, LOW);
40      } else {
41        Serial.println("
42Access Denied");
43      }
44      input = "";
45    } else if (key == '*') {
46      input = "";
47      Serial.println("
48Input Cleared");
49    } else {
50      input += key;
51    }
52  }
53}

Code Explanation

  • #include <Keypad.h>: Includes the Keypad library to interface with the 4x4 keypad.
  • char keys[ROWS][COLS]: Defines the keypad layout.
  • keypad.getKey(): Reads the pressed key.
  • String password = "1234": Sets the correct password.
  • if (input == password): Checks if entered password is correct.
  • digitalWrite(relayPin, HIGH);: Turns ON the relay or LED if password is correct.
  • key == '#' or '*': Handles submission or clearing of input.

Applications

  • Electronic Door Locks
  • Secure Keypad Entry Systems
  • Access Control Panels
  • Locker Security
  • Safe Lock Mechanisms
  • Attendance & Voting Systems

Conclusion

Using ESP32 with a 4x4 keypad is a cost-effective and reliable way to build secure password-based access systems. This method offers flexibility for smart door locks and other authentication-based projects where user input is required.