4×5 Matrix Keypad Example Code and Schematic

Hello world! Today i made a Example Code for the 4×5 matrix keypad.
I could not find any codes that were working or doing anything at all. So i made this example code for the 4×5 keypad.
You can let the buttons do anything! I just gave each button a function in the code, You can see what value is what key in the serial monitor.

Subscribe to my Youtube channel!

Schematic:
4x5 Matrix Keypad

Keypad pinout:
4x5 Matrix Keypad

Just wire it up like the schematic. Here is little more info i grabbed from the internet about the 4×5 Keypad.

Connect:
Row 1 to Arduino pin 2
Row 2 to Arduino pin 3
Row 3 to Arduino pin 4
Row 4 to Arduino pin 5
Row 5 to Arduino pin 6
Column A to Arduino pin 8
Column B to Arduino pin 9
Column C to Arduino pin 10
Column D to Arduino pin 11

Key values with connector at top.
4 9 14 19
3 8 13 18
2 7 12 17
1 6 11 16
0 5 10 15

Connector (from left to right with keys facing up, connector at top):
Columns Rows
X X X X X X X X X
A B C D 1 2 3 4 5

(Rows link keys from left to right, columns link keys from top to bottom)
Good luck!
(NOTE! This project is just an example on how the keypad works! It does not really have a function other then turning on leds, a buzzer & relays.)

Parts list:
Arduino Mega! ( You need a mega!, Or you need to multiplex!)
10x Leds.
1x Buzzer.
Keypad 4×5 Matrix Keypad (very cheap on aliexpress)
2x 1 channel relay
Jumper wires set MM/FF/MF

Code:

/*
Author: Danny van den Brande, Arduinosensors.nl. BlueCore Tech.
In this example code i gave all the buttons a function, Because i
can not find any projects with this keypad, i made a example.
If you make a project with it such as a alarm or whatever. 
Please share it with me @ contact@arduinosensor.nl.
With this code way we know which value is assigned to which key, 
you can also see the values in the serial monitor.
you can modify those parts as you wish to give it other functions.
*/
int Buzzer = 16;
int Relay1 = 14;
int Relay2 = 15;
int Led1 = 30;
int Led2 = 31;
int Led3 = 32;
int Led4 = 33;
int Led5 = 34;
int Led6 = 35;
int Led7 = 36;
int Led8 = 37;
int Led9 = 38;
int Led10 = 39;
int rowCounter =0; // row counter
int columnCounter =0; // column counter
int foundColumn = 0;
boolean foundCol = false;
int keyValue = 0;
int noKey = 0;
boolean readKey = false;
int debounce = 300; // set this to the lowest value that gives the best result
String keyString;
int keyNum; // used for numeric keys
boolean numericValue = false; // flag to show if number selected

const int row1 = 2;
const int row2 = 3;
const int row3 = 4;
const int row4 = 5;
const int row5 = 6;
const int colA = 8;
const int colB = 9;
const int colC = 10;
const int colD = 11;

const int ledPin = 13; // onboard LED

