## Best illustration of Logic Behind the Interfacing of Keypad to Microcontrollers

by realfinetime  |  in Microcontroller at  12:47

Switches and sensors are the most common input methods to microcontrollers. If you want to create a calculator using some switches, a matrix of switches is used. Switch matrix should be used in such a way that, minimum number of digital pins of microcontroller is using. If you want to connect sixteen switches to a microcontroller, in normal case, sixteen digital pins are used. If the switches are arranged as a matrix as shown below, only eight digital pins are used. Number of digital pins required becomes more relevant if the size of matrix is bigger.

R1 = R2 = R3 = R4 = R5 = R6 = R7 = R8 = 1KOhm

Algorithm for detecting the key that is pressed

1. Make Port 2 as an input port.
2. Make Port 1 as an output port.
3. Send LOW ( 0 ) through Port 1 ( A, B, C and D ).

4. All the switches will be off by default. That is, Port 2 ( E F G H ) will be pulled to 5V through the pull up resistors ( R1, R2, R3 and R4 ) and Port 2 ( E F G H ) will read 1 1 1 1 since it is an input port.

5. When any switch is pressed, corresponding column ( E, F, G or H ) get grounded. For example, if S6 is pressed as shown in the following image, F get grounded through the switch ( S6 ) and B, since A, B, C and D are at LOW voltage ( 0V ). Then Port 2 will read 1 0 1 1 ( E F G H ). Since F reads 0, microcontroller will assume that the switch pressed will be either one of  S2, S6, S10 or S14. Next is to find the pressed switch from S2, S6, S10 and S14. Read the following procedures to select the pressed switch from these switches.

Send LOW through A and HIGH through B, C and D. If F reads 0, then pressed switch will be S2. Because, if the pressed switch is S2, F will read LOW from A through S2 since A is LOW. If S2 is not pressed, F will read HIGH through pull up resistor R2. In our circuit F will read HIGH, because we have already mentioned that pressed switch is S6. Eliminate S2. Remaining switches are S6, S10 and S14.

Send LOW through B and HIGH through A, C and D. If F reads 0, then pressed switch will  be S6. Because, if the pressed switch is S6, F will read LOW from B through S6 since B is LOW. If S6 is not pressed,  F will read HIGH through pull up resistor R2. Since S6 is pressed in this circuit as we have already mentioned, F will read 0 and microcontroller will conclude the pressed switch as S6.

Send LOW through C and HIGH through A, B and D. If F reads 0, then pressed switch will be S10. Because, if the pressed switch is S10, F will read LOW from C through S10 since C is LOW. If S10 is not pressed, F will read HIGH through pull up resistor R2. In our circuit F will read HIGH, because we have already mentioned that pressed switch is S6. Eliminate S10.

Send LOW through D and HIGH through A, B and C. If F reads 0, then pressed switch will be S14. Because, if the pressed switch is S14, F will read LOW from D through S14 since D is LOW. If S14 is not pressed, F will read HIGH through pull up resistor R2. In our circuit F will read HIGH, because we have already mentioned that pressed switch is S6. Eliminate S14.

From these analysis, microcontroller will conclude that pressed switch is S6.

## IMPORTANT NOTICE

All the circuits, published in this blog is only after testing and getting proper results in my private lab. When you try these circuits, you should check the supply voltage, polarity of components, presence of childrens nearby and shorts in the circuits. This website will not be responsible for any harm happened to you or your components caused by your carelessness.