Arduino Nano ESP32 - Keypad

This tutorial provides instructions on how to use Arduino Nano ESP32 with keypad. In detail, we will learn:

Hardware Preparation

1×Arduino Nano ESP32
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 Adapter for Arduino Nano

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 the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 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 Arduino Nano ESP32

The wiring diagram between Arduino Nano ESP32 and Keypad

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code

Keypad 3x4

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-keypad */ #include <Keypad.h> #define ROW_NUM 4 // four rows #define COLUMN_NUM 3 // three columns char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; const byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // The Arduino Nano ESP32 pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3}; // The Arduino Nano ESP32 pin connected to the column pins of the keypad Keypad keypad = Keypad( makeKeymap(key_layout), 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

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-keypad */ #include <Keypad.h> #define ROW_NUM 4 // four rows #define COLUMN_NUM 4 // four columns char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; const byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // The Arduino Nano ESP32 pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // The Arduino Nano ESP32 pin connected to the column pins of the keypad Keypad keypad = Keypad( makeKeymap(key_layout), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); void setup() { Serial.begin(9600); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); } }

Detailed Instructions

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Open the Library Manager by clicking on the Library Manager icon on the left navigation bar of 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.
Arduino Nano ESP32 keypad library
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano 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 Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-keypad */ #include <Keypad.h> #define ROW_NUM 4 // four rows #define COLUMN_NUM 3 // three columns char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; const byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // The Arduino Nano ESP32 pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3}; // The Arduino Nano ESP32 pin connected to the column pins of the keypad Keypad keypad = Keypad( makeKeymap(key_layout), 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 "#"
  • Check out the result on the 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

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!