void setup(){
  Serial.begin(9600);
  pinMode(row1, OUTPUT);
  pinMode(row2, OUTPUT);
  pinMode(row3, OUTPUT);
  pinMode(row4, OUTPUT);
  pinMode(row5, OUTPUT);
  pinMode(colA, INPUT_PULLUP);
  pinMode(colB, INPUT_PULLUP);
  pinMode(colC, INPUT_PULLUP);
  pinMode(colD, INPUT_PULLUP);
  //
  pinMode(ledPin, OUTPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Led1, OUTPUT);
  pinMode(Led2, OUTPUT);
  pinMode(Led3, OUTPUT);
  pinMode(Led4, OUTPUT);
  pinMode(Led5, OUTPUT);
  pinMode(Led6, OUTPUT);
  pinMode(Led7, OUTPUT);
  pinMode(Led8, OUTPUT);
  pinMode(Led9, OUTPUT);
  pinMode(Led10, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  
  digitalWrite(ledPin, LOW); // turn LED off
}

void loop(){
  if(noKey == 20){ // no keys were pressed
    readKey = true;  // keyboard is ready to accept a new keypress
  }
  noKey = 0;
  for(rowCounter=row1; rowCounter<(row5 +1); rowCounter++){
    scanRow(); // switch on one row at a time
    for(columnCounter = colA; columnCounter <colD +1; columnCounter++){
      readColumn(); // read the switch pressed
      if (foundCol== true){
        keyValue =(rowCounter-row1) +5*(columnCounter - colA);
      }
    }
  }
  if(readKey==true && noKey == 19){  // a key has been pressed
    convertKey(); //convert key number to keypad value 
    //   
    // used for debug   
    Serial.print(keyValue); 
    Serial.print("  -  ");
    Serial.println(keyString);
    //
    if (keyValue == 4)
    {
      digitalWrite(Relay1, HIGH); // RELAY 1
    }
    else if (keyValue == 16) // ESC KEY
    {
      digitalWrite(Relay1, LOW);
    }
    {
    
}

    if (keyValue == 9)
    {
      digitalWrite(Relay2, HIGH); // RELAY 2
    }
    else if (keyValue == 16)
    {
      digitalWrite(Relay2, LOW);
    }
    {
    
}

    if (keyValue == 3)
    {
      digitalWrite(Led1, HIGH); // LED 1
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led1, LOW);
    }
    {
    
}

    if (keyValue == 8)
    {
      digitalWrite(Led2, HIGH); // LED 2
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led2, LOW);
    }
    {
    
}
    if (keyValue == 13)
    {
      digitalWrite(Led3, HIGH); // LED 3
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led3, LOW);
    }
    {
    
}

     if (keyValue == 2)
    {
      digitalWrite(Led4, HIGH); // LED 4
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led4, LOW);
    }
    {
    
}

     if (keyValue == 7)
    {
      digitalWrite(Led5, HIGH); // LED 5
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led5, LOW);
    }
    {
    
}

     if (keyValue == 12)
    {
      digitalWrite(Led6, HIGH); // LED 6
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led6, LOW);
    }
    {
    
}

     if (keyValue == 1)
    {
      digitalWrite(Led7, HIGH); // LED 7
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led7, LOW);
    }
    {
    
}

     if (keyValue == 6)
    {
      digitalWrite(Led8, HIGH); // LED 8
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led8, LOW);
    }
    {
    
}

     if (keyValue == 11)
    {
      digitalWrite(Led9, HIGH); // LED 9
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led9, LOW);
    }
    {
    
}

     if (keyValue == 5)
    {
      digitalWrite(Led10, HIGH); // LED 10
    }
    else if (keyValue == 16)
    {
      digitalWrite(Led10, LOW);
    }
    {
    
}

     if (keyValue == 10)
    {
      digitalWrite(Led1, HIGH); // LEDSHOW 1 RIGHT ARROW
      delay(100);
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, HIGH);
      delay(100);
      digitalWrite(Led10, LOW);
      delay(100);
      }
    {
    
}

     if (keyValue == 0)
    {
      digitalWrite(Led10, HIGH); // LEDSHOW 2 Left ARROW
      delay(100);
      digitalWrite(Led10, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led1, HIGH);
      delay(100);
      digitalWrite(Led1, LOW);
      delay(100);
    }
    {
    
}

     if (keyValue == 18)
    {
      digitalWrite(Led10, HIGH); // LEDSHOW 3 Up Arrow
      delay(100);
      digitalWrite(Led10, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led1, HIGH);
      delay(100);
      digitalWrite(Led1, LOW);
      delay(100);
      digitalWrite(Led1, HIGH); 
      delay(100);
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, HIGH);
      delay(100);
      digitalWrite(Led10, LOW);
      delay(100);
      digitalWrite(Led10, HIGH); 
      delay(100);
      digitalWrite(Led10, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led1, HIGH);
      delay(100);
      digitalWrite(Led1, LOW);
      delay(100);
      digitalWrite(Led1, HIGH); 
      delay(100);
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, HIGH);
      delay(100);
      digitalWrite(Led10, LOW);
      delay(100);
      digitalWrite(Led10, HIGH); 
      delay(100);
      digitalWrite(Led10, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led1, HIGH);
      delay(100);
      digitalWrite(Led1, LOW);
      delay(100);
      digitalWrite(Led1, HIGH); 
      delay(100);
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, HIGH);
      delay(100);
      digitalWrite(Led10, LOW);
      delay(100);
      digitalWrite(Led10, HIGH); 
      delay(100);
      digitalWrite(Led10, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led1, HIGH);
      delay(100);
      digitalWrite(Led1, LOW);
      delay(100);
      digitalWrite(Led1, HIGH); 
      delay(100);
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, HIGH);
      delay(100);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, HIGH);
      delay(100);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, HIGH);
      delay(100);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, HIGH);
      delay(100);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, HIGH);
      delay(100);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, HIGH);
      delay(100);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, HIGH);
      delay(100);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, HIGH);
      delay(100);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, HIGH);
      delay(100);
      digitalWrite(Led10, LOW);
      delay(100);
    }
    {
    
}

     if (keyValue == 14)
    {
      digitalWrite(Buzzer, HIGH); // BUZZER
    }
    else if (keyValue == 19) //* Key
    {
      digitalWrite(Buzzer, LOW);
    }
    {
    
}

     if (keyValue == 17)
    {
      digitalWrite(Led1, HIGH); // LEDSHOW 4 DOWN ARROW
      delay(10);
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, HIGH);
      delay(10);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, HIGH);
      delay(10);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, HIGH);
      delay(10);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, HIGH);
      delay(10);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, HIGH);
      delay(10);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, HIGH);
      delay(10);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, HIGH);
      delay(10);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, HIGH);
      delay(10);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, HIGH);
      delay(10);
      digitalWrite(Led10, LOW);
      delay(10);
      }
    {
    
}

     if (keyValue == 15)
    {
      digitalWrite(Led1, HIGH); //ENTER key
      digitalWrite(Led2, HIGH);
      digitalWrite(Led3, HIGH);
      digitalWrite(Led4, HIGH);
      digitalWrite(Led5, HIGH);
      digitalWrite(Led6, HIGH);
      digitalWrite(Led7, HIGH);
      digitalWrite(Led8, HIGH);
      digitalWrite(Led9, HIGH);
      digitalWrite(Led10, HIGH);
      digitalWrite(Relay1, HIGH);
      digitalWrite(Relay2, HIGH);
    }
    else if (keyValue == 19) 
    { 
      digitalWrite(Led1, LOW);
      digitalWrite(Led2, LOW);
      digitalWrite(Led3, LOW);
      digitalWrite(Led4, LOW);
      digitalWrite(Led5, LOW);
      digitalWrite(Led6, LOW);
      digitalWrite(Led7, LOW);
      digitalWrite(Led8, LOW);
      digitalWrite(Led9, LOW);
      digitalWrite(Led10, LOW);
      digitalWrite(Relay1, LOW);
      digitalWrite(Relay2, LOW);
    }
    {
    
}
    readKey = false; // reset the flag
    delay(debounce); // debounce
  }
}

