ESP32 - Keypad

The keypad is widely used in many devices such as door lock, ATM, calculator...

In this tutorial, we will learn:

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Keypad 3x4
1×Keypad 4x4 (optional)
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Keypad

Keypad

The keypad is a set of buttons arranged in rows and columns (called matrix). Each button is called key

Keypad has various types. Two popular types for DIY projects are keypad 3x4 (12 keys) and keypad 4x4 (16 keys).

Pinout

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

  • Keypad 3x4 has 7 pins 4 row-pins (R1, R2, R3, R4) and 3 column-pin (C1, C2, C3).
  • Keypad 4x4 has 8 pins 4 row-pins (R1, R2, R3, R4) and 4 column-pin (C1, C2, C3, C4).
  • Keypad Pinout

How It Works

This section is the in-depth knowledge. DON'T worry if you don't understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.

The process of detecting the key pressing is called scanning keypad.

It is called “scanning” because it checks one key by one key.

Row-pins are connected to ESP32's output pins

Column pins are connected to ESP32's input pins (INPUT_PULLUP, in this state, the value of the input pin is HIGH if the key is not pressed).

For each row:

  • Sets all row-pins is HIGH.
  • Sets only the current row-pin to LOW.
  • Reads the state of each column.
    • If a column-pin is HIGH ⇒ key at (row, column) is NOT pressed.
    • If a column-pin is LOW ⇒ key at (row, column) is pressed.
  • Repeats the above process for the next row-pins.

※ NOTE THAT:

The above is one of the methods to scan keypad. We can invert all HIGH to LOW and all LOW to HIGH to scan keypad.

Why does keypad is arranged and connected as a matrix? This makes the scanning process complicated. Why do not use each key as an independent button, then the state of the key is simply determined by reading the state of a button?

⇒ As we know, an independent button requires one ESP32's pin and GND. Let's take keypad 4x4 as an example. If we each key as an independent button, it requires 16 ESP32 pin for 16 keys plus GND pin. If we arranged a connected key in matrix form, we just need to use 8 ESP32's pin, so we can save ESP32's pin. In short, the answer is: to save the ESP32 pins.

Wiring Diagram

  • ESP32 - Keypad 3x4 wiring diagram
  • ESP32 Keypad 3x4 wiring diagram

    Image is developed using Fritzing. Click to enlarge image

  • ESP32 - Keypad 4x4 wiring diagram
  • ESP32 Keypad 4x4 wiring diagram

    Image is developed using Fritzing. Click to enlarge image

How To Program For Keypad

Thanks to Keypad library, using keypad with ESP32 is a piece of cake, no matter whether you understand how the keypad works or not.

ESP32 Code

Keypad 3x4

#include <Keypad.h> const int ROW_NUM = 4; //four rows const int 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}; // GIOP18, GIOP5, GIOP17, GIOP16 connect to the row pins byte pin_column[COLUMN_NUM] = {4, 0, 2}; // GIOP4, GIOP0, GIOP2 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> const int ROW_NUM = 4; //four rows const int 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}; // GIOP19, GIOP18, GIOP5, GIOP17 connect to the row pins byte pin_column[COLUMN_NUM] = {16, 4, 0, 2}; // GIOP16, GIOP4, GIOP0, GIOP2 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 Steps

On Arduino IDE, Go to Tools Manage Libraries

ESP32 add library
  • Search “keypad”, then find 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
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • 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
    3 6 9 4 * #
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Keypad and Password

A popular application of keypad is the password input. In this application, we specify two special keys:

  • A key to start/re-start the password input. For example, key "*"
  • A key to terminate the password input. For example, key "#"

The password will be a string that contains the remaining keys, except for two selected special keys.

When a key is pressed.

  • If the key is NOT neither "*" nor "#", append the key to the user's input password string.
  • If the key is "#", compare the user's input password string with the password to determine the input password is correct or not, and then clear the user's input password string
  • If the key is "*", clear the user's input password string

Keypad - Password Code

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-keypad */ #include <Keypad.h> const int ROW_NUM = 4; //four rows const int 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}; // GIOP18, GIOP5, GIOP17, GIOP16 connect to the row pins byte pin_column[COLUMN_NUM] = {4, 0, 2}; // GIOP4, GIOP0, GIOP2 connect to the column pins Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password = "1234"; // 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("password is correct"); // DO YOUR WORK HERE } else { Serial.println("password is incorrect, try again"); } 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 "1234" keys and press "#"
  • See the result on Serial Monitor. It looks like the below:
  • COM6
    Send
    password is incorrect, try again password is correct
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Challenge Yourself

  • Display the pressed key of the keypad on LCD. Hint: Refer to ESP32 - LCD
  • Make a door lock with password protection using the keypad.

Alternative Keypads

The above code also works with the following keypads:

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere