The ADC Keypad

 

As you know, a normal 4x3 keypad would use up four outputs and three inputs of a microcontroller. There are four rows and three columns, each row connected to an output and each column connected to an input. As each output is set high, the three input  pins are scanned for a high indicating a button push. By identifying which output and which input was set high, the microcontroller can determine which key was pressed.

This technique works perfectly well. The only drawback is that it requires seven dedicated I/O pins to function. Even using the bigger Picaxes(40x1), this cuts down on your usable I/O which never makes anyone happy. I came across a post one day on the PicAxe forums talking about possibly using one input and a READADC command to read a keypad. The posters explanation in theory was sound. Now of course when I came across that post, I wasnt looking for it. I haven't been able to find it again to see if he has tried to prove it. So, I will.

Here it is. This circuit is the fruit of our labor. By our I mean BCJKiwi and I. We've worked on this project for a couple weeks now as we could both use a one-wire keypad in our individual projects. We've gone back and forth with different ideas to wind up where we are. BCJKiwi is an intelligent guy and working with him was a pleasure.

The circuit is simple. A bunch of 10K resistors chained together with a bunch of taps(much like a multi-tap transformer) between them. A 1M pull-up resistor is used to keep the ADC input of the Picaxe from floating around.

The Build

We recommend using resistors with a tolerance of 1% but 5% may be suitable for some situations. The switches may be of any type with a contact resistance of less than 100Ω when using 1% resistors and 10Ω when using 5% resistors.

What if I only want eight switches for my project?  Not a problem. The circuit is built the same way except only eight switches and ten resistors are used. The resistor values stay the same. It is wired exactly the same. Just remember when changing the number of switches, there must always be a 10KΩ resistor outside of the end switches and of course the pull-up resistor. We've physically tested out to twenty switches with success. However, with twenty switches, a little more drift was observed on the open circuit. Some resistor value adjustment may be needed past this point.

The Code

The build was extremely simple, no resistor values to calculate and no fancy wiring. The code.... is even easier.

A conventional 4x4 matrix keypad uses about 24 lines of code to reach a key value. The ADC keypad, regardless of the number of switches,  requires 2 lines of code to reach a key value. It is so fast, I had to put in a pause in order to not get multiple hits. Here it is:

                                                      readadc 0,b0                             'Use your pin number and variable

                                                      let b1=b0+X/Y                          'Use your variables

                                                      if b1=4 then yada yada yada      ' You have a key value now, use it

 

If a key is pressed, b1 will be a number from 1-(# of sw's). If no key is pressed, b1s' value will be 1 higher than your # of sw's..

Now, the code is pretty generic. The equation however depends on the number of switches being used. It is, however,  unbelievably simple to come up with the equation. BCJKiwi has written an excellent spreadsheet to calculate the numbers needed for the equation. Simply enter the number of switches and the spreadsheet provides everything needed for entry into the code. Here is the equation:

let b1=(b0+X)/Y

where

X=step/2

and

Y=step

Example

Ok, most of this is pretty self-explanatory but why add the half-step?       

Without the addition of the half-step, the calculation provides the theoretical precise number leaving no room for variance in resistor values, switch contacts and leads. If the result in the real circuit produces a value of 15.99 instead of 16, then the PICAXE integer maths would return a result of 15! Adding the half-step ensures the result of the PICAXE calculation always produces the correct key number.

Conclusion

Well that's it. The circuit and the code. So easy to build, so easy to program.

Again thanks to everyone for their input and support.

If your interested in how we got to this point, the concept from beginning to end can be located here.

Home