How to use 4X4 matrix keypad with Arduino

In this Arduino tutorial, I will teach you how to use a 4X4 matrix keypad/membrane keypad with Arduino. This keypad can be used in many smart applications in system automation and control. It can basically be used to automate password enabled entry.
Below are images of some matrix keypads

matrix keypad with arduino
multiple keys membrane keypad
4X5 Matrix membrane keypad
4X5 Matrix membrane keypad
4X4 Matrix keypad
4X4 Matrix keypad
3X4 Matrix keypad
Figure 4.0 3X4 Matrix keypad
1X4 Matrix keypad
Figure 5.0 1X4 Matrix keypad

In this Arduino tutorial we will discuss:

  1. How 4X4 matrix keypad works
  2. How to 4X4 matrix keypad with Arduino
  3. How to print pressed keys on the serial monitor
  4. How to connect LCD with I2C, Matrix keypad and Arduino
  5. How to print pressed keys on LCD

Suggested readings:

Arduino tutorial for beginners
Arduino Serial communication
Interfacing LCD with Arduino
Arduino library, what it is and how it works

1X4 matrix keypad, 3X4 matrix keypad, 4X5 matrix keypad and salvaged telephone keypads can be used with Arduino.

How 4×4 matrix keypad works

The 4X4 membrane keypad has the buttons arranged in rows and columns in a 4X4 matrix. Under the buttons are membrane switches. Each switch in a row which is under a button key is connected to another switch in the same row through an electronically conductive trace laid under the keypad buttons, and there are 4 rows in all.

Again, each switch in a column which is under a button key is connected to another switch in the same column through an electronically conductive trace laid under the keypad buttons, and there are 4 columns in all. The 4 rows and 4 columns makeup 8 pins of the 4X4 matrix keypad .See image below

internal connection of a 4X4 matrix keypad
Figure 6.0 internal connection of a 4X4 matrix keypad

Pressing a button joins a row and a column, thereby creating an electrical conducting path between the row and the column. See image below.

Schematic of the 4X4 matrix keypad
Figure 7.0 Schematic of the 4X4 matrix keypad

How Arduino detects pressed button

Arduino detects which button is pressed by identifying which rows and columns where joined, and here is how it happens.

  • When no key/button is pressed, the Arduino “keypad.hlibrary code causes the row pins to go LOW and the column pins to go HIGH. See image below.
How Arduino detects pressed key
Figure 8.0 How Arduino detects pressed key

When a key/button is pressed, this causes a row key and a column key to be joined. Because the row pins were initially pulled LOW by the Arduino “keypad.hlibrary code, the particular column pin that is now in contact with a row as a result of the pressed button will be pulled LOW from its initially HIGH state. Hence, we now know the column on which the key/button was pressed. See image below.

How Arduino detects pressed key
Figure 9.0 How Arduino detects pressed key

 To identify the row that coincides with the pressed column, the Arduino with the “keypad.hlibrary will cause the row pins to go high sequentially and simultaneously read the column pins to note which row pin will cause a column key to go high from a LOW state. See image below.

How Arduino detects pressed key
Figure 10.0 How Arduino detects pressed key

Since all the column pins have been on a HIGH state from the beginning and has not switched state except the column that was pressed and was switched to a LOW state, it is this same pin that will switch from a LOW state to a HIGH state. With this, the Arduino now knows which row pin was pressed. By combining the row and column pins, we can identify the exact key/button that was pressed.

 From the explanation above, we can say that row 2 and column 1 pins were connected when the button was pressed, hence, with this information, we can deduce that button 4 was pressed.

Wiring the matrix keypad with Arduino

The first wiring we will make with the keypad is to connect it the Arduino and a computer, and with a simple code we can print the various key presses on the computer screen using the serial monitor feature of the Arduino IDE. See the wiring diagram below.

Arduino connection with 4X4 matrix
Figure 11.0 Arduino connection with 4X4 matrix

It should be noted that the key factor driving the perfect interaction between the 4X4 matrix keypad and the Arduino board is the Keypad library, the library written by Mark Stanley and Alexander Brevig works just fine. I have written a tutorial on how to download Arduino libraries, you can read it here to guide you on how to install the keypad library.

#include <Keypad.h>
const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
  }
}

Once you have made the connection as shown above, upload the code above to the Arduino. Once the code is uploaded, open the Arduino Serial monitor and watch, any key you press on the keypad will be displayed on the serial monitor.

serial monitor displaying key presses
Figure 12.0 Serial monitor displaying key presses

How to connect LCD with I2C, Matrix keypad and Arduino

Because the Arduino UNO board does not have enough digital pins to accommodate all the LCD pins as well as the 4X4 matrix keypad pins, hence, we use an I2C LCD module. The image below below shows an I2C module.

I2C module
Figure 13.0 I2C module

An I2C (inter-integrated circuit) is a Two wire interface serial communication protocol used to interface several digital integrated circuits in a master “Master-Slave” setup. The I2C module has an address for any digital device it is being connected to. For our design, the bus address is 0X27. Yours may have a different bus address, to find out which address is your I2C module, upload the code below to your Arduino board after you have made the following connection.

Arduino and i2c
Figure 14.0 Arduino and I2C
#include <Wire.h> //include Wire.h library
void setup()
{
  Wire.begin(); // Wire communication begin
  Serial.begin(9600); // The baudrate of Serial monitor is set in 9600
  while (!Serial); // Waiting for Serial Monitor
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address; //variable for error and I2C address
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for (address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);
      Serial.println("  !");
      nDevices++;
    }
    else if (error == 4)
    {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000); // wait 5 seconds for the next I2C scan
}
Serial monitor print of the i2c bus address
Figure 15.0 Serial monitor print of the I2C bus address

Above is the image of the serial monitor showing the printed bus address.

The I2C uses two wires, the Serial Clock (SCL) and Serial Data (SDA). It is in different pins on the various Arduino boards, see table below.

Table below show various Arduino board pins for Serial Clock and Serial Data connections.

Arduino SCL and SDA pins

On the Sparkfun RedBoard, the SCL and SDA pins have dedicated pins, see below

sparkfun red board showing SCL and SDA pins
Figure 16.0 Sparkfun red board showing SCL and SDA pins

To display the pressed keys on a Liquid crystal display, connect the parts as shown below and upload the Arduino code that follows

How to connect LCD with I2C, Matrix keypad and Arduino
Figure 17.0 How to connect LCD with I2C, Matrix keypad and Arduino

the Arduino code is shown below

#include <Wire.h>// library to invoke I2C protocol
#include <LiquidCrystal_I2C.h>//library to interface the I2C breakout board with the LCd display
#include <Keypad.h>// the matrix keypad library

const byte ROWS = 4;// number of rows on the keypad
const byte COLS = 4;// number of columns on the keypad

char hexaKeys[ROWS][COLS] = {  // mapping the keys to characters
  {'1', '2', '3', 'A'},        
  {'4', '5', '6', 'B'},        
  {'7', '8', '9', 'C'},        
  {'*', '0', '#', 'D'}         
};

byte rowPins[ROWS] = {9, 8, 7, 6}; //Arduino pins connected to row pins of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Arduino pins connected to column pins of the keypad

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);//associating the arduino pins that are connected to 
                                                                                 //the keypad to the various characters

LiquidCrystal_I2C lcd(0x27, 16, 2);  // associating the LCD with the I2C Serial bus

void setup()
{
  lcd.backlight();//setting up the LCD's backlight
  lcd.init(); // initialise the LCD
  Serial.begin(9600);//initialise Serial communication
}

void loop(){
  char customKey = customKeypad.getKey();//awaiting key press
  if (customKey)//checking whether a key is press
  {
    lcd.clear();//clears the LCD
    lcd.setCursor(0, 0); //setting printing position on the LCD
    lcd.print(customKey);//print the key pressed on the LCD
     Serial.println(customKey);//print the pressed key on the Serial monitor
  }
}

Once the code is successfully uploaded, any key you press on the 4X4 Matrix keypad will be displayed on the 16×2 liquid crystal display.

One of the main uses of the 4X4 matrix/membrane keypad is to design password enabled entry, hence, in the next tutorial, we will discuss how to use the 4X4 matrix keypad to design a password enabled door. So, check out soon. Before then, endeavour to read the following Arduino project tutorials:

Interfacing 16X2 LCD with Arduino
How to make robotic arm with Arduino
Arduino remote controlled light switch
Water level indicator and control using ultrasonic sensor and Arduino
Arduino scrolling text display

Leave a Reply

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