Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading DIP switches using few GPIO pins

Status
Not open for further replies.

geekEE

Electrical
Feb 14, 2005
412
I'm trying to read a few 4-bit rotary DIP switches in a microcontroller in the most economical way possible. I'd like to minimize the number of GPIO pins used so that I don't have to buy a larger chip than absolutely necessary. I thought of two different ways and I wanted to see if anyone had any other tips or tricks to share.

1. I could use shift registers like the 74HC165 to read the switches and shift them in. This could be extended by cascading shift registers.

2. I could use resistors to encode the switches as analog levels and read them in using the 10-bit A/D in the microcontroller. I could extend this by using an analog mux to read in more switches. Does anyone know an arrangement of resistors that would maximize the resolution? The usual DAC R-2R tree doesn't work unless the switches are SPDT.

Anyone have any other tricks or comments?
 
Replies continue below

Recommended for you

My answer was killed when I hit the wrong browser key :( and now the site is down, so it's not letting me submit my answer :(

Anyway, I think I have an answer using a modified resistor ladder:

Code:
+5V---+-S-+------+-S-+------+-S-+------+-S-+------+--Micro
      |-R-|      |-R-|      |-R-|      |-R-|      |
                                                  R
                                                  |
                                                 ===
                                                Ground

4 DIP switches in series, each with a parallel resistor, each resistor in the typical decade fashion.

An open switch includes the resistor in the divider, a closed switch bypasses it.

Had a blast coming up with this one!

Dan - Owner
Footwell%20Animation%20Tiny.gif
 
Dan, you get negative benefits using decade resistor values in your earlier posting. It eats up dynamic range and makes noise margin a problem. Equal resistors would have been fine had the OP not required a rotary 4 bit DIP switch.
 
I see where you were going macgyvers, but I think it would still end up with the 16 different voltages being spread unevenly across the range between GND and VCC.

logbook, I don't think equal resistors would work there because then I would not be able to distinguish between the different switches being closed.

Of course, the point is moot because the 4-bit rotary DIP switches that I'm trying to read have four SPST switches with one common pin.

Thanks for everyone's input so far!
 
output is from opamp and input line could be a negative voltage. The input can be digitally strobed. All inputs could be summed into the opamp avoiding an analog mux. Tolerances are left as an exercise for the designer.
 
geek,

Uneven isn't a problem, as long as the smallest voltage change is within the resolution of your ADC (taing resistor tolerances into account). Still, it's a shame I didn't see the rotary part sooner... I'll think on it again when I get a moment at work today.

Dan - Owner
Footwell%20Animation%20Tiny.gif
 
For simplicity, combine logbook's & macgyver's solution. Use positive reference for input line, remove op amp, and tie the right side of the 2.2K to ground.

Now you have a voltage divider with the output on the left end of the 2.2K, sent that to the ADC.

You'll have reduced dynamic range and linearity, but extracting 4 bits with a 10 bit ADC shouldn't be a problem.

You can look at this as approximating switchable current sources driving a resistive load. The smaller you make the load resistor (2.2K) compared to the binary weighted ones, the better your linearity, but the smaller your full scale.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor