In the previous post – Matrix Keypad with Atmega8 – part 1, we discussed why a matrix keypad is useful and how we can use and interface it with an ATmega8. We will move on with out discussions.
In the last part – Matrix Keypad with Atmega8 – part 1 we discussed the need of a matrix keypad and how it works. Lets take the discussions forward. Here we will see why we need to make the columns HIGH Impedance and I will also share the code with you.
Why make other Columns High Impedance while one column is made LOW?
The way we have designed our solution is that we are making one column LOW while others are in TRISTATE. This gives rise to a common question – Why? Why the other columns in TRISTATE? Why not HIGH?
The questions are very valid and lets examine.
Lets say we selected column number C1, so we make it LOW(i.e. GND or logic 0) and at the same time we make all other columns high impedance (i.e. input).
If we don’t make other lines high impedance (tristate or Input) they are in output mode. And in output mode they must be either LOW(GND or logic 0) or HIGH (5v or logic 1). We can’t make other lines LOW as we can select only one line at a time and C1 is already low in this assumption. So the only other possible state is all other columns are HIGH. This is shown in figure below. Red colour on column indicate high state while green is for low state.
Now lets see what happen when a user presses two keys at once – say keys 1 and 2
Do you see what happens? It leads to a short circuit from the HIGH C2 to HIGH C1 which will burn the buffer of the MCU immediately.
Hence, to prevent it from happening, all other columns are kept at tristate(neither LOW nor HIGH) but very high input impedance that prevent either source or sink of current from them. So if we kept C2 at high impedance state it wont allow current to flow to GND on C1.
To help you out, a code file is placed here: Matrix_keypad.c
The code displays which key is pressed on an LCD. You will have to use the LCD libraries as well. You can download the libraries from here.
In the code, the connections are also explained. I suggest you try to understand the connections. In case you are not able to understand or have any doubts, do let me know in the comments below. Will be happy to help.
Vivek is a Senior Embedded Engineer at Robert Bosch. He has been working on Embedded Systems for the past 10 years. He loves to share his knowledge and train those who are interested. Nerdyelectronics.com was started out of this interest.