In the previous article, I explained about different display modules which can be used for projects. In this post, we will see about Numpad/keypad modules and how to use keypads for Arduino Projects. We will learn how it works and how to use it with Arduino.
So without wasting any more time, Let’s get started.

To use keypads first you have to understand how the keypad works.

A Keypad is nothing but a buttoned matrix with an n*n number of rows and columns. The Rows are horizontal and Columns are vertical.
In the 4×4 matrix, there are 4 Rows and 4 columns and in 4×3 there are 4 Rows and 3 Columns.

Each button in a row is connected to all the other buttons in the same row. Same with columns.

Pressing a button closes the switch between a column and a row trace, allowing current to flow between a Column pin and a Row pin. This is how Arduino finds which button is pressed.

When NO button is pressed, all row pins are in a LOW state and all column pins are in a HIGH state.

When we press a particular button, the column pulls to low as the current flows through that column.

Now the microcontroller knows which column the button is in. To find the row a microcontroller just have to check each row one by one. So the Arduino will pull each row HIGH, one row at a time.

When the column pin goes HIGH, Arduino knows which button has been pressed.

In the above case “5” was pressed. So we see the only second row and the second column is HIGH.

Now let’s see how to connect the keypad to arduino and program it:

## Connections:

The connections are fairly simple, follow the scheme given below:
R1 = pin 2
R2 = pin 3
R3 = pin 4
R4 = pin 5
where R1…R4 are row pins.

C1 = pin 6
C2 = pin 7
C3 = pin 8
C4 = pin 9
where C1…C4 are column pins.

After the connections are made, move to Arduino IDE to upload the code.

## Coding:

Before you can start coding you have to install a library to use the keypad.

• Sketch >> Include Library >> Manage Libraries.
• In the search bar type “Keypad.h” and scroll down to find “Keypad library by Mark Stanley version 3.1.1”
• Install the library and restart IDE.

Now copy the code below and paste it in the IDE. Upload it to Arduino:-

This code will help you check the working of Keypad, It shows the button pressed on Serial monitor.

``````/*Code for 4x4 keypad*/

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] =
{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2};
byte colPins[COLS] = {9, 8, 7, 6};

void setup()
{
Serial.begin(9600);
}

void loop()
{