ESP32 - Keypad

This tutorial instructs you how to use ESP32 with keypad. In detail, we will learn:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Keypad 3x4 and 4x4 Kit
1×(Alternative) Keypad 3x4
1×(Alternative) Keypad 4x4
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Keypad

Keypad

The keypad is composed of a group of buttons arranged in the matrix (rows and columns). A button represents for a key. There are many types of keypad. The keypad 3x4 (12 keys) and keypad 4x4 (16 keys) is two most common-used on DIY projects.

Keypad Pinout

Keypad pins are categorized into two groups: row and column.

  • The keypad 3x4 has 7 pins:
    • R1, R2, R3, R4: row-pins
    • C1, C2, C3: column-pins
  • The keypad 4x4 has 8 pins:
    • R1, R2, R3, R4: row-pins
    • C1, C2, C3, C4: column-pins
    Keypad Pinout

    How Keypad Works

    See how keypad works

Wiring Diagram between Keypad and ESP32

ESP32 - Keypad 3x4 wiring diagram

  • How to connect ESP32 and keypad 3x4 using breadboard
ESP32 Keypad 3x4 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and 3x4 keypad

ESP32 - Keypad 4x4 wiring diagram

  • How to connect ESP32 and keypad 4x4 using breadboard
ESP32 Keypad 4x4 Wiring Diagram

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and 4x4 keypad

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

Keypad 3x4

#include <Keypad.h> #define ROW_NUM 4 // four rows #define COLUMN_NUM 3 // three columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; byte pin_rows[ROW_NUM] = {18, 5, 17, 16}; // GPIO18, GPIO5, GPIO17, GPIO16 connect to the row pins byte pin_column[COLUMN_NUM] = {4, 0, 2}; // 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); } }

Keypad 4x4

#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); } }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Type keypad on the search box, then look for the keypad library by Mark Stanley, Alexander Brevig
  • Click Install button to install keypad library.
ESP32 keypad library
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • Open Serial Monitor on Arduino IDE
How to open serial monitor on Arduino IDE
  • Press some keys on keypad
  • See the result in Serial Monitor
COM6
Send
1 2 3 4 * #
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Keypad and Password

The keypad is widely used to authenticate someone by password. In this tutorials, a valid password is pre-defined in the code. When an user inputs the password from keypad, it is compared with the pre-defined password:

  • If matched, the access is granted.
  • If not, the access is defined.

To use password with keypad, we specify two special keys:

  • A key to start inputting the password. For example, key "*"
  • A key to finish inputting the password. For example, key "#"

The password inputed from an user will be stored in a string, called the inputted password string. When a key is pressed:

  • If an inputted key is "*", clear the inputted password string to start new password
  • If an inputted key is NOT "*" or "#", add the key to the inputted password string.
  • If an inputted key is "#", compare the inputted password string with the pre-defined password.

Keypad - Password Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-keypad */ #include <Keypad.h> #define ROW_NUM 4 // four rows #define COLUMN_NUM 3 // three columns char keys[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; byte pin_rows[ROW_NUM] = {18, 5, 17, 16}; // GPIO18, GPIO5, GPIO17, GPIO16 connect to the row pins byte pin_column[COLUMN_NUM] = {4, 0, 2}; // GPIO4, GPIO0, GPIO2 connect to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password = "7890"; // change your password here String input_password; void setup() { Serial.begin(9600); input_password.reserve(32); // maximum input characters is 33, change if needed } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); if (key == '*') { input_password = ""; // clear input password } else if (key == '#') { if (password == input_password) { Serial.println("The password is correct, ACCESS GRANTED!"); // DO YOUR WORK HERE } else { Serial.println("The password is incorrect, ACCESS DENIED!"); } input_password = ""; // clear input password } else { input_password += key; // append new character to input password string } } }
  • Run above code
  • Open Serial Monitor on Arduino IDE
How to open serial monitor on Arduino IDE
  • Press "123456" keys and press "#"
  • Press "7890" keys and press "#"
  • See the result on Serial Monitor. It looks like the below:
COM6
Send
The password is incorrect, ACCESS DENIED! The password is correct, ACCESS GRANTED!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES