# Solved: 4 DIP switches on 1 input



## Who's Me (Aug 29, 2006)

I have been trying to figure this out all day and I'm stumped so sorry for another long post with all numbers and math but I know you guys can get me pointed in the right direction. 

I have 4 DIP switches feeding one ADC input on a Picaxe 08m. (+5V to 0Vin shows a variable between 255 and 0)
Switch 4 connects ADC straight to ground which voids any other switch position (goes into programming mode if ADC = 0). So that switch can be ignored.
The three remaining switches give 8 possible combinations of resistance (with all switches open Power comes through Rup and ADC reads 255) so ignoring that.
Now I'm left with 7 possible combinations of resistance.
Optimally my ADC would show:
Switch combination 0(all open) ADC = 255 
Switch combination 1 ADC = 225
Switch combination 2 ADC = 195
Switch combination 3 ADC = 165
Switch combination 4 ADC = 135
Switch combination 5 ADC = 105
Switch combination 6 ADC = 75
Switch combination 7 ADC = 45
If switch 4 is closed ADC = 0 (no matter what other switches say)
With 1K Rup (Gives ADC = 255)
I am trying to pick the values of R1, R2, R3
I have found that ~7.1K Resistance gives me ADC = 225 (equiv. to sw. comb.1)
And 220 ohms Resistance gives me ADC = 46 (equiv. to sw. comb. 7) 
I don't need the ADC numbers to equal exactly what I show, but no matter what combination of resistors I try I always find one or two switch combinations that come way too close together. (sw. comb. 7 may show ADC = 45 but sw. comb. 6 shows ADC = 46 which is only a difference of 1) I would like to keep them separated by like 30 points as I show above but even 10 would be better than 1.

Hopefully I'm on the right track, properly explained what I'm looking for and someone will give me 3 resistor values but chances are I'm looking at this all wrong, way over thinking it and there's an easier way to accomplish what I want.


----------



## e2e8 (Dec 23, 2009)

I would think something like 10 100 1000.
To figure this out, I think you need to think about it in terms of the equations for adding resistances in parallel.
I think a better way would be to make voltage divider behind each switch. 5/2 , 5/4, 5/8 volts maybe


----------



## e2e8 (Dec 23, 2009)

I realize my first suggestion will not work.


----------



## Koot (Nov 25, 2007)

We are given that Sw1 is 7,100 ohms @ 225 ADC and Sw7 is 220 ohms @ 45 ADC. Sw0 is Open or no resistance @ 255 ADC, which is not helpful because it doesn't add a third value. Therefore, we are given only two resistance values. We are also given that each ADC value is linear with a value of 30 points. A quick check tells us that Sw1 with 7,100 ohms @ 225 ADC is 31.56 ohms per ADC point and Sw7 with 220 ohms @ 45 ADC is 4.89 ohms per ADC point. What we have is a nonlinear relationship between two variables - the slope of the resistance curve would show the relationship changes at a nonlinear rate as the ADC value changes, i.e. nonlinear. Can we assume the midway position of Sw4 (between Sw1 and Sw7 & between 225 ADC and 45 ADC) should be 25.48 ohms per ADC point - or 3,440 ohms to give 135 ADC? I wouldn't bet that would be true. It is necessary to have at least one more resistance value (e.g. for 135 ADC) in order to have enough data to work with...to aid in confirming the nonlinear slope at other values. However, it would be best to have all the resistance values for all the ADC values. That said, with the DIP4 switch you're trying to use you only have three choices of resistance that must [then] be used in combinations (with the same three resistors) to offer seven [fairly] exact ADC values. I'm afraid three is not enough to give you anything close to what you need. In fact, mathematically speaking, it would be really lucky to hit desired resistances (whatever they may be) using a DIP4 switch. The odds are really against that happening.

I would suggest using a Resistor Substitution (Decade) Box and determine exactly what resistance is needed for each ADC value. Then, instead of using a DIP4 switch I would use either a DIP8 switch or a tiny micro rotary selector switch with 8 positions (or however many you choose if you want more). That way you can select the exact stand-alone resistance for each ADC position.


----------



## Who's Me (Aug 29, 2006)

With 1K Rup and the following resistances to ground
open = 255 ADC
7.12K = 225 ADC
3.15K = 195 ADC
1.77K = 165 ADC
1.09K = 135 ADC
0.68k = 105 ADC
0.39K = 71 ADC
0.22K = 45 ADC
0 Ohms (DIP4) = 0 ADC
I think that non linearity is what is causing me so much trouble.

I know mathematically there is no way I will ever get exactly these resistance values from 3 resistors, that would be too easy.

However, I don't necessarily care what my ADC number is as long as DIP 4 = 0 ADC.
The other 8 ADC values can be any number between 255 and 10 as long as there is at least 10 points between any two selections. 

Actually it looks like maybe ADC values should be kept somewhere above ~135. (In the lower range it only takes a few Ohms Resistance change to affect ADC number.) So preferably the other 8 ADC values would be any number between 255 and 135 as long as there is at least 10 points between any two.

I really don't want to use a rotary switch or a DIP 8 Because in my head if I have 8 possible switch combinations from 3 DIP switches there has to be a fairly easy way to get 8 significantly different resistances from it. Maybe I need more resistors or to wire it differently, maybe if 1 switch affects Rup....


----------



## Koot (Nov 25, 2007)

You said, "Actually it looks like maybe ADC values should be kept somewhere above ~135." I don't know how you came to this conclusion, but you're probably right. I question the effectiveness of going below 135.

Have you considered using a small resistor trimmer pot instead of a DIP switch with its necessary dedicated resistors for each ADC value?

I wouldn't switch or mess with the value of Rup. The 1K (Rup) resistor is a pull-up resistor to insure that given no other input the circuit assumes a default value and pulls the line high. It also is to limit the amount of current that can flow through the circuit. The resistor might also send a signal to the device that it is connected. The 1K resistor is needed so +5VDC isn't shorted directly to Grd. When switch DIP4 (Sw4) is closed (On) it takes it to the low state with a direct connection to ground. But, it isn't considered a short to Grd because current will flow through the 1K (Rup) resistor to ground, thus limiting the amount of current that can flow to a very small amount.

Now that you have [nonlinear] resistance values for each of your desired ADC points we can calculate the parallel resistance needed for ADC points 135, 105, 71, & 45. You'll need to use your resistance values for ADC points 225, 195 & 165, which are 7.12K, 3.15K & 1.77K respectively, to parallel with other resistors to obtain the desired values. Your choice in how to obtain the lower resistance values needed for ADC points 135, 105, 71, & 45 are as follows:

(The following is assuming that for ADC values 225, 195 & 165 only one switch is in the On position.)

Two DIP switches engaged On:

1.09K (For ADC135): 7.12K (ADC 225) paralleled with 1,287 ohms, or 3.15K (ADC 195) paralleled with 1,667 ohms, or 1.77K (ADC 165) paralleled with 2,840 ohms.

0.68K (For ADC 105): 7.12K (ADC 225) paralleled with 1,752 ohms, or 3.15K (ADC 195) paralleled with 884 ohms, or 1.77K (ADC 165) paralleled with 1105 ohms.

0.39K (For ADC 71): 7.12K (ADC 225) paralleled with 413 ohms, or 3.15K (ADC 195) paralleled with 445 ohms, or 1.77K (ADC 165) paralleled with 500 ohms.

0.22K (For ADC 45): 7.12K (ADC 225) paralleled with 227 ohms, or 3.15K (ADC 195) paralleled with 237 ohms, or 1.77K (ADC 165) paralleled with 252 ohms.

Three DIP switches engaged On:

1.09K (For ADC135): 7.12K (ADC 225) & 3.15K (ADC 195) paralleled with 2,176 ohms, or 3.15K (ADC 195) & 1.77K (ADC 165) paralleled with 28,500 ohms.

0.68K (For ADC 105): 7.12K (ADC 225) & 3.15K (ADC 195) paralleled with 988 ohms, or 3.15K (ADC 195) & 1.77K (ADC 165) paralleled with 1,700 ohms.

0.39K (For ADC 71): 7.12K (ADC 225) & 3.15K (ADC 195) paralleled with 475 ohms, or 3.15K (ADC 195) & 1.77K (ADC 165) paralleled with 595 ohms.

0.22K (For ADC 45): 7.12K (ADC 255) & 3.15K (ADC 195) paralleled with 245 ohms, or 3.15K (ADC 195) & 1.77K (ADC 165) paralleled with 273 ohms.

Note: As you can see, with the necessity of having resistor values for ADC 225, 195 & 165 the parallel resistor values you need to obtain the other ADC points are no where close. There simply isn't enough switching capability with only 4 switches on the DIP switch.


----------



## Who's Me (Aug 29, 2006)

My overall reasoning for keeping ADC values separated by as much as possible is to account for variations in resistor tolerances. As you show 45 ADC = 4.89 ohms fer ADC point. so if I have 5% resistors, with a [email protected]% I could have +-50 ohms and with [email protected]% I could have +-355ohms.
If I'm looking at this correctly, Below the mid-range small variations in resistance can cause large changes in ADC requiring a much larger spread in ADC values.
Therefore the higher I keep the ADC value the more resistance variation it will take to cause swings in ADC, so ADC values should only need a spread of a few points. 

I Don't want to use a pot as there will be no way to read what value you have until you power up and test. (with DIPs I can Know what switch combination is say program 3, power up and know what it will do.)

For the next paragraph I have no idea what I'm talking about, just the few bits and pieces I've gathered.

Rup has something to do with input impedance
Picaxe manual for ADC with pot says max input impedance 20K. I read that as, use max 20K pot so max 20K resistance to V+ or max Rup = 20K.
Higher impedance will have lower accuracy but probably only starts to make much difference once you try to break ADC into much smaller pieces.(As in a temperature sensor where you want to be able to see very small resistance changes.) 
If I were to have 10K Rup ADC should never see more than 10K impedance no matter what the resistance is to ground. I don't know if any of that is correct but I'm only risking a three dollar chip.

Parallel Resistors won't work so I tried series. I rewired the breadboard as shown in schematic and started trying different resistor combinations. I may have found something that works
If Rup = 4.7K
Rdown = 4.7K 
R1,R2,R3 = 2.2K, 4.7K, 10K I get,

sw. pos. 0 = 21.3K to ground and 210 ADC
sw. pos. 1 = 19.1K to ground and 205 ADC, Res. Difference of 2.2K and 5 ADC 
sw. pos. 2 = 16.7K to ground and 200 ADC, Res. Difference of 2.4K and 5 ADC
sw. pos. 3 = 14.5K to ground and 193 ADC, Res. Difference of 2.2K and 7 ADC
sw. pos. 4 = 11.4K to ground and 181 ADC, Res. Difference of 2.2K and 12 ADC
sw. pos. 5 = 9.2K to ground and 170 ADC, Res. Difference of 2.2K and 11 ADC
sw. pos. 7 = 6.7K to ground and 152 ADC, Res.Difference of 2.2K and 18 ADC
sw. pos. 8 = 4.5K to ground and 127 ADC, Res. Difference of 2.2K and 25 ADC

Even though ADC isn't separated by much in the higher range it takes more resistance fluctuation to change the number. I figure if my program considers sw. pos 2 to be between 198 and 202 ADC with a spread of 4 points that's not much different than sw. pos. 7 being between 140 and 163 ADC with a spread of 23 points.

The best I can tell this should work even with small Resistance fluctuations caused by Resistor tolerance, temperature changes, board layout... (I did get 1% resistors)

Although, You will probably tell me where I screwed up, I'll scream, pull out some hair, eat this 4DIP, and rework it with an 8DIP.

P.S. I found a store near me that sells components besides Radio Shack, they only wanted $3.59 for a TO-92 5V reg. and $1.29 each for 1% 1/4W Resistors So if anyone is looking for a deal I can hook them up


----------



## JohnWill (Oct 19, 2002)

Why are you using 5% resistors? 1% resistors are dirt cheap. I stopped using 5% resistors many years ago!


----------



## Who's Me (Aug 29, 2006)

I usually use 5% because I have hundreds of them and I never really built anything that 5% would make much difference on.
I did buy the Radio shack 50 pack of 1% after I figured the range on 5%. and that is what I am using now.


----------



## Koot (Nov 25, 2007)

Who's Me said:


> My overall reasoning for keeping ADC values separated by as much as possible is to account for variations in resistor tolerances. As you show 45 ADC = 4.89 ohms fer ADC point. so if I have 5% resistors, with a [email protected]% I could have +-50 ohms and with [email protected]% I could have +-355ohms.
> If I'm looking at this correctly, Below the mid-range small variations in resistance can cause large changes in ADC requiring a much larger spread in ADC values.
> Therefore the higher I keep the ADC value the more resistance variation it will take to cause swings in ADC, so ADC values should only need a spread of a few points.
> 
> ...


Yes, you should definitely use at least 1% resistor tolerance...or better. Also, you can pre-test the resistors and hand-pick the exact value that best works. There's no reason why your series resistor stepping lattice shouldn't work. I question changing Rup from the [assumed] factory suggested 1K resistance. Your total resistance of 21.3K for Sw 0 is above the maximum (combined 1K at input plus highest resistor) Rup of 20K. Also, as you stated the high resistance will have lower accuracy. I see that your maximum resistance (Sw 0) only gives you ADC 210 whereas you originally wanted two ADC values (255 and 225) higher than what you now have. Obviously you are okay with this narrowed ADC range - you originally wanted 255-45, but now have only 210-127. I assume you're happy with this narrowed range. If so - that's good. Have fun!

PS - Where in NE are you?


----------



## Who's Me (Aug 29, 2006)

I'm using 1% resistors now.
The 1k Rup wasn't from the manual, it came from some schematic I found online for detecting one of multiple switches. which we found didn't work too well for my circuit.
The only thing I found in the manual about impedance was Max. 20K
I did a little more reading on impedance and now I think my assumption on that meaning max. 20K pot was incorrect. The way I understand it now, the impedance my input "reads" should be the (average?) of the two resistances on either side of it. Or max source impedance on 20K pot would be in the middle of it's range and 5K (10K*10K) / (10k+10K)=5K. 
If I am correct, then with the values I show Max. impedance should be with all switches open {Rup * (R1+R2+R3+Rdwn) / (Rup + (R1 + R2 + R3 + Rdwn) = 3.86K 
I'm sure I didn't explain it properly because I really don't know what I'm talking about.
I changed the values around again which spread my values a little more and I think I have a working design now.

The reason I wanted such a wide range was to account for fluctuations in resistance. The range I'm working in would require like 2K error to trigger the wrong selection (I think). so yes I'm fine with narrowed range.

Thank you for your help.

PS I forgot a couple of periods in N.E. I'm actually in DE (zoom way in on your map)


----------



## Koot (Nov 25, 2007)

Very good. Have fun with it!


----------

