Menu Close

Arduino -4×4 Matrix Keypad

This tutorial of Robo India explain how a 4X4 Matrix Keypad (16 Buttons Keyboard) can be attached to Arduino.

1. Introduction:

A step by step illustrated tutorial to explain how to embed a 16 buttons 4X4 Keypad matrix to Arduino. All these buttons are connected to each other in a form of 4X4 matrix in row and column arrangement. This matrix keypad has got 8 pins, 4 for column and 4 for rows. To get it working output is given to either column or row and output is detected. This process is done at a high frequency for all of the columns and rows.

2. Required Hardware

Following Hardware will be required to perform this sketch of shift register.

S.No.ItemQuantity
1.R-Board with FTDI or Arduino Board1
2.4X4 Matrix Keypad1
3.Male to female Jumpers 8

3. Building Circuit

Make following circuit with the help of above mentioned components.

3.1 You may go with Robo India’s R-Board(UNO Compatible)-

or

3.2 You may go with original Arduino UNO Board-

 4. Programming:

Once we are done with circuit part, here is our programme to this circuit. Every command of the following programme is explained in the comment section.

You may download this code (Arduino Sketch) from here.

/ 4x4 Keypad Matrix tutroial
// Robo India | www.roboindia.com

byte h=0,v=0;              // variables used in for loops
const int period=50;       // A little delay to avoid errors.
int kdelay=0;              // Non Blocking Delay
const byte rows=4;         // Rows in Keypad
const byte columns=4;      // Columns in Keypad
const byte Output[rows]={6,7,8,9};   //Row connceted to Arduino Pins
const byte Input[columns]={2,3,4,5}; //Columns connected to Arduino Pins
void setup() 
{
  for(byte i=0;i<rows;i++)      //Loop to declare output pins.
  {
  pinMode(Output[i],OUTPUT);
  }
  for(byte s=0;s<columns;s++)  // Loop to decalre Input pins, Initial Pulledup.
  {
    pinMode(Input[s],INPUT_PULLUP);
  }
  Serial.begin(9600);         // Initializing Serail communication.
}
void loop()
{
  if(millis()-kdelay>period) //used to make non-blocking delay
  {
    kdelay=millis();  //capture time from millis function
switch (keypad())     //Switch to get which button is pressed. 
   {
            case 0:
            Serial.println(1);
       break;  
            case 1:
            Serial.println(2);
       break;
            case 2:
            Serial.println(3);
       break;
            case 3:
            Serial.println("A");
       break;
            case 4:
            Serial.println(4);
       break;
            case 5:
            Serial.println(5);
       break;
            case 6:
            Serial.println(6);
       break;
            case 7:
            Serial.println("B");
       break;
            case 8:
            Serial.println(7);
       break;
            case 9:
            Serial.println(8);
       break;
            case 10:
            Serial.println(9);
       break;
            case 11:
            Serial.println("C");
       break;
            case 12:
            Serial.println("*");
       break;
            case 13:
            Serial.println(0);
       break;
            case 14:
            Serial.println("#");
       break;
            case 15:
            Serial.println("D");
       break;
            default:
            ;
}
}
}

// Below function is used to detect which button is pressed.

byte keypad()                   
{
 static bool no_press_flag=0;    
  for(byte x=0;x<columns;x++)     
  {
     if (digitalRead(Input[x])==HIGH);   
     else
     break;
     if(x==(columns-1))        
     {
      no_press_flag=1;
      h=0;
      v=0;
     }
  }
  if(no_press_flag==1)  
  {
    for(byte r=0;r<rows;r++)  
    digitalWrite(Output[r],LOW);
    for(h=0;h<columns;h++)  
    {
      if(digitalRead(Input[h])==HIGH) 
      continue;
      else   
      {
          for (v=0;v<rows;v++)    
          {
          digitalWrite(Output[v],HIGH);   
          if(digitalRead(Input[h])==HIGH)  
          {
            no_press_flag=0;               
            for(byte w=0;w<rows;w++)  
            digitalWrite(Output[w],LOW);
            return v*4+h;  
          }
          }
      }
    }
  }
 return 50;
}

5. Output:

 After uploading this sketch open serial monitor and you will see the following like result.

This circuit looks like following on R-Board and original UNO Board.

If you have any query please write us at info@roboindia.com

our whatsapp helpline:  +91 9694011188

Thanks and Regards
Content Development Team 
Robo India
http://roboindia.com

Leave a Reply