void scanRow(){
  for(int j =row1; j < (row5 +1); j++){
    digitalWrite(j, HIGH);
  }
  digitalWrite(rowCounter , LOW); // switch on one row
}

void readColumn(){
  foundColumn = digitalRead(columnCounter);
  if(foundColumn == 0){
     foundCol = true;
  }
  else{
  foundCol=false;
  noKey=noKey +1; // counter for number of empty columns
  }
}

void convertKey(){
  // converts the key number to the corresponding key
  keyString ="";
  keyNum = 99;
  numericValue = false;  
  //
  switch (keyValue) {
    // column A
    case 0:
    keyString = "Left Arrow";
    break;
    case 1:
    keyString = "7";
    keyNum = 7;
    break;  
    case 2:
    keyString = "4";
    keyNum = 4;    
    break;
    case 3:
    keyString = "1";
    keyNum = 1;    
    break;
    case 4:
    keyString = "F1";
    break;
    // column B
    case 5:
    keyString = "0";
    keyNum = 0;
    break;
    case 6:
    keyString = "8";
    keyNum = 8;    
    break;  
    case 7:
    keyString = "5";
    keyNum = 5;    
    break;
    case 8:
    keyString = "2";
    keyNum = 2;    
    break;
    case 9:
    keyString = "F2";    
    break;   
    // column C
    case 10:
    keyString = "Right Arrow";
    break;
    case 11:
    keyString = "9";
    keyNum = 9;    
    break;  
    case 12:
    keyString = "6";
    keyNum = 6;    
    break;
    case 13:
    keyString = "3";
    keyNum = 3;    
    break;
    case 14:
    keyString = "#";   
    break;   
    // column D
    case 15:
    keyString = "Ent";
    break;
    case 16:
    keyString = "Esc";   
    break;  
    case 17:
    keyString = "Down Arrow";   
    break;
    case 18:
    keyString = "Up Arrow";  
    break;
    case 19:
    keyString = "*";   
    break;     
  }
  if (keyNum == 99){
      numericValue = false; // show a non numeric key pressed
  }
  else{
      numericValue = true;  // show a number key pressed
  }
}
Danny van den Brande

http://www.arduinosensors.nl

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

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