Multiple buttons to one IO pin?
smbaker
Posts: 164
Does anyone have a clever solution to attaching more than one button to a single IO pin? One of the standard tricks with other microcontrollers is to use resistors to turn a collection of buttons into voltage dividers and then sample them with built in A/D. We don't have built in A/D so that is not an option. I wonder if maybe there's a way to throw a capacitor onto a pin and turn it into a cheap A/D converter (set the·dir to output to charge the cap, set the dir back to input, and measure how long it takes the cap to discharge through a resistor that's attached to the button).
I can think of numerous ways to do it with two pins using a parallel-in, serial-out·shift register (one pin for data, one for clock). I·could probably get the shift register idea down to a single pin by throwing an independent clock at it.
I'm looking to support 4-6 buttons.
I can think of numerous ways to do it with two pins using a parallel-in, serial-out·shift register (one pin for data, one for clock). I·could probably get the shift register idea down to a single pin by throwing an independent clock at it.
I'm looking to support 4-6 buttons.
Comments
-Phil
I'll briefly address the analog-to-digital "switch-plex" approach:
Have a look at this page:
www.edn.com/article/CA6360317.html
Download the .pdf here:
www.edn.com/contents/images/6360317.pdf
And the article at the top of this page:
www.edn.com/article/CA56657.html
Download the .pdf here:
www.edn.com/contents/images/2499di.pdf
Keep in-mind, with this analog/switch approach, simultaneous closures (key presses) may result in undesired results; or in-fact desired results!...
For example; if you want to ignore multiple simultaneous switch closures, your code should ignore resulting invalid analog voltages that appear. On the other-hand, you might allow your code to accept some voltages for certain multiple switch closures to add more functions; e.g. pressing two keys simultaneously puts you back one step in a user-interface. Programming to use or ignore multiple switch closures obviously makes things more complex. But this is always the case with any user button/keypad driver.
Also, even with the delays introduced by analog to digital conversion, your code may require delays to de-bounce switch closures.
Lastly, pay attention to the fact that the simple delta-sigma ADC solution outlined in the Propeller AN001 really requires that the passive components be connected VERY close to the physical pins on the Propeller chip. This is stated somewhere in AN001 if memory serves. I've had mixed (mostly poor) results with this approach when trying it with a 40pin propeller on a plug prototyping board. YMMV...
Please do reply at some point and share with us what your application is and what solution worked for you. In the mean-time, there's usually someone here willing to help in near real-time wherever you are.
Best Regards, David (GMT+7)
Use the schematic below. Use different values for the resistors to the switches - like 10k, 22,k, 33k, 47k - and a much smaller value for the resistor to the pin - like 1k. Your button driver checks the pin. If it is logical high it switch to output low to ground for a certain time to discharge the cap, then switches back to input mode and measures the time to logical high input. Time depends on RC-product.
If you use approximately exponentially resistor values you can even distinguish simultaneous presses of several buttons! Build your own polyphonic piano keyboard.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets for tools & toys
this is not a direct answer to your question, but maybe you can have an idea out from this:
http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4944
I use the PCF8575 and 8574 a lot, they are great for expansion i/o. Just be sure to read the data sheet about writing all "1's" to the outputs prior to reading.
Thanks,
Scott