Introduction to Matrix Keypad :

logo_YT.jpg

Matrix Keypads normally available in 4x4 buttons or 4x3 buttons formats.   By using matrix keypad(s) the number of pins used for Arduino for input can be reduced and also aesthetic.

SBS-ARD-6-KEYPAD-TN-WIX.jpg

A matrix keypad is equivalent to connecting push button switches in matrix form and connecting each pin (out of 2) of the button to a row and a column. So, when a button is pressed on the keypad, the connected row and column of the button are shorted. The concept of button connections in a keypad is shown here for easy understanding.

KEYPAD_ANIM2.gif

The pinouts of 4x3 and 4x4 standard keypads are shown here.  You may confirm it, by using continuity tester (refer video).  Testing a keypad before using in a project is advisable.

keypads_pinouts_small.jpg

Connecting Matrix Keypad :

All the pins ( 7 or 8 ) of matrix keypad should be connected to Arduino board.

The test circuit diagram used to connect a 4x4 keypad to arduino board is shown here.  In case of 4x3 keypad, connection between column_4 of keypad and A0 of arduino is not required. 

A 4 x 4 matrix keypad is used here for explanation.

keypad_test_circuit_small.jpg

Working with Matrix Keypad :

The pinouts are divided in to rows and column connections as shown above, the row connections are treated as voltage inputs, sequentially, one by one and the voltages are read from column connections, to know the button contact status.  The row and column connections may be reversed for voltage inputs and reading button status.

To understand working principle of a matrix keyboard, 4 pins of Arduino are configured as outputs and connected to 4 rows. Again, 3 or 4 (based on columns) pins of Arduino are configured as inputs and set to logic high by default.

Now, using for loop, one row out of four, is set to logic low, sequentially and remaining three rows are set to logic high.  Then, start scanning the status (logic levels) of all the column pins sequentially.

If any button linked to the particular row is pressed, then the column connected to the pressed button is also pulled low (logic low), where as remaining columns are set to high, by default. Now, read the row number and column number to know the button number.  If no button is pressed throughout the for loop, then no column is set to logic low.

This process continues, whenever you want to read the keypad status, which is called as a function readKeypad, in our Arduino sketch.

Sketching for Matrix Keypad :

The full sketch is explained for 4x4 in magenta coloured text. For 4x3 keypad, relative code is marked with green colour.  Comments are included wherever required, for easy understanding.

The value of pressed button may be viewed using Serial Monitor,  

const int nRows = 4; //4 number of Rows
const int nCols = 4; // = 3; //4 or 3 number of Columns

 

// assign text on buttons to an array for 4 rows and 4 columns
char keys[nRows][nCols] =

{
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

 

// assign text on buttons to an array for 4 rows and 3 columns
char keys[nRows][nCols] =

{
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};


const int pRows[nRows] = { 9,  8, A5, A4}; //Arduino pins of 4 Rows
const int pCols[nCols] = {A3, A2, A1, A0}; //Arduino pins of 4 Columns

const int pCols[nCols] = {A3, A2, A1}; //Arduino pins of 3 Columns

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
// pin no 13 by default
  Serial.begin(9600);  // start Serial connection with 9600 baud rate
  for ( int i=0; i<nRows; i++ )
   pinMode(pRows[i], OUTPUT);  
// set all rows as output mode

  for ( int i=0; i<nCols; i++ )
  {
   pinMode(pCols[i], INPUT);   
// set all columns as input mode
   digitalWrite(pCols[i], HIGH);  // set all columns to high, by default
  }
}

char readKeypad ()  // important function used to read the button press status
{
  for ( int r=0; r<nRows; r++ )
  {
    for ( int i=0; i<nRows; i++ )
    digitalWrite(pRows[i], HIGH);  
// initially set all rows to high
    
    digitalWrite(pRows[r], LOW);  
// set one row, i.e., r, as logic low
    
    delay(1);
    
    for ( int c=0; c<nCols; c++ )
    {
      if (digitalRead(pCols[c])==LOW) 
// read the status of all columns
        return ( keys[r][c] );  // and return the key text, if logic low
    }
  }

  return 0;  // return null, if no key is pressed
}

void loop()

{
  char key = readKeypad (); 
// call the function in the main loop

  if (key) // check for key is pressed
  {
    digitalWrite(LED_BUILTIN, HIGH); 
// glow the on-board LED
    Serial.print ("Key pressed = " );  // send the text to serial
    Serial.println(key);  // send the key text value to serial
    delay(250);
    digitalWrite(LED_BUILTIN, LOW); 
// set the on-board LED off
  }
}

The readKeypad function is the important function, which reads the status of a button press and returns the text value, if any button is pressed or returns null value, if no button is pressed.

The LED_BUILTIN is used to indicate that a button is pressed on keypad.  The delay(250) is included to avoid multiple key press status, when a button is pressed, which may be adjusted as per individuals's requirement.