Connect multiple tact switches to a single microcontroller pin

in how-to by Ian | 8 comments

How to get input from multiple tact switches with a single input pin:

A technique of interfacing multiple tact switches using a single ADC input pin was demonstrated using the PIC12F683 microcontroller. A digital input from four tact switches were individually read through the AN0 ADC channel and displayed it back on an LED. While this technique saves input pins and reduces the size, one downside of the design is detecting combination of switches. The calculations of R’ resistors become more complex in such a case. The resistor values must be chosen very carefully so that the voltage resulting from a switch combination is unique.

Via the contact form.

This entry was posted in how-to and tagged , .

Comments

  1. Theodore Flokos says:

    Well you can always use potentiometers to adjust the resistance in your needs .

  2. Vince Mulhollon says:

    A very close cousin that just needs a digital IO instead of analog input is ye olde R/C time constant w/ capacitors. So you set the pin to output, set to “1″ for awhile, charge up a small cap, switch the pin to input, see how long the cap takes to drain to a “0″, the faster it zeros the smaller the switched resistance across the cap. A “cheap” A/D pin might be 8 bits, but a “cheap” timer could easily be 20 bits, so its possible to have more switches in parallel, or be more accurate or more noise resistant. Be careful not to make the sample times too long, and oversample and average the previous 3 to 9 results for obvious reasons (such as tapping a low value resistor for partial cycle looks like holding down a high value for the whole cycle)

  3. Paul Bonser says:

    @Vince, that sounds like exactly how analog joysticks work (or used to work, I don’t know how they are done these days), but with a potentiometer instead of fixed resistors. Well, joypads used fixed resistors.

    I guess this means that you could read multiple potentiometers from a microcontroller’s digital IO pins regardless of how many analog IOs it has (at the cost of an extra resistor and capacitor per pin.

    Actually, you could just hook each pin through its associated potentiometer to the same capacitor, and then scan through them one at a time. Perhaps one separate pin could be used to quickly charge and discharge the capacitor through a minimal resistor between each read to speed up the whole process.

  4. Ray Moore says:

    The blurb states that calculating the resistors becomes complex when trying to detect multiple inputs simultaneously. Anyone ever hear of something called an R/2R ladder? This gives you a nice smooth progression and only uses 2 values of resistors, with which you can easily get 8 or even 16 inputs from a single ADC input on a microcontroller, depending on the accuracy of your ADC anyhow. 8 bits(inputs) should be very easily doable on most AVR and PIC microcontrollers with ADC inputs.

    • rsdio says:

      I was going to suggest that the resistors are not hard to calculate if you use binary weighting, where each switch voltage is double the value of the previous. Thus, each resistor is double the previous. However, this does require a different resistance for each switch and forfeits potential bulk discounts.

      The R/2R ladder can be cheaper if you’re buying a lot of resistors because there are at most only two values, making it easier to reach discount quantities. You can even double the resistors (series or parallel combination) if you want all to be the same resistance value, but that bumps up the total number of resistors by 50%, so you really need to be manufacturing a large number of units for the total cost to come out lower. Of course, if you’re only making one-off circuits, the cost difference is surely negligible to nonexistent.

      5 switches require 1% resistors in an R/2R network, and more than 5 requires even higher accuracy. If you avoid the R/2R scheme and use different resistances for each switch bit, then you can avoid the problem of needing highly accurate (expensive) resistors.

      Another benefit of the binary-weighted resistor network is that it requires half the number of resistors as the R/2R solution (or 1/3 the number if you’re doubling the same resistance in the R/2R scheme).

      See the Analog Devices MT-015 Tutorial for more details.
      http://www.analog.com/static/imported-files/tutorials/MT-015.pdf

  5. Zach says:

    When I tried this, I noticed a couple things: 1) If the design requires very low power, running the ADC can easily dominate power consumption. 2) It doesn’t get rid of the switch bounce problem, If the sample happens while the switch is bouncing it will add apparent resistance. So, if you take a couple samples, to make sure the ADC is stabilized, and don’t care about the power the ADC uses, it works quite well.

  6. Rohit de Sa says:

    Whoa! Smart comments all around. Will keep these in mind. I’ve learned a lot from this one post!
    Thanks Vince/Paul/Ray/Zach :-)

  7. Ian says:

    While I would agree that the binary-weighted resistor network is certainly easier provided you only want to detect individual buttons being pushed “one downside of the design is detecting combination of switches”. I have no doubt that a binary-weighted ladder could be setup to detect this but fail to see how it could be more efficient than R2R provided you require combinations of buttons…

    Please correct me if I am misunderstanding :)

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.