Matrix Keyboard

Matrix keyboard is an input device that contains different switches that can generate a unique HEX code that can be read by the computer, and the corresponding switch pressed can be identified with the leveled value on them.

Add images.

Working of the Matrix Keyboard:

The matrix keyboard consists of 16 buttons which are nothing but switches. These buttons are connected as rows and columns. As all the buttons are set in as rows, therefore as per the number of rows an columns, an ASCII code is generated corresponding to the row or column index of the button.

Applications of Matrix Keyboard:

Due to their simplicity, these kinds of keyboards can be used in different technological applications. Some applications include:

  • Automated Machines.
  • Industrial Machines.
  • Washing machines.
  • lifts.
  • etc.

Matrix Keyboard with Arduino:

Due to their simplicity, a matrix keyboard can be used in different prototype projects such as- Robotics, machines designed, wireless communication projects, etc.

Components Required :

  • Arduino Uno.
  • 4*4 Matrix Keyboard
  • Jumper wires.
  • Bread Board.
Displaying the button pressed on the Serial Monitor

Connecting the Matrix Keyboard into the Arduino :

See the matrix keyboard as shown in the below diagram. As the pins are not numbered, so we shall consider the serial number from the right to left of the keyboard.

  • Connect the pin 1(starting from right) of the Matrix keyboard into the PIN 2 of the Arduino
  • Connect the pin 2 of the keyboard into the PIN 3 of the Arduino.
  • Connect the pin 3 of the keyboard into the PIN 4 of the Arduino.
  • Connect the pin 4 of the keyboard into the PIN 5 of the Arduino.
  • Connect the pin 5 of the keyboard into the PIN 6 of the Arduino.
  • Connect the pin 6 of the keyboard into the PIN 7 of the Arduino.
  • Connect the pin 7 of the keyboard into the PIN 8 of the Arduino.
  • Connect the pin 8 of the keyboard into the PIN 9 of the Arduino.

arduino-keyboard-matrix

Now edit the following code into the Arduino Editor :

#include <Keypad.h>
const byte ROWS = 4; 
const byte COLS = 4; 
char hexaKeys[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 customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
void setup(){
  Serial.begin(9600);
}
  void loop(){
  char customKey = customKeypad.getKey();
   if (customKey){
    Serial.println(customKey);
  }
}

Program Analysis :

  • #include <Keypad.h>, this is the library for the keypad module which has to be imported explicitly from the Arduino library.
  • const byte ROWS = 4; , ROWS is the variable that is going to represent the rows in the matrix.
  • const byte COLS = 4;, COLS is the variable that is going to represent the column in the matrix.
  • char hexaKeys[ROWS][COLS], declaring a two-dimensional array to represent a specific button.
  • byte rowPins[ROWS] = {9, 8, 7, 6}; , the rows variable is assigned with pins 6, 7, 8 and 9 respectively.
  • byte colPins[COLS] = {5, 4, 3, 2}; the column variable is assigned with pins 2,3 4 and 5 respectively.
  • Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); , customKeypad is the Keypad object variable declared which can be used to access and read the Keypad values.
  • Serial.begin(9600); , the serial monitor port starts transferring at the rate of 9600 bits per second.
  • char customKey = customKeypad.getKey();, getKey() is the function to get the value from the Keypad through the object variable customKeypad. This key will be stored in the customKey variable as a character variable.
  • if (customKey), it returns true if a key is found.
  • Serial.println(customKey); , prints the tapped key into the serial monitor of the Arduino IDE.

arduino-keypad-value-serial-monitor

Project-Servo motor with unique Key

Aim of the Project :

To rotate a servo motor only when a certain key is tapped in the Keypad.

See the live demo of the project here.. Click here to see

Components Required :

  • Arduino UNO Board.
  • 4 * 4 Matrix keyboard.
  • Jumper Wires.
  • Bread Board(You may use it, but not compulsory for this project)
  • Active Buzzer
Connecting the Keypad:

arduino-keyboard-matrix

See the matrix keyboard as shown in the above diagram. As the pins are not numbered, so we shall consider the serial number from the right to left of the keyboard.

  • Connect the pin 1(starting from right) of the Matrix keyboard into the PIN 2 of the Arduino
  • Connect the pin 2 of the keyboard into the PIN 3 of the Arduino.
  • Connect the pin 3 of the keyboard into the PIN 4 of the Arduino.
  • Connect the pin 4 of the keyboard into the PIN 5 of the Arduino.
  • Connect the pin 5 of the keyboard into the PIN 6 of the Arduino.
  • Connect the pin 6 of the keyboard into the PIN 7 of the Arduino.
  • Connect the pin 7 of the keyboard into the PIN 8 of the Arduino.
  • Connect the pin 8 of the keyboard into the PIN 9 of the Arduino.
Connecting the Servo Motor:

arduino-servo-connection-for-keypad

  • Connect the plus(+) pin of the servo motor into the 5v pin of the Arduino.
  • Connect the minus(-) pin into the GND pin of the Arduino.
  • Connect the data pin of the servo motor into the PIN 11 of the Arduino.
Connecting the Active Buzzer
  • Connect the plus(+) pin of the buzzer into the PIN 12 of the Arduino.
  • Connect the minus(-) pin of the buzzer into the GND of the Arduino.

arduino-servo-and-buzzer-for-keypad

Now Edit the following code into the Arduino Editor :

#include <Keypad.h>
#include<Servo.h>
Servo myservo;
const byte ROWS = 4; 
const byte COLS = 4; 
char hexaKeys[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 customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 
void setup(){
  Serial.begin(9600);
  myservo.attach(11);
  pinMode(12,OUTPUT);
}
  void loop(){
  char customKey = customKeypad.getKey();
   if (customKey){
    Serial.println(customKey);
    if(customKey=='6')
    myservo.write(180);
    else if(customKey=='9')
    myservo.write(0);
    else if(customKey=='3')
    {
    digitalWrite(12,HIGH);
    delay(100);
    digitalWrite(12,LOW);
    }
    }
}

Program Analysis :

The program is the same as that of the above. Just the added things are:

  • include<Servo.h> for including the servo library.
  • Servo myservo;, declaring an object variable called myservo to create an object to access the data from the servo.
  • myservo.attach(11); , the function attach is used to indicate that the PIN 11 is designated for the servo functionalities.

Now, inside the function void loop(), we have set some conditions :

  • if(customKey=='6'), then myservo.write(180); is the statement which will be executed, which is nothing but to rotate the servo shaft left by an angle of 180 degrees.
  • else if(customKey=='9'), then myservo.write(0); is the statement which will be exected which means rotate the shaft back to 0-degree position.
  • else if(customKey=='3'), then digital.write(11, HIGH);, is the statement that will be executed, which means to activate the buzzer alarm, indicating that key 3 is pressed which is the wrong key.

FAQ

Can we use the keypad in different kinds of projects such as password-controlled door, lift controller, etc?

Yes! The Matrix keypad can be used to implement many input based projects.

What do I need to run a heavy motor with Arduino and keypad?

The procedure to use the keypad will be the same, but to run a motor that requires more power, a relay can be used.

Comment / Suggestion Section
</