How to Use a Matrix Keypad With Ardunio

by PaperAirplaneandThings in Circuits > Arduino

2778 Views, 17 Favorites, 0 Comments

How to Use a Matrix Keypad With Ardunio

576ddcfb67400c734900062e.jpeg
IMG_7479.jpg

Matrix Keypads are cool! It's like a keyboard that can easily be used to interface with your Arduino for various different projects, from combo locks to calculators to wireless remotes and extra buttons on your keyboard, these certainly are very useful. Today we're going to explore how to interface them with the Arduino, using the power of arrays and for loops to do all the work.

Understanding How a Matrix Works

576dce5e4936d46ec3000350.jpeg
IMG_7477.jpg

A matrix is a very simple, yet fundamental part of the keypad. It tells the micro controller, the Arduino in our case, what button is pressed using a very logical method. First, the Arduino will set a pin assigned to a Column to HIGH and search through the row pins to see if one of the row pins reads HIGH. If so, we know what column it is in because we set that pin to high, and we know what row it is in because we read that row as HIGH. Therefore, if we know the row and column, we know what button was pressed. The Arduino will individually set each column to HIGH many times per second and will also read the state of each row many times per second. So, by continuing this cycle very quickly, the keypad becomes fairly accurate and quick to respond. That, is a very simplified version of how a keypad matrix works.

Here is a more detailed(and a better explanation) of how a keypad matrix works:

http://pcbheaven.com/wikipages/How_Key_Matrices_Works/

The Wiring

576dd2df45bceb45950011df.jpeg
IMG_7483.jpg
IMG_7484.jpg
IMG_7485.jpg

The Wiring for this project is very simple. All you need are these parts:

x1 Matrix Keypad(4x4)

x1 Arduino and compatible Arduino IDE(Nano used in this 'ible)

x9 Jumper Wires(preferably 4 of two different colors and one ground wire)

x4 220 Ohm Resistor(Pull-Down resistor on the input)

x1 Breadboard

x1 Multimeter(Not completely necessary but helpful in the beginning)

Let's start with the hard part and pinout of your matrix keypad- the keypad should have 8-9 pins, 4 of which are rows and 4 of which are the columns. If you have a nine pin matrix, the extra wire should be marked and you can ignore that. To find the pinout of the keypad and you can't find a data sheet , set your multimeter to continuity mode and test the pins while pressing the buttons. Test the first and fifth pins, the multimeter should read continuity when one of the corner buttons are pressed(first row first column). Once you have found the row and column pins, you are done with the hard part. But first, PLEASE MARK THE CONNECTOR. It is so very annoying when the connector flips over because of the wire and you lose track of the pins. Save yourself some time and mark it!

Next, connect the row pins to pins D2-D5 of the Arduino, and column pins to pins D6 to D9. Then, connect 220 Ohm resistors from the row pins(D2-D5) to ground(GND) via the breadboard. That is essentially all of the circuitry, so let's go code it!

Coding the Keypad Matrix

The code is fairly simple- all it does is set the column outputs HIGH one after the other and scans the rows for any signs of a button press. If it detects a button press, it calls on the array for the column and row of the button press and prints the character on the pressed button. One thing to keep in mind is that you can change the size and layout of your matrix by simply adjusting the array and pinouts. The program prints the button press to the Serial Monitor directly. Here is the code:

void setup() {
// put your setup code here, to run once:

for(int IN = 2; IN < 6; IN++) {

pinMode(IN, INPUT);//Initializes the pins }

for(int OUT = 6; OUT < 10; OUT++) {

pinMode(OUT, OUTPUT);//Initializes the pins

}

Serial.begin(9600); }

void loop() { char Matrix[4][4] = { {'1', '2', '3', 'C'},

{'4', '5', '6', 'D'},

{'7', '8', '9', 'E'},

{'A', '0', 'B', 'F'} };//2D array for the keypad. Edit to change the layout of your //keypad

for (int column = 6; column < 10; column++) {

digitalWrite(column, HIGH);//Setting the column pins to HIGH in a sequence

for (int row = 2; row < 6; row++) {

int rowState = digitalRead(row);// Scanning through all the row pins

if(rowState == 1) {//If a button press is detected

Serial.println(Matrix[row-2][column-6]);

digitalWrite(column, LOW);

}

}

}