How to interface a matrix keypad to an Arduino


In this post, we are going to show you how to interface a matrix keypad to an Arduino.
In the most simple case, typically one port pin (GPIO) is required to read the status of a switch connected to a microcontroller. When multiple switches need to be read, it is not feasible to allocate one pin for each of them. This is when a matrix arrangement of switches can be used to reduce the amount of pins required.
Matrix Keypad and Arduino
The schematic of a 4×4 matrix keypad is shown below. With a 4×4 matrix keypad, only 8 GPIO is required to read the 16 switches.
Matrix Keypad

Matrix Keypad logic

All the switches are assumed to be released when at rest. So there is no connection between the rows (R1-R4) and columns (C1-C4). When any one of the switches are pressed, the corresponding rows and columns are short circuited. For example, if switch 1 is pressed, C1 and R1 is short-circuited. If 7 is pressed down, then C3 is linked to R2 and so on.

So, how does a matrix keypad works? The matrix is controlled by a microcontroller. For the above 4×4 matrix with 16-buttons, 8 pins of a micro controller is used. 4 pins are set to OUTPUTS and connected to the COLUMN wires (C1-C4), while another 4 pins are used as INPUTS and is connected to the ROW wires (R1-R4). The OUTPUTS pins are set to logic HIGH at the same time. The Columns output go LOW one by one in cycle as depicted in the animation below.
Matrix Keypad Animation

Every time each Column are set to LOW, the other Columns are set to HIGH and the rows are read. For example, if C1 is set to low and reading R1 returns LOW, it means that number 1 is being pressed down.

The Matrix keypad is wired to an Arduino as follows:
Arduino Matrix Keypad Connections

Arduino Code

The code scans the keypad for a key press and displays it on an LCD. The LCD displays all the consecutive key presses. The “*” key press clears the LCD screen. Below is a snippet of the code and a link to the full code is also provided.

void loop() {
 lcd.setCursor(i, 1);
 int key = getinp(); //Get key press if any
 if (key!=0xff) //if a key was pressed
  i = i + 1; //index used to move the LCD cursor by 1.
  if (key == 15)
    lcd.setCursor(1, 0);
    lcd.print("Key Pressed:");
    i = 1;
    lcd.print(key); //Print the pressed key

/*Function to scan matrix keypad for key presses*/  
int getKey()
  int c =0xFF;    
  /*Set all columns to low */
  /*Set Column 4 to low */
  digitalWrite(A0,LOW); //C4
  /*Read all rows one by one */
  if(analogRead(A4)== LOW) c=0x0D; //R4
  if(analogRead(A5)== LOW) c=0x0C; //R3
  if(analogRead(A6)== LOW) c=0x0B; //R2
  if(analogRead(A7)== LOW) c=0x0A; //R1
  /*Set Column 3 to low */
  digitalWrite(A0,HIGH); //C4
  digitalWrite(A1,LOW); //C3
  /*Read all rows one by one */
  if(analogRead(A4)== LOW) c=0x0E; //R4
  if(analogRead(A5)== LOW) c=9; //R3
  if(analogRead(A6)== LOW) c=6; //R2
  if(analogRead(A7)== LOW) c=3; //R1
  /*Set Column 2 to low */
  digitalWrite(A1,HIGH); //C3
  digitalWrite(A2,LOW); //C2
  /*Read all rows one by one */
  if(analogRead(A4)== LOW) c=0; //R4
  if(analogRead(A5)== LOW) c=8; //R3
  if(analogRead(A6)== LOW) c=5; //R2
  if(analogRead(A7)== LOW) c=2; //R1
  /*Set Column 1 to low */
  digitalWrite(A2,HIGH); //C2
  digitalWrite(A3,LOW); //C1
  /*Read all rows one by one */
  if(analogRead(A4)== LOW) c=15; //R4
  if(analogRead(A5)== LOW) c=7; //R3
  if(analogRead(A6)== LOW) c=4; //R2
  if(analogRead(A7)== LOW) c=1; //R1

return c;
/*Function to get input key pressed*/
int getinp()
  int n;     //value to store key press
  int d;    //value used to temporarily store keypress

  n = 0xFF;

while (n == 0xFF) //waits for keypress
  n = getKey();       //Accessing the getKey() function
  d = n;        //give value of key pressed to d
  while (d != 0xFF)       //loop waits for the user to release the button
    d = getKey();     // Scans keypad for a keypress
 delay(300);          // debouncing time
return n;         //return value of key pressed

The full code can be downloaded here: ArduinoMatrixkeypadCode

For a demo, watch this:

If you have any comments or suggestions, please contact us at contact@

Add a Comment

Your email address will not be published. Required fields are marked *