Arduino UNO R4 - Keypad 4x4

This tutorial will teach you how to use an Arduino UNO R4 with a 4x4 keypad. We will cover the following steps:

Arduino UNO R4 4x4 Keypad

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Keypad 4x4
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

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 4x4 Keypad

The keypad has 16 membrane buttons arranged in rows and columns, known as a matrix. Each button is called a key.

Pinout

A 4x4 keypad has 8 pins, divided into two categories: rows and columns.

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

Wiring Diagram

The wiring diagram between Arduino UNO R4 Keypad 4x4

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

#include <DIYables_Keypad.h> // DIYables_Keypad library 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] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad DIYables_Keypad keypad = DIYables_Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); void setup(){ Serial.begin(9600); delay(1000); Serial.println("Keypad 4x4 example"); } void loop(){ char key = keypad.getKey(); if (key){ Serial.println(key); } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the 4x4 keypad to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Go to the Libraries icon on the left side of the Arduino IDE.
  • Type DIYables_Keypad in the search box, and look for the keypad library by DIYables.io.
  • Press the Install button to install the keypad library.
Arduino UNO R4 keypad library
  • Copy the code above and open it with the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino UNO R4.
  • Open the Serial Monitor.
  • Press some keys on the keypad.
  • Check the results in the Serial Monitor.
COM6
Send
1 2 3 4 A B C * #
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Keypad and Password

A common use of a keypad is for entering a password. In this case, we use two special keys:

  • A key to start or restart entering the password. For example, the "*" key.
  • A key to finish entering the password. For example, the "#" key.

The password will be made up of the other keys, excluding the two special keys. When a key is pressed:

  • If the key is not "*" or "#", add the key to the password the user is entering.
  • If the key is "#", check if the entered password matches the set password, then clear the entered password.
  • If the key is "*", clear the entered password.

Keypad - Password Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-keypad-4x4 */ #include <DIYables_Keypad.h> // DIYables_Keypad library 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] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad DIYables_Keypad keypad = DIYables_Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM ); const String password = "1234A"; // change your password here String input_password; void setup(){ Serial.begin(9600); Serial.println("Keypad 4x4 password"); 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 the code provided above.
  • Open the Serial Monitor.
  • Enter the keys "1234BC" and then press "#".
  • Enter the keys "1234A" and then press "#".
  • Check the results 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!