Using Keypad for Arduino Projects

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.

Understanding The Keypad:

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.

Keypad for Arduino Projects
4*4 keypad

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.

Keypad configuration for Arduino

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

Keypad of arduino function

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:

Requirements:

Connections:

Connection of keypad with Arduino Uno

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.
To download the library, open IDE and goto:-

  • 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*/

#include<keypad.h>

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};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600);
}
  
void loop()
{
  char key = keypad.getKey();
  
  if (key)
  {
    Serial.println(key);
  }
}

Connect Arduino to your PC and upload the code. Next, open the serial monitor and type any numbers, you should see the numbers printed on the serial monitor.

That’s all for this tutorial-using keypad for Arduino Projects. In the next article, we will make a simple Arduino based calculator.