1
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Would it be a better option to consider using a shift register as in this tutorial: arduino.cc/en/Tutorial/ShiftIn \$\endgroup\$ – josef.van.niekerk Oct 24 '12 at 12:50
  • \$\begingroup\$ I'll read up on R-2R ladders on WikiPedia, I did see an article there which seemed quite detailed. \$\endgroup\$ – josef.van.niekerk Oct 24 '12 at 12:51
  • \$\begingroup\$ The problem is that the tolerance of the biggest resistor will make the smallest useless: you can address this issue by measuring each resistor with an ohmmeter of the resolution needed for the D/A conversion. \$\endgroup\$ – clabacchio Oct 24 '12 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.