How to use a 4x4 Keypad Matrix with Arduino

How to use a 4x4 Keypad Matrix for Arduino

4x4 Keypad Matrix for Arduino

INTRODUCTION

A 4x4 keypad consists of 16 buttons arranged in the form of an array containing four lines and four columns.

APPLICATIONS

It is used in the authentication systems where input from the user is required. Other applications include ATMs, Vending machines, Door lock systems, and many more. Even your mobile phone has a keypad.

WIRING DIAGRAM

Pins C1, C2, C3, C4 on the keypad should be connected to digital pins 5, 4, 3, 2 on the Arduino, respectively.

Pins R1, R2, R3, R4 on the keypad should be connected to digital pins 9, 8, 7, 6 on the Arduino, respectively.

4x4 keypad matrix connections for arduino

CODE

Download the required library from:

https://playground.arduino.cc/code/keypad and paste it in the “Arduino/libraries" file path in your PC or MAC.

Guide to installing libraries here:

https://www.arduino.cc/en/guide/libraries

https://www.digikey.com/en/maker/blogs/2018/how-to-install-arduino-libraries

 

#include <Keypad.h>

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


char keymap[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 myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, ROWS, COLS);

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  char keypressed = myKeypad.getKey();
  if (keypressed)
    {
      Serial.println(keypressed);
    }
}

END RESULT

Open the Serial monitor and see results when buttons are pressed

ADDITIONAL RESOURCES

https://www.instructables.com/id/Connecting-a-4-x-4-Membrane-Keypad-to-an-Arduino