Tag Archives: input device

4×4 Matrix keypad interface with Arduino (8 Pin method)

Whenever we design any embedded project or gadget, user input is the most important feature to be designed. The user input may contain alphabetical & numerical keys, also some arrow keys for display navigation. You may find keypads in door locks, printers, microwaves, phones, etc. In the case of Arduino, you may find various input interfaces and matrix keypads are most famous among all. A matrix keypad is a set of keys arranged in a matrix pattern that enables more keys in limited pins. A matrix keypad is identified by the number of Column X Row, for ex: 4×4 & 4×3.


Usually, membrane matrix keypad is widely used in various devices, but key mounted PCB keypad is also used in many specific designs, and both of them are working on the same concept. Here you can see the pin-out of 4×4 matrix keypad:

Pin No. 1 2 3 4 5 6 7 8
Raw/Column R1 R2 R3 R4 C1 C2 C3 C4

As you can see, in 4 (Row) x 4 (Column) keypad, there is a total of 16 keys. All these membrane keys are connected to each other with a conductive trace layer below the layer of pad forming a 4×4 matrix grid. Generally, if you want to attach 16 keys, you may need 16 input pins + 1 Ground pin = 17 Pins. But in the case of a 4×4 matrix keypad, it takes only 8 pins. Let’s see the working principle of the 4×4 matrix keypad.

The working principle is as simple as its internal wiring. If you press a button, it will shorten its row with the joining column & the same will allow flowing current between them. For example, if you press ‘6’, R2 & C3 will get shorten. Meanwhile, the assigned microcontroller will be continuously scanning all these lines & will sense the button pressed state.

4×4 Matrix keypad connection with Arduino UNO

Connect the keypad with your Arduino board (We have taken UNO in our case) as shown in the above figure, you can use any digital pins from D2 to D13 (D0 & D1 are avoided as those pins are for onboard UART & advised to not use them for any other purpose than serial communication.

Next, to use this keypad with your Arduino board you will need library for the same. You can install the required library using the library manager in Arduino IDE. Open Arduino IDE, Sketch>Include Library>Manage Libraries and Library Manager will load.

In the Library manager, type ‘keypad’ and wait for the results. For this tutorial, we will use a library ‘Keypad by Mark Stanley, Alexander Brevig’. Select that library from the results and install it.


You can download this sketch from here.

After uploading the sketch to your Arduino board, you can see the output on IDE serial monitor for which key is pressed.

Code Explanation

As you can see in code, after including ‘Keypad.h’ library in the first line we have defined number of rows & columns. It is required because sometimes you may have to use 3×4 or 1×4 keypad too so you can set numbers as per requirement.

Next, a two-dimensional keymap array is defined as ‘hexakeys[ROWS][COLS]’. It contains the characters to be displayed when a particular key is pressed on the keypad.

Characters defined in the code are as per the keypad example we have taken. If you have different characters on your keypad then you can configure the same. For example: If you want to use this keypad as a calculator, you can define the array like this. 

After that, we have to create an object of the library ‘Keypad(makeKeymap(hexakeys), rowPins, colPins, ROWS, COLS )’ which contains five parameters.

makeKeymap(hexakeys) : This parameter is used to make the internal keymap equal to our user-defined keymap.

rowPins & colPins : These are the pins on Arduino where rows & columns of keypad connected.

ROWS & COLS : These are the number of rows & columns of our matrix keypad.

Once the above keypad object is created, we have to use a simple command getKey() to check which key is pressed.

This library contains some of the useful functions which are not mentioned/used in the above example code:

  • setHoldTime(unsigned int time) : This function sets the number of milliseconds the user will have to hold a button until the HOLD state is triggered.
  • setDebounceTime(unsigned int time) : This function sets the number of milliseconds the keypad will wait until it accepts a new keypress or keyEvent.
  • KeyState getState() : This function returns the present state of any of the keys. The four defined states are IDLE, PRESSED, RELEASED & HOLD.
  • char waitForKey() : This function is used to waits forever until someone presses a key. Using this function might be problematic as it blocks all other functions/code/loop until any key is pressed. It means no IO On-Off, no LCD screen updates, no interrupt routines etc.
  • addEventListener(keypadEvent) : This function triggers an event if the keypad is used. For ex: You can use this to blink any LED while any key is pressed.

I hope this tutorial will be helpful to add user input enhancement in your DIY stuff. You can make various projects using such keypad where you need to input text/numbers, in future such projects will be uploaded in #BeatYourBit website. Give your feedback about this tutorial in Contact, Your feedback will be appreciated.

Subscribe BYB on FacebookTwitterInstagram & Telegram for latest updates.