4×4 Matrix Keyboard Keypad

A while ago i purchased a 4×4 keypad on ebay, and actually forgot everything about it. But today it arrived!!! 🙂

After 30 secs on google I found a library for my Arduino and decided to install it.

The way to determine is pretty straight forward.

You actually just connect to some pins, and see what happens when you press the buttons.

For this keypad I had to start in the opposite sides, minus one. The minus one is because the two outer pins in opposite ends are not used for anything.

To test what was what, I grabbed my multimeter, and discovered a dead battery… Instead I then also used my Arduino to check for connection.

void setup()
{
  Serial.begin(9200);
  pinMode(2, INPUT);
  digitalWrite(2, HIGH);
}

int last;
void loop()
{
  if (digitalRead(2) != last)
  {
    last = digitalRead(2);
    Serial.println(digitalRead(2));
  }
}

With this it will write on the serial monitor if there are a connection or not, and after a few minutes I came up with this setup.

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#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 hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

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

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

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

Pin connections, from left to right on the keypad

KeyPad > Arduino
 Pin 0 > N/A
 Pin 1 > Pin 2
 Pin 2 > Pin 3
 Pin 3 > Pin 4
 Pin 4 > Pin 5
 Pin 5 > Pin 6
 Pin 6 > Pin 7
 Pin 7 > Pin 8
 Pin 8 > Pin 9
 Pin 9 > N/A

In the serial monitor, you should now be able to see what button is pressed. 🙂

Leave a Reply