Arduino Keypad Tutorial | How to Interface 4×4 Keypad with Arduino

Table of Contents

In this tutorial, we will learn about 4×4 Matrix Keypad and how the Arduino Keypad Interface works. A Keypad is an input device that is used to enter passwords, dial a number, browse through menu and even to control robots.

You might have seen keypad being used at ATMs, Security Systems, Telephones etc. where the users are allowed to input data to the system. Keypads can be used with Microcontrollers and prototyping platforms like Arduino to implement a variety of projects.

So, in this article, I’ll show you how to interface a 4×4 Matrix Keypad with Arduino and how to acquire the data from the keypad.

Overview

A matrix keypad is nothing but a systematic arrangement of buttons in horizontal and vertical fashion. For example, a 4×4 Keypad consists of 16 keys or buttons that are arranged in 4 Rows and 4 Columns. The following image shows a typical Button Type 4×4 Matrix Keypad.

Arduino Keypad Image 1

There are two types of matrix keypads available today. One is the traditional Button type Keypad as shown in the image above while the other is a Membrane type Keypad, which doesn’t contain any buttons but works due to electrical contact between surface of the key and the underlying circuit.

Also read: How to Build a Simple Arduino Calculator?

How Keypads Work?

As mentioned earlier, a 4×4 Matrix Keypad consists of 16 Keys or Buttons arranged in four rows and four columns. The internal circuit of the 4×4 Keypad with all the 16 buttons is shown in the following image.

Arduino Keypad Image 2_1

I’ll explain the working of the 4×4 Matrix Keypad in general without considering any specific microcontroller. This working can be applied to any Microcontroller, even Arduino and Raspberry Pi.

Assume all the rows of the Keypad are made LOW and all the columns of the Keypad are made HIGH by the Microcontroller. When no button or key is pressed, this will be the default status of the rows and columns.

Arduino Keypad Image 3_1

Now, when a key is pressed, the corresponding column will become LOW as the current flows from HIGH Column Pin to LOW Row Pin. The Microcontroller (or Arduino) can easily identify the Column of the Key just by scanning for LOW on Columns.

The trick part comes in identifying the Row of the Key. For this, the Microcontroller should make the Rows of the keypad HIGH, one-by-one and check for the Column Pins to become HIGH. This procedure is continued till the earlier detected Column becomes HIGH.

This way, the microcontroller can determine both the Column and the Row of the Key and hence the Key pressed is identified.

Arduino Keypad Interface

Now, I’ll show you how to interface or connect the 4×4 Keypad with Arduino UNO. A 4×4 Matrix Keypad consists of 8 pins and we need to use 8 pins of Arduino to connect to the keypad. The following image shows the simple circuit of the Arduino Keypad Interface.

Arduino Keypad Image 4_1

In this circuit, I’ve connected the Rows of the keypad to the Digital Pins 0 through 3 of the Arduino i.e. ROW1 to Digital Pin 0, ROW2 to Digital Pin 1, ROW3 to Digital Pin 2 and ROW4 to Digital Pin 3.

Similarly, the Columns of the Keypad are connected to Digital Pins 4 through 7 of Arduino. We will now see a small project using Arduino Keypad Interface.

Circuit Diagram

Arduino Keypad Image 5

Components Required

  • Arduino UNO
  • 16×2 LCD Display
  • 4×4 Matrix Keypad
  • 10KΩ Potentiometer
  • 1KΩ Resistor (1/4 Watt)
  • Breadboard
  • Connecting Wires
  • Power Supply

Circuit Design

The circuit design of the project is very similar to the Arduino Keypad interface shown above. The additional component is the 16×2 LCD Display. The data pins of the LCD are connected to Digital Pins 11 through 8. The E and RS Pins of the 16×2 LCD Display are connected to Pins 12 and 13 of Arduino.

Code

 

#include <Keypad.h>
#include <LiquidCrystal.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{1,2,3,A},
{4,5,6,B},
{7,8,9,C},
{*,0,#,D}
};
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6, 7}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
void setup()
{
lcd.begin(16, 2);
lcd.print(Electronics Hub);
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
}
void loop()
{
char customKey = customKeypad.getKey();
if (customKey)
{
lcd.print(customKey);
}
}

 

Working

The aim of this simple project is to explain how to interface a 4×4 Matrix Keypad to Arduino, how the Arduino Keypad Interface works and how to determine the key pressed on the keypad and display it on the 16×2 LCD Display.

In order to determine the Key pressed on the Keypad, we have use a special library called, well “Keypad”. You should first download this library from this link and place it in Arduino/libraries directory (C:\Program Files (x86)\Arduino\libraries or C:\Program Files\Arduino\libraries).

NOTE: The library “Keypad” was developed by Mark Stanley and Alexander Brevig.

After installing the library, you can copy the above code and upload it to Arduino. In the code, the keys of the 4×4 Matrix keypad are mapped with digits from 0 to 9, symbols * and # and alphabets A, B, C and D.

Arduino Keypad Image 6

So, whenever a key is pressed, Arduino will detect the key using the “Keypad” library and display the same on the 16×2 LCD Display.

Applications

There are a wide range of application of the Arduino Keypad Interface. Some of them are mentioned below.

  • Arduino based Calculator
  • Password based Door Lock System
  • Home Security Systems
  • Home Automation Systems
Facebook
Twitter
LinkedIn
Pinterest

Similar Articles & Blogs

Explore similar articles on various electronics and electrical topics – 

Logic OR Function

The Logic OR Function function states that an output action will become TRUE if either one “OR” more events are TRUE, but the order at which they

Learn More >>

Logic AND Function

In 1854, George Boole performed an investigation into the “laws of thought” which were based around a simplified version of the “group” or “set” theory, and from

Learn More >>

Binary Coded Decimal

As we have seen in this Binary Numbers section of tutorials, there are many different binary codes used in digital and electronic circuits, each with

Learn More >>

Binary Fractions

We know that decimal (or denary) numbers use the base ten (base-10) numbering system where each digit in a decimal number is allowed to take one

Learn More >>