Let's Make Robots!

Multiple swithces to analog input?

Just had an idea. I'm planning to use a LOT of (tactile) switches on a walking robot. For instance I wanna put several under each foot. But it seems like a waste of pins to use a digital pin for each switch (or use multiplexers). So I thought I might be able to connect the switches in paralel and read several at a time from an analog input using resistors of different sizes connected in serial with each switch. Like this:

switches_s.jpg

Each combination should then output a unique voltage (according to my understanding).

Do you think this would work?

(sorry if this question seems silly/banal but I'm still quite a noob to this electronics mumbo jumbo :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
TeleFox's picture

Sorry mate, no dice on that circuit. The current going into an analog pin is very, very low, so by V=I/R we can see that very little voltage will be dropped by the resistor. If any of the switches are closed to micro will detect 5V at the analog input, and if all the switches are open you'll get erratic readings since the input will be 'floating'.
The idea is a good one though, so I've drawn you up a modified version that'll do the trick =)

AnalogMultiInput.jpg

The upper 'switched' resistors form a voltage divider with the lower resistor when one or more switches are closed. If all the switches are open then the lower resistor pulls the output to ground, ensuring you get a definitive zero-volt reading.
The voltage detected by the analog input will be Vin = 5V(1-Rs/(Rs+2kΩ)), where Rs is the effective combined resistance of whichever resistors are currently connected through a closed switch.
The resistors are connected in parallel so the combined resistance is Rs = 1/(1/R1 + 1/R2 + ...) and so on for however many resistors are currently connected. I've chosen the values here based on an educated guess, so they could probably be selected more intelligently, but they'll do the job at least. Here's a table for what Rs and Vin you'll get when different switch combinations are closed. An 'X' means that the switch for that resistor is open, an 'O' means it is closed.

2kΩ          X        O        X        X        O        X        O        O
4kΩ          X        X        O        X        O        O        X        O
8kΩ          X        X        X        O        X        O        O        O
Rs(kΩ)     ∞        2        4        8      1.33    2.67     1.6     1.14
Vin(V)       0       2.5     1.67     1      3.00    2.14    2.78    3.19

Ok, so the input values only go from 3.19V to 1V (not including 0V), which isn't great. Preferably you want them spread out across the 0-5V range as evenly as possible to (1) reduce the possibility of electrical noise messing up your readings and (2) give you the ability to connect more switches to a single analog input. But at least this should give you an idea.