Multiple Switches with one analogue input

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Multiple Switches with one analogue input

Post by medelec35 »

Has any one had success with there own values for R1, R2, R3, R4 & R5.

I would assume binary weighted would be best, but LSB can’t be too high.

Best values would produce a difference of at least 200mV with all permutations.
I can mange 200mV with most but with one permutation there is only a difference of 50mV.

I am looking to have more than one switch closed at a time.
Multi SW Port.JPG
Multi SW Port.JPG (36.19 KiB) Viewed 6939 times
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Multiple Switches with one analogue input

Post by Benj »

Hello

If you are creating a DAC then you will be better of using the R/2R approach.

Resistors normally have a 5% tolerance which means that more then 5% of your analogue end voltage will be inaccurate which will completely illiminate the least significant bit and maybe even the next bit too.

Resistors in a pack will still have 5% tolerance but within the pack they will all be almost identical so the tolerance problem is removed. This is why the R/2R DAC is so great. For the 2R resistors simply use two of your single R resistors.

http://www.avr-asm-tutorial.net/avr_en/AVR_DAC.html

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Multiple Switches with one analogue input

Post by medelec35 »

Thanks Ben. I Did think of R2R fist, but ruled it out since I am using a SPST dip switch. Don't R2R need a SPDTswitch i.e switch to either o/p or short resistor to ground? Refering to the link you posted: If you think about it, bits o/p from a Microcontroller will be either at +5V or GND when using as DAC.
If I am wrong and R2R can use SPST, then I will defiantly use that method!
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Multiple Switches with one analogue input

Post by Benj »

Hello

I think you can get away with using SPST switches with the R/2R system.

Simply connect each switch to +V and then connect the other side of the switch to the bitx as shown on the weblink In my previous post.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Multiple Switches with one analogue input

Post by medelec35 »

Thanks Ben. I will give it a try.
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Multiple Switches with one analogue input

Post by Benj »

Thinking about this I may have been wrong. Let me know how you get on.

Otherwise it may be worth getting low tolerance resistors to eliminate the previous error you were getting. Alternativly you could use multiples of the same resistor value to avoid the tolerance issue.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Multiple Switches with one analogue input

Post by medelec35 »

Code: Select all

msb			lsb	    Voltage
0	0	0	0	    0
0	0	0	1	    0.493
0	0	1	0	    0.704
0	0	1	1	    0.925
0	1	0	0	    0.986
0	1	0	1	    1.234
0	1	1	0	    1.312
0	1	1	1	    1.441
1	0	0	0	    1.455
1	0	0	1	    1.767
1	0	1	0	    1.877
1	0	1	1	    2.025
1	1	0	0	    2.016 < Problem
1	1	0	1	    2.187
1	1	1	0	    2.23
1	1	1	1	    2.321				
R=10k	2R=20k			
		

It nearly works! Have highlighted where the problem one is.
Going back to previous method, just trying to work out which resistors are best values. I know best is R 2R 4R 8R 16R etc. but is the initial R value which would make not make R too large. Finally its what value to make R5? If too low will have more significant effect on 16R.
Martin

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Multiple Switches with one analogue input

Post by Steve »

I've had a look at this and the best I can come up with is a minimum difference of 70mV. To do this, I used the resistors as follows:
R1 = 2k2
R2 = 4k7
R3 = 10k
R4 = 22k
R5 = 1k

Things can be drastically improved if the network of resistors is powered off 10V instead of 5V. Using the same resistor values, the minimum difference becomes 140mV.

A spreadsheet is attached. I hope I got the math(s) right!
Attachments
multiple switches to ADC.xls
(18.5 KiB) Downloaded 310 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Multiple Switches with one analogue input

Post by medelec35 »

Thank you to both of you for your replies. I appreciate it.
Especially thanks to Steve for taking the time to produce a useful spreadsheet.
I will go the 10V supply route, since alreay have a 10V supply to drive 2 EC motors.
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Multiple Switches with one analogue input

Post by medelec35 »

I have just completed a test on real hardware. Measured actual resistor values, and entered them into spreadsheet.

Steve your maths is spot on! I have attached results for anyone to view.
Attachments
multiple switches to ADC(1).xls
(19 KiB) Downloaded 318 times
Martin

Post Reply