Aim:
Matrix Keypads are commonly used in calculators, telephones etc where a number of input switches are required. A keypad is a set of buttons arranged in a block or “pad” which usually bear digits, symbols and usually a complete set of alphabetical letters. If it mostly contains numbers then it can also be called a numeric keypad. Keypads are found on many alphanumeric keyboards and on other devices such as calculators, push-button telephones, combination locks, and digital door locks, which require mainly numeric input.
Description:
We know that matrix keypad is made by arranging push button switches in row and columns. In the straight forward way to connect a 4×4 keypad (16 switches) to a microcontroller we need 16 inputs pins. But by connecting switches in the following way we can read the status of each switch using 8 pins of the microcontroller.
The status of each keys can be determined by a process called Scanning. For the sake of explanation lets assume that all the column pins (Col1 – Col4) are connected to the inputs pins and all the row pins are connected to the output pins of the microcontroller. In the normal case all the column pins are pulled up (HIGH state) by internal or external pull up resistors. Now we can read the status of each switch through scanning.
- A logic LOW is given to Row1 and others (Row2 – Row-4) HIGH
- Now each Column is scanned. If any switch belongs to 1strow is pressed corresponding column will pulled down (logic LOW) and we can detect the pressed key.
- This process is repeated for all rows.
The keypad has 8 communication lines namely R1, R2, R3, R4, C1, C2, C3 and C4. R1 to R4 represents the four rows and C1 to C4 represents the four columns. When a particular key is pressed the corresponding row and column to which the terminals of the key are connected gets shorted. For example if key 1 is pressed row R1 and column C1 gets shorted and so on. The program identifies which key is pressed by a method known as column scanning. In this method a particular row is kept low (other rows are kept high) and the columns are checked for low. If a particular column is found low then that means that the key connected between that column and the corresponding row (the row that is kept low) is been pressed. For example if row R1 is initially kept low and column C1 is found low during scanning, that means key 1 is pressed.
Block Diagram
Schematic
Code
// *********************************************************
// Project: Interfacing keypad to Arduino microcontroller
// Author: Hack Projects India
// Module description: Operate keypad
// *********************************************************
#include <Keypad.h>
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 6, 7, 8, 9 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 10, 11, 12 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin 13
void setup()
{
pinMode(ledpin,OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(9600);
}
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '1':
Serial.println(key);
break;
case '2':
Serial.println(key);
break;
case '3':
Serial.println(key);
break;
case '4':
Serial.println(key);
break;
case '5':
Serial.println(key);
break;
case '6':
Serial.println(key);
break;
case '7':
Serial.println(key);
break;
case '8':
Serial.println(key);
break;
case '9':
Serial.println(key);
break;
case '0':
Serial.println(key);
break;
case '*':
Serial.println(key);
break;
case '#':
Serial.println(key);
break;
}
}
}
No comments:
Post a Comment