4×4 keypad interfacing with Arduino Uno R3 with code

Interfacing Keypad with Arduino: Our today’s tutorial we will learn how to interface keypad with Arduino. In embedded devices one of the essential part is keypad and it is used to interact with embedded devices, keypad is input device which is used to give commands to other devices, from calculator to computer; input is given through keypad, in this tutorial you will know how to interface keypad with Arduino, Arduino is a mini computer which is user-friendly. We will see how to wire the keypad with Arduino board and write the program for communication between Arduino and keypad. I recommend you to check this first tutorial on getting started with Arduino Uno R3keypad interfacing with arduino

Components to be Used:

  • 4×4 keypad
  • Arduino UNO
  • Connecting Wires

If you bought an original keypad used for this purpose then you don’t need to figure out pins but if you got it from some other electronic device then you need to identify pins which will be used for this purpose.

Identifying the Pins:

First you need to get a piece of paper and draw the right hand diagram as you see it below. I’ve already written my pin numbers (1, 2, 3 across the bottom and 7, 6, 5, 4 down the right side) which you can just leave off of your drawing. Next, you are going to use your Ohm meter to find out which pins are connected to which keys. The first thing to do is count how many pins are on your keypad (as seen in the photo below.) The photo is showing 14 pins though not all of the pins are used. Don’t worry, once you’ve completed this procedure you will know which pins are unused and can be ignored.


  • Connect your Ohm meter leads to pins 1 and 2.
  • Press all the buttons until the meter indicates a closure.
  • Write down the pin numbers next to the column and row for the key you just found. Example: Your meter is connected to pins 1 and 5. When you pressed the number 7 your meter reacted. Write 1 under COL0 and 5 next to ROW2.
  • If the meter didn’t react then move the meter lead from pin 2 to pin 3 and repeat steps 2 and 3 above.
  • Now, keep moving the lead to the next pin and repeat steps 2 and 3 for each pin.
  • Once you have reached the end move the first meter lead from pin 1 to pin 2 and repeat steps 2 and 3 while connecting the second meter lead to pins 3 through the highest pin.
  • Once you have completely identified all the pins on the diagram then you can safely ignore any unused keypad pins. You are now ready to wire the keypad to your Arduino.

If you bought keypad look one below. You don’t need to perform above steps. Below diagram is enough for knowing pin configuration.

Wiring the Circuit:

Follow the given pin order for wiring the circuit. As shown in left diagram. Start from left to right.

Keypad Pin 1 (R4) –> Arduino Pin 5

Keypad Pin 2 (R3) –> Arduino Pin 4

Keypad Pin 3 (R2) –> Arduino Pin 3

Keypad Pin 4 (R1) –> Arduino Pin 2

Keypad Pin 5 (C4) –> Arduino Pin 9

Keypad Pin 6 (C3) –> Arduino Pin 8

Keypad Pin 7 (C2) –> Arduino Pin 7

Keypad Pin 8 (C1) –> Arduino Pin 6

Connect the circuit according to given diagram.

Add Keypad Library:

Before going to programming part first download keypad library from Arduino official website.

Code for Arduino:

#include <Keypad.h>

const byte ROWS = 4; //four rows

const byte COLS = 4; //three columns

char keys[ROWS][COLS] = {


  {'4','5','6','B' },




byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){



void loop(){

  char key = keypad.getKey();

  if (key != NO_KEY){




Testing the Circuit:

  1. After hardware connection, insert the sample sketch into the Arduino IDE.
  2. Using a USB cable, connect the ports from the Arduino to the computer.
  3. Upload the program.
  4. See the results in the serial monitor


Now whichever key you press, it is shown on serial monitor of Arduino IDE.

3 thoughts on “4×4 keypad interfacing with Arduino Uno R3 with code”

  1. Hi,

    Easier to connect via I2C and PCF8574 to the Arduino.
    You can also connect Display 16×2 with the same type PCF8574 to I2C BUS and it will only take up 2 I/O on the Arduino!

    • Hi Max, Yes you are right. But this tutorial is just to give an idea to beginners about keypad interfacing with Arduino. There are many ways to save GPIO pins of Arduino. Even with one Analog pin Arduino we can scan a keypad of any size.


Leave a Comment