Electronic – arduino – 10-bit digital representation with SPDT switches and voltage division

adcarduinovoltage divider

I'm completely new to electronics, and have been trying to come up with a way to connect 10 SPDT switches to a single analog pin on my Ardiono Mega 2560 (and off course connected to 5V and Ground). The idea is to enable as many switches on a single pin, to try and free up pins for other uses.

I've put 10 switches in series, with unique resistor values on one pole of each, and in this way, trying to represent the bit values, 1,2,4,8,16,32,64,128,512, which will give me a resolution of 0-1023 integral values, which is the range that I understand the ADC on the Arduino chip supports. The voltage divider works, and gives the correct voltage, but ONLY when a single switch is flipped.

My circuit looks as follows:

enter image description here

Here we can see that the last switch is thrown, and the output voltage is 2.5V, which corresponds to a digital value of 512 as expected. However, when using more than one switch, the voltage drops more, giving me the inverse result of what I'm looking for.

I don't know if what I'm showing here makes sense, but ideally, I want to be able to detect the position of any combination of switches, by converting the voltage to binary and work from there.

Best Answer

This isn't going to work. Between the accuracy of your resistors and the accuracy of the A/D, you're not going to get exactly one known reading for each unique switches setting. Not even close.

In general the way to produce a set of evenly spaced analog values from different switch settings is to do what most D/As do, which is to use a R-2R ladder. I have to do something else now, so don't have time to get into what a R-2R ladder is, maybe later. However, this is a well known construct and you will easily find lots about it out there.

Carefully look at your D/A error budget and that of the A/D. With .1% resistors and depending on the absolute accuracy of the A/D, you might be able to resolve 8 switches if everything works out right. 7 or 6 switches sounds a lot more doable. 10 is definitely in the guaranteed to fail range.