Arduino Nano 33 IoT - Keypad 4x4

This guide shows you how to use the Arduino Nano 33 IoT with a 4x4 keypad. In this lesson, you will learn:

Arduino Nano 33 IoT Keypad 4x4

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Keypad 4x4
1×Breadboard
1×Jumper Wires
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 4x4

A 4x4 keypad is a versatile input device with 16 buttons arranged in a 4-row by 4-column grid. It includes numbers (0-9) along with additional keys like *, #, and customizable function keys. Commonly used in microcontroller projects, security systems, and menu navigation, it operates using a matrix scanning method to detect key presses efficiently.

Keypad Pinout

The 4x4 keypad has 8 pins in total, they are split into two groups: rows and columns:

  • Four pins for the rows: R1, R2, R3, R4.
  • Four pins for the columns: C1, C2, C3, C4.
4x4 Keypad Pinout

How Keypad Works

Check out how the keypad works here: https://arduinogetstarted.com/tutorials/arduino-keypad#content_about_keypad

Wiring Diagram between 4x4 Keypad and Arduino Nano 33 IoT

The wiring diagram between Arduino Nano and 33 IoT Keypad

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT - Keypad 4x4 Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-keypad-4x4 */ #include <DIYables_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 33 IoT pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // The Arduino Nano 33 IoT pin connected to the column pins of the keypad DIYables_Keypad keypad = DIYables_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 you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Click the Library Manager icon on the left side of the Arduino IDE.
  • In the search box, type DIYables_Keypad and find the keypad library from DIYables.io.
  • Click the Install button to add the keypad library.
Arduino Nano 33 IoT keypad library
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button on the Arduino IDE to build and send the code to your Arduino Nano 33 IoT board.
  • Open the Serial Monitor in the Arduino IDE.
How to open serial monitor on Arduino IDE
  • Push some buttons on the keypad.
  • Look at the result in the Serial Monitor.
COM6
Send
1 2 3 4 * #
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Keypad and Password

A keypad is commonly used to verify someone's identity using a password. In this tutorial, a valid password is already set in the code. When a user enters the password on the keypad, it is checked against the pre-set password:

  • If it matches, you are allowed access.
  • If it doesn't match, you are not allowed access.

To use a password on the keypad, we choose two special keys.

  • A key that begins entering the password. For example, the "*" key.
  • A key that finishes entering the password. For example, the "#" key.

The password that a user enters will be saved in a text variable called the inputted password string. When a key is pressed:

  • If the key you press is "*", remove everything you typed for the password to start over.
  • If the key you press is not "*" or "#", add that key to the password you are typing.
  • If the key you press is "#", compare the password you typed with the set password.

Keypad - Password Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-keypad-4x4 */ #include <DIYables_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 33 IoT pin connected to the row pins of the keypad const byte pin_column[COLUMN_NUM] = {5, 4, 3}; // The Arduino Nano 33 IoT pin connected to the column pins of the keypad DIYables_Keypad keypad = DIYables_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 the code shown above.
  • Open the Serial Monitor in the Arduino IDE.
How to open serial monitor on Arduino IDE
  • Press the keys "123456" and then press the "#" key.
  • Press the keys "7890" and then press the "#" key.
  • Look at the result on the Serial Monitor. It should look like this:
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!