Matrix Keypad Interfacing with Arduino

The keyboard matrix is the arrangement of circuit connections between the keyboard controller and all the keys on the keyboard. Each key does not have its own dedicated circuit; instead, each key is placed at the intersection of a matrix row and a matrix column. The keyboard repeatedly applies current to each column in turn, and checks to see which rows output current. From this, the keyboard can deduce which keys in that column have been depressed. The matrix takes the form of a printed circuit, either on a conventional PCB, or on membrane sheets.

The matrix arrangement allows for current to flow backwards through part of the circuit, which leads to “ghost” keypresses being detected when certain groups of three keys are held down. This ghosting phenomemon can be prevented by placing diodes in series with each switch (so long as the keyboard has a PCB) or by using capacitive sensing. The problem can be mitigated by using a gaming-optimised matrix or by blocking any combination of three keys or more. The latter prevents keyboard shortcuts from functioning, so the matrix is laid out in such a way as to ensure that pressing groups of three or four keys works so long as all but one of the keys are modifiers. Keyboard shortcuts involving multiple modifiers is a relative modern phenomenon on PCs, so vintage keyboards without diode protection will either ghost or block certain shortcuts.

As a result of this and the fact that keys on the keyboard are not all square, the matrix arrangement does not take the form of a regular grid. Adjacent matrix intersections do not always correspond with adjacent keys and vice versa.


1. Where Matrix keypad is used?
2. What is Matrix Keypad?
3. How to detect a key press?
4. 4×4 Keypad Pin diagram
5. Arduino 4×4 Keypad connections
6. 4×4 Matrix Keypad Library.
7. Arduino 4×4 keypad code

Where Matrix keypad is used?

In many applications we need user input for the system, It may be a set point for controller, data, preset values, etc. But some applications require multiple keys for entering numbers and other special functions. Such applications are:

  1. Mobile phone
  2. Code lock system
  3. Access control systems
  4. Data entry equipment’s

What is Matrix keypad?

If we need to enter 0 to 9 and A to F, we need 16 buttons, as we know that single key requires single IO line, It means for entering 0 to 9 and A to F we need 16 keys i.e. 16 IO Lines, but our Arduino have limited IO lines, and circuit become complex because of large number of wires, We use Keypad Matrix as shown in figure for 16 Keys It uses only 8 IO Lines.

4x4 Matrix Keypad
4×4 Matrix Keypad

How to detect a key press in Arduino?

Before we start exploring matrix keypad let us see a single key detection algorithm.
Reading a IO line using Arduino command


This command gives result in HIGH or LOW (1 or 0) depending on the voltage at arduino pin 6

Example: As shown in below figure we have a switch connected on arduino pin 6 and when switch is pressed LED on pin 13 should become on.

Arduino Single Key Detection
Single Key Detection

Note:  PIN 13 have on board LED, so it is not shown in circuit

Before we program arduino understand few basic concepts

Where to connect second pin of switch?

Switch second pin in most cases is connected to Ground. And IO pin where we have connected switch is held at high (pulled up).

What is pull up?

Pull up is putting IO pin in high(up) state by means of resistor. One end of resistor is connected to IO pin and second to the +5V. Normally 4.7K to 10K resistor is used.

Why 4.7K to 10K Ohm resistor?

To keep the current in safe limit (normally close to 500uA to 1mAmp)
Use of very high pull up resistor may introduce noise; resistor must have value more than noise current level.

How we can eliminate this resistor?

This resistor can be eliminated by using internal pull up resistor of microcontroller  ATmega328.
Internal pull up can be activated by making that pin high “digitalWrite(Pin,HIGH);” and defining it as input “pinMode(Pin,INPUT);”

Program for single key interface with arduino

 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 6.
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 6 from ground
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 6;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     //When switch is pressed IO pin becomes low
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 

Let us move to our 4×4 Matrix keypad interface

4×4 Matrix Keypad Connections with Arduino

Connect Arduino to keypad as shown in figure.

Arduino Connections with keypad
Arduino Connections with keypad

Rows A,B,C,D are connected to arduino pin 9,8,7,6 and columns 1,2,3,4 are connected to arduino pin 5,4,3,2.

We are observing pressed key on serial terminal. So we have not connected any display.

Arduino Code for 4×4 Keypad interfacing

We are using Keypad Library for this code

    4x4 keypad interfacing with arduino
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char CalculatorKeys[ROWS][COLS] = {
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(CalculatorKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
void loop(){
  char customKey = customKeypad.getKey(); //read pressed key
  if (customKey){    //Check if keyis pressed
    Serial.println(customKey);    //display pressed key on serial terminal


Press some keys and observe the result on serial terminal. This way you can make digital code lock projects also.

Leave a Reply