## Sunday, 3 February 2013

### interfacing 4*4 matrix keypad to Microcontroller

What is matrix key pad?

Ans:  A matrix key pad is a two dimensional arrangement of switches in row and columns. This arrangement follows an algorithm to get the status of the keys. Here comes the questions:
How a switch works ?
A switch is nothing but two conductive terminals and an adjustable contact that connects the two terminals. So that one can adjust a the position of the switch manually to make circuit "OPENED"  or "CLOSED"

This switch  is called a "Tact switch",which is generally used to make matrix keypads. it consists of four terminals but opposite pins are shorted internally.
How to connect the switch to a microcontroller?
Before that don't forget that a switch simply "closes" or "opens" a circuit.
But a microcontroller is a digital device that only understands "ZERO(0V)" or "ONE(5V)".
so we need a circuit that indicate the status of a switch in turms of "0" or '1"....
here it goes..... working of the circuitry is left to the reader's assignment
if switch close=1 and switch open =0 then it is called active high logic
if switch close=0 and switch open =1 then it is called active low logic

• Reader must Google about the concept of    "Pull-up" and "Pull-down" resistor
• The resistor in the above circuit prevents the              circuitry from "short circuit of  Vcc and Gnd" when the switch is pressed.

Now we go for the matrix key pad arrangement. Take a look at the figure below. the keys are arranged in   four rows and four columns.
So we got 4+4 pins to interface this matrix key pad to the microcontroller.
Now how to check weather a switch is pressed or not....

The basic algorithm for matrix key pad ....
Let us assume the row pins and columns are connected an 8 pin port
7       6      5      4      3      2      1     0
R1   R2     R3    R4    C1   C2   C3   C4

1. Configure all row pins of microcontroller as outputs and column pins as input .(7,6,5,4 are output and 3,2,1,0 are input.)
2. pull -up all column pins
3. write '1's to row pins so all row pins are at high state.
4. make R1 pin 0 and remaining pins high
5. scan each column pin (if any key is pressed in this row then that particular column pin will be zero)
6. make R2 pin 0 and remaining pins high
7. scan each column pin (if any key is pressed in this row then that particular column pin will be zero)
8. make R3 pin 0 and remaining pins high
9. scan each column pin (if any key is pressed in this row then that particular column pin will be zero)
10. make R4 pin 0 and remaining pins high
11. scan each column pin (if any key is pressed in this row then that particular column pin will be zero)
This is basic algorithm. initialize a variable and return the corresponding value to the variable. This algorithm
will be same for all microcontroller. Some expert developers add some additional hardware to it and makes it more reliable. 