Arduino Nano - Keypad

This tutorial instructs you how to connect Arduino Nano to keypad 3x4 and 4x4 and how to program for it. The tutorials also provide the method and Arduino code to verify the the password inputted from keypad by users.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Keypad 3x4 and 4x4 Kit
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter 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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Keypad

Keypad

A keypad is composed of keys arranged in a matrix. Each individual button is referred to as a key. Keypads come in various styles. Two of the most commonly used for DIY projects are the 3x4 keypad (12 keys) and the 4x4 keypad (16 keys).

The Keypad Pinout

The pins are split into two categories: rows and columns.

  • A 3x4 keypad has seven pins: Four of them are row-pins, labeled R1, R2, R3, and R4. The other three are column-pins, labeled C1, C2, and C3.
  • A 4x4 keypad has 8 pins: Four of them are row-pins, labeled R1, R2, R3, and R4. The other four are column-pins, labeled C1, C2, C3, and C4.
Keypad pinout

Wiring Diagram

The wiring diagram between Arduino Nano and Keypad

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

Arduino Nano Code for Keypad 3x4

#include <Keypad.h> const int ROW_NUM = 4; // four rows const int COLUMN_NUM = 3; // three columns const byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} }; 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); } }

Arduino Nano Code for Keypad 4x4

#include <Keypad.h> const int ROW_NUM = 4; // four rows const int COLUMN_NUM = 4; // four columns const byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // The Arduino Nano pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // The Arduino Nano pin connected to the column pins of the keypad char key_layout[ROW_NUM][COLUMN_NUM] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; 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

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “keypad” and locate the keypad library created by Mark Stanley and Alexander Brevig.
  • Then, press the Install button to complete the installation of the keypad library.
Arduino Nano keypad library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Open the Serial Monitor.
  • Press some keys on the keypad.
  • Check the result in the Serial Monitor.
COM6
Send
3 6 9 4 * #
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Keypad and Password

A common use of the keypad is for password entry. Two special keys are used for this purpose:

  • A key to initiate or restart the password input, such as the "*" key
  • A key to end the password input, such as the "#" key

The password will be a string comprised of all the keys, except for two specific special keys.

When a key is pressed:

  • If the key is not "*" or "#", add the key to the user's input password string.
  • If the key is "#", compare the user's input string with the authorized passwords to determine if the input password is correct, then clear the user's input password string.
  • If the key is "*", erase the user's input password string.

Keypad - Password Code

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

Detailed Instructions

  • Run the code above.
  • Open the Serial Monitor.
  • Press the "123456" keys followed by the "#" key.
  • Press the "1234" keys followed by the "#" key.
  • Check out the result on the Serial Monitor.
COM6
Send
password is incorrect, try again password is correct
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!