4x4 Keypad Matrix

Keypad matrix is a superb example of multiplexing. In keypad we interface 16 keys with 8 pins.
In Keypad, total number of rows required is √(16) = 4.
Total number of columns required is (16)/(rows) = 4.
Thus the total number of pins required are (R+C) = 8.
Thus with this method of multiplexing we can interface N2 leds or push button by 2N pins. hatchnhack_keypad_matrix

Example :

Interface Keypad interface with Arduino.

  1. We have four rows and four columns
  2. We define rows as ‘OUTPUT’ and columns as ‘INPUT’. 3. Give the ‘HIGH’ voltage on output pins.
  3. Now, we detect if any button is pressed.
  4. If any button is pressed, make all the row output ‘LOW’.
  5. Now, we give the HIGH voltage to each row one by one and check the input voltage at each column pin.
  6. Thus we detect the push button which is pressed according to the row which is driven High and the column at which the input voltage is High.
int R[4] = {10,11,12,13};                   // Rows of the Keypad Matrix
int C[4] = {6,7,8,9};                       // Columns of the keypad Matrix 
int row  = 0;                           //To store which rows button is pressed
int column = 0;                         //To store which column button is pressed
int i; 
bool is_switch_pressed;                //To detect whether the switch is pressed or not 
char key_press[16] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'}; 
void setup()
  for(i=0; i<4;i++) 

    pinMode(R[i],OUTPUT);              // Initialize all rows pin as Output
    pinMode(C[i],INPUT);               //Initialize all column pin as Input
void loop() {
  for(i =0 ; i< 4; i++) 
    digitalWrite(R[i],HIGH);           //Put all row pin as HIGH to detect 
//which button is pressed 
    is_switch_pressed = digitalRead(C[0]) || digitalRead(C[1]) || digitalRead(C[2]) || digitalRead(C[3]); 
    //If any button is pressed 
        for(i = 0 ; i<4 ; i++) 
            for(int j = 0 ; j<4; j++) 
                for(int j = 0 ; j<4; j++ ) 
                    row = j;                    
                    column  = i; 
        Serial.print("The key pressed is : "); 
    for(int i =0 ; i<4 ; i++) 
            is_switch_pressed = digitalRead(C[0]) || digitalRead(C[1]) || digitalRead(C[2]) || digitalRead(C[3]); 

Exercise :

  • Interface keypad matrix and lcd with Arduino such that whenever you press any key, the number or symbol display on the lCD.
  • Make a calculator with the help of keypad matrix, lcd and Navi switches.