At first sight the matrix offers a component saving. Yet we compare the matrix analog vs 1 pin ladder analog hex keypad versions and find they have important differences , more than just price.
Though price was the reason why I built the ladder version for the first time about seven years ago.
Building on a PCB was / is a lot cheaper than buying the 16 key 4 x 4 matix keypad as a commercial product.
Price today, RS catalogue from 35 to 50 euros... not exactly a poor man's dream.
The ladder has almost equal voltage increments/decrements when you press a key above or below your present pressed key . This makes life a lot easier for the PIC decoding software.
Whereas the voltages that correspond to the matrix keys become very cramped at the end of the scale...
useable but cramped.
The matrix of course has the little problem called ghosting. But with a 4 x 4 matrix that has a diode across each switch junction then you lay your ghost to rest.
That is internally we have added 16 diodes
, well we can try to add, accessing those points with a good cutter or maybe we find it is impossible.
Aha you cry, but you haven't said what happens to the ladder ghost !!
Well the ladder has the nice property that the highest key pressed doesn't allow current to get to resistors lower down the ladder.
Not Clear? We can press 9, 7 and 5 ( using three fingers ) The value shown on our display
has to be ????
Yes, it is 9 !!! (Well maybe the equivalent of key 8.8. But our software hits that on the head as impossible and gives you a single 9 value.)
So no ghosts. No diodes, no commercial product and component wise a lot cheaper.
Just before I drop out of the exchange of ideas, there is also the little matter of contact bounce.
Most manufacturers are coy on giving exact figures but when you close a switch, a GOOD switch we can safely assume with 2013 products that the bouncing is over in about 10ms We then can take a reading of the voltage that corresponds to pin 3
The example hex matrix offers a an RC 390 ohm 10nF . Taking the end voltage as 5*RC we get 5 * 3.9e-6 about 20us:
So obviously the RC LPF does nothing to debounce.
increase the R by a factor of a 1000 to 390 kohm. Theoretically yes, as we now have a time of 20ms.But as the PIC analog input is looking for a source resistance of < 2000 ohm it won't work by not correctly mating with the pin input charge RC network.
increase C by 1000, C then is 10uF. Not quite sure how the PIC will react to that either. My gut feeling is, MPUs don't like large capacitive loads on pins in general.
Solution 3. Debounce in software.
This is a good general solution for when you are using a matrix with unknown contact bounce times using the infamous cheapo switches: You can play around anticipating the worst and start using a constant input after say 300ms. .
You read the analog pin i/p , for example every 5ms ... before fine tuning to what cheapo offers.
I'm not knocking cheapo, just advising ' buyer beware'
We also mustn't forget the other side of closing a switch, when we are done we remove our finger we let it open ... all by itself.
This can be real fun. Some measured values with cheapo can be as long as a second, dare I say longer?
To look at some gaphs tryhttp://softsolder.com/201...apacitors-dont-fix-it/
There is also the "too, too big C" stored joules energy problem, which first time round shows as failures because you are eventually soldering the switch contacts together, I say eventually.
Use a good quality switch.
We now know what we can expect from a switch and here paying about 50 cents a switch offers a minimal guarantee of:-
- bounce to open times symmetry.
Then we can use an RC circuit to clean up the rise as well as the fall times. Some values ?:
I have used when battery life is important about 100k ohm which at worst is taking about 0.05mA from the power supply.
To get to the 20msec fully steady state value this means a C of 40nF:
So RC = 100k , 39nF or nearest.
Just a cautionary word on real world scenarios
When and how you read the input.
1. Don't take the sample until you are sure the input signal has stopped bouncing. If you are not sure go to a software debounce routine.
2. 100k is for almost noise free system environments.
If you are using motors in your project, there can be some very large voltages floating around on your wiring .
These may wel,l and will, falsify your input reading.
To get the best of all worlds, good quality switch, biggish capacitor ( with a good low esr ) say sample through 3k3 providing noise immunity and for our 20ms steady debounced voltage we now require a C of about 1uF
Checking :- RC = 3300 * 1 e-6 = 3. 3ms which x5 to 99% = 16ms ( near enough )
To get round loading on the resistive ladder and sourcing to the analog input pin matching, we can use a suitable single supply opamp configured as a voltage follower.
Here microchip offer their MCP601 made for the impedance matching role.
Also we have a slow application, we operate at unity gain so input output tracking is guaranteed. ( famous last words )
I hope I have clarified the possible issues when using a 1 pin analogue keypad.