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:

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

Wikipedia gives a good reference on R-2R networks, that can be used in ADC reading of several switches, or just faking a DAC by using several of just 2 values of resistors. This would allow a more full swing of values over the range of the ADC reading, and more even spacing of those values. Examples seen use 10k and 20k resistors, but I might be inclined to use 1k and 2k values.

Note what the Wikipedia article says about the tolerances of the resistors. On a lot of electronics, you may find a resistor pack in a Single Inline package (SIP) or Dual Inline Package (DIP) chip. The SIP packages will either have multiple resistors all with a common connection at one end or a set of individual resistors. I find SIP resistor networks on old modems boards and other electronics all the time.

I've never measured to see how close in value all the resistors in one network are. There are going to be different tolerances (5%, 2%, etc.) for resistor networks just like for discrete resistors. However, I would think a resistor network all manufactured in one package would have a good chance at being close in value.

Good luck!

A good point, you do have to be aware of the tolerance on resistor values, but fortunately it's easy to measure the larger resistors to within 1% with a properly calibrated multimeter. If you can't find accurate enough resistors you can always use less bits (or in this case fewer switches) connected to a single analog input, but instead use multiple sets of resistors tied to as many analog inputs as you need.

Note to self - don't reinvent the wheel.

I kinda like your setup better. It's simpler, more intuitive and uses fewer resistors...

And generally I think one SHOULD reinvent the wheel once in a while. Who knows.. one might actually make a better wheel :D

Thanks ;)

Like I said this electronics mumbo jumbo still surprises me somewhat. However when I was trying to figure out how to measure the current draw of my servos a while back I became aware that the ADC has a large (100K I believe) resistor build in. So in order to lead the current into the ADC rather than the ground where there is less (~no) resistance, I would need a resistor on the ground. So I came up with this.

Knowing that the peak current draw of the servo was 750mA I calculated that I needed a ~6 Ohm resistor in order to get a full resolution reading on the ADC (5V / 0.75A = 6.666.. Ohm). It turned out 6 Ohm was too much for the servos, so now I'm using a 0.51 Ohm resistor which is large enough to get a useful reading and small enough not to bother my servos.

However I had this on my mind in this case too, but I thought it would be better just to lead all the current directly into the ADC.

I still don't understand why the ground connection is necesary, but hey I believe you :)

Besides you said: "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"

I see why the larger resistors make sense to get larger current drops, but I don't see how your setup increases the current going into the ADC?

And you said: "if all the switches are open you'll get erratic readings since the input will be 'floating'".

I get it. However wont that be the case in your setup too? And couldn't this be solved by adding a permanent resistor in paralel with the switches?

Anyway thanks a lot for the awesome reply. I'll be studying this and trying it out in the days to come :D

Virtually no current flows into an analog input because that would be a bad thing. The ADC would bias the incoming signal by drawing current, plus it would increase the current draw of the circuit. No doubt there are other problems it would cause also.

I chose larger resistors to limit the wasted current flowing through the bias resistor system. The lowest effective resistance from the parallel resistors will be 1.14kΩ, so adding that to the 2kΩ resistor we have a minimum resistance of 3.14kΩ. This means that, in the worst case, only 5V/3.14kΩ = 1.59mA is being consumed by the bias resistors. I could have chosen larger resistors to further reduce the current cost, but if you make them too large you'll make the circuit susceptible to noise problems.

Because the analog input is always connected to the ground via the 2kΩ resistor the output is never floating.

You really helped me to understand a thing or two there :)

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 =)

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.