Matrix Keypad


Many application requires large number of keys connected to a computing system. Example includes a PC keyboard, Cell Phone keypad and Calculators. If we connect a single key to MCU, we just connect it directly to I/O line. But we cannot connect, say 10 or 100 keys directly MCUs i/o. Because It will eat up precious I/O lines and there will a great mess of wires from MCU to Keypad interface. We want to avoid all these troubles so we use some clever technique. The technique is called multiplexed matrix keypad. In this technique keys are connected in a matrix (row/column) style as shown. 

So, when sees a keyboard with more than 100 keys on it and if you are not familiar with the key matrices, then you may think that inside this keyboard, there is a chip (probably a microcontroller) that has at least the same number of inputs to read each key separately. Well, this is far from true. :) In reallity, there are number of SPST (single pole single throw switches) which are connected as shown in the image. 

Matrix keypad comes in different sizes. Like, this one is 4x4 buttons. 


Now, as you can see that, if normally you had to use 8  SPST switches, then you must have used atleast 16 IO pins, but in this case, we will only use 8 pins of MCU to use all these buttons individually. But to get this advantage, we have to program this arrangement of keys.

Keypad Working logic

Before proceding to programming, i should tell you about the working principal of keypad martix. To make you understand  the operation principle, i have selected an image which will also show connection pins to each row and column wire. Then, i will give power to only one column, the column B. The wire that is red, indicates that it has power, and the button that is purple indicates that the button is pressed. Then, i will simulate a button press to button number B3:

Watch the above animation. The column wire B has power all the time. No other wire has power, until the button B3 is pressed. This button makes contact between the column B and the row 3. Because column B has power, the row 3 will also have power as long as the button B3 is pressed! What this means it that, if we know which column has currently power, and we watch the rows, then we can understand which button was pressed, if we detect power on a row! If for example we know that the column B has power, and we detect also power to row 3, then we understand that the button B3 is pressed.



So, how does a matrix finally works? If you have understand the previous example, then it will be very easy for you to follow. The matrix is controlled by a microcontroller. For the above 16-button 4x4 matrix, 8 pins of the micro controller will be used. The first 4 pins will be OUTPUTS and will be connected to the COLUMN wires, while the other 4 pins will be INPUTS and will be connected to the ROW wires. The OUTPUTS of the microcontroller will NOT all have power at the same time. The outputs will go high one by one in cycle. This happens many times per second, but i will slow things down.

So,this is -in VERY VERY slow motion- how a typical microcontroller would cycle its outputs. During this time, it will also monitor the inputs for a signal. As long as all inputs are LOW (with a pull down resistor or with internal uC pull-down resistors), the uC will take no action. Now, suppose that the operator presses the button 3C. Look what happens:

The microcontroller loops its outputs normally. The operator has pressed the C3 button. This button has connect the matrix col C, with the matrix row 3. When the output C of the microcontroller becomes HIGH, the signal arrives also at the input 3 of the microcontroller, through the pressed button. The uC monitors the 4 inputs and detects that when the specific output (C) is high, there is a HIGH signal at the 

input 3. So, this means that the input C3 is pressed! Easy it is not?

Circuit with 8051 Mcu

With 8051 MCu, or even any microcontroller, we need pins according to size of matrix keypad. For an instance, if we take example of 4x4 matrix keypad, then we will need 8 pins. 

So with NxN pad we can use NxN switches with Nx2 pins of any MCU. Now, N pins will be inpout and N pins will be output. as you can see in figures. 

For actuall circuit diagram, you can consider the 8051 mcu connection image with mat-keypad.  as you can see connections are done.

Code explaination

To start with code, firstly i will suggest you to read my inroduction article for programming in 8051 MCu. Well, i consider that you already know about basic programming skills. Now, i guess you have enough knowledge to build your logic about functioning of matrix keypad. Its like switching the output 

column ABCD and checking input lines/rows.

Lets, have a look over the code snapshot. Here, you can see that in the initall() function, col1 to col4 are made input (its opposite to as explained above, but this does not matter, you should only have 4 inputs and 4 outputs). you can also make whole port 0 with 8 pins configured for matric keypad, by giving hex value of 0x0F which is 4 0000s and 4 1111s, that is 0s for output pins and 1s for input pin and you can connect either column or row of your keypad to input. All depends on your algorithm of finding the pressed button. 

I have commented that hex value initialization port and followed the sbit confugration, because hex value initialization only works for 4x4 size as a port ocntain 8 pins. For greater sizes, this will fail. So. follow the individual pins initialization always.

Now, look at the key_input function. Firstly, it has a return type of char means, it will return the value of button pressed. here, you can see the loop, which is giving a pulse to all output pins and making them low in TDMA fashion. This is achieved by rotation operator in c (>>) which is rotating the not value of 0x80, and hence 01111111 value. Its basically giving low to activated row. (Remember that simple input button works as active low in this case because i am checking the button with 0/low value for pressed).

After that, in if statement, there is a checking of button pressed and on the basis of which row was activated at the time (carring 0 value) is used to calculate the button. This is done by adding value of k variable (which is holding active row at an instant) to the index of column as an offset.

Remaining statements are normal like while(col1==0); to hang till release of button. You can implement it in different way. I leave that up to you. signal variable is used to show pressing of ANY key through outthe board, hence that is also included.

To compile and burn this program of matrix-keypad at your home, just read my article of basic intro and basic setup of 8051 microcontroller. There, you will find all information on how to compile and burn. I hope that this article helped you regarding key-pad matrix and if you have any query, please feel free to contact me.