Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

27 June 2010
Author: Giorgos Lazaridis
How a Key Matrix Work

A typical PC keyboard with 109 keys

This is all the PCB that hides inside a keyboard. Only 26 connectors are used to interface all 109 keys! Thanks to the matrices.

What you probably have in front of you, is a keyboard with more than 100 keys on it... 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...

What are the key matrices?

The matrices are actually an interface technique. It can be used to interface inputs like the PC keyboard keys, but also to control multiple outputs like LEDs. According to this technique, the I/O are divided into two sections: the columns and the rows. You can imagine a matrix as an excel sheet. Here is a 4 x 4 matrix

The blue lines are the columns and the red lines the rows. There are 16 knots that the rows and columns intersect. The columns and the rows are NOT in contact! Suppose that we want to make a key matrix. To do this, we will have to connect a button to each knot. The buttons will have a push-to-make contact. When the operator pushes this button, it will connect the column and the row that it corresponds to. Now i will put the push-to-make buttons onto the matrix

The buttons are named with the Column:Row name that they connect. For example, the top-left button is named A1 and the bottom right is named D4.

How does a key-matrix works?

To understand the operation principle, i will re-draw the above matrix without colors. I will also put 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.

A matrix in the real life

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?

Pressing multiple buttons on a matrix

Pressing multiple buttons simultaneously on a matrix is not always a good idea. There are situations that the matrix operates normally, but not always. Suppose for example that someone pressed the buttons B1, B2 and B3 simultaneously. What will happen? Let's take a look:

When the output B becomes HIGH, then the three inputs 1,2 and 3 of the microcontroller will also become HIGH. Supposing that the firmware is written in a way that can handle such an event, the operation will be carried out normally. The uC will understand that the buttons B1, B2 and B3 are pressed. Let's see another situation where the buttons A3, B3 and C3 are pressed:

In this situation, the matrix will also work normally. The microcontroller will detect a signal at input number 3, when the output A, B and C are HIGH. When the output D is HIGH, no signal is detected.

The ghosting problem

I will continue the above examples with another situation, where the buttons C2, B2 and B3 are pressed simultaneously:

When the output C is HIGH, then the input 2 will also become HIGH. The uC will understand that the button C2 is pressed. But something else happens here. The button B2 is also pressed! This means that the HIGH signal will go through the button B2 to the column B. And due to the fact that the button B3 is pressed, the signal will arrive simultaneously at input #3 as well! But the microcontroller knows that at this moment, only the output C is HIGH, and because it detects HIGH signal at inputs 2 and 3, it will think that buttons C2 and C3 are pressed, something that is wrong! The button C3 is NOT actually pressed! This is known as ghosting, and usually gives a headache to PC gamers, especially when the game requires multiple buttons to be pressed simultaneously. Take for example the MAME console, which simulates arcade games. If you play samurai shodown 1v1, and one player has low defense and kicks while the other is flying high and uses the sword, this will require 6 keys to be pressed! Yawks! Ghosting occurs. (that's why i made my MAME arcade console)

P]The masking problem comes in continue to the ghosting. When masking is occurred, the controller cannot detect a key change that has occur. Suppose for example that the ghosting problem has occur, by pressing C2, B2 and B3 simultaneously, like the previous example. The controller will think that the button C2 C3 is pressed, although the operator has not pressed C3. Now, the operator presses the button C3 without releasing any other button. Nothing changes at the microcontroller. And now the operator releases the button C2... What happens? Nothing! The microcontroller still thinks that C2 is pressed and cannot detect the button release! This is the masking problem, also a gamers headache.[/P]
 By pressing buttons C2, B2 and B3 simultaneously, the ghosting problem occurs. The operator presses button C3, yet nothing changes Now the operator releases button C2. Still nothing changes and the uC cannot detect the button release! Masking has occur.

Any solution to the ghosting and masking?

Yes there is! And it is a cheap one. Using a diode for each button, the masking and ghosting problems are instantly solved! This is how it works:

The above situation would normally cause ghosting. Yet, the diodes prevents the current to go backwards. For better understanding, you can see the 3 steps of the above animation here (as always, click to enlarge images):

Because the cycling speed must be quite fast (some Khz), you should choose proper diodes. A cheap solution is the 1N4148 general purpose diodes. If you plan to have many keys and thus you need faster sampling, then select a schottkey diode instead.

This is also a very interesting chapter with matrices. Using matrices, you can control for example 9 LEDs each one separately, with only 6 outputs. Look how it works:

Above there is a typical 3 x 3 LED matrix. All microcontroller pins, for both columns and rows are now OUTPUTS. Suppose now that all outputs are HIGH. All LEDs will have HIGH on both anode and cathode, and this means than none LED will light! This is how to turn completely off the LED matrix, by giving either HIGH or LOW to all outputs. Now suppose that we want to turn on the middle LED. To do this, we give HIGH to column 2 and rows 1 and 3, and LOW to column 1 and 3 and row 2. This is what will happen:

The middle LED is forward biased and therefore it lights. All other LEDs are either reverse biased, or they have the same voltage (positive or negative) to their both leads, and therefore they do not light. This way, someone can control for example 64 LEDs each one separately, with an 8x8 matrix, using only 16 outputs from a microcontroller.