reading DIP switches using few GPIO pins
reading DIP switches using few GPIO pins
(OP)
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?
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?





RE: reading DIP switches using few GPIO pins
http://en.wikipedia.org/wiki/Multiplexer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"It's the questions that drive us"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RE: reading DIP switches using few GPIO pins
Anyway, I think I have an answer using a modified resistor ladder:
CODE
|-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

http://www.Hi-TecDesigns.com
RE: reading DIP switches using few GPIO pins
Dan - Owner

http://www.Hi-TecDesigns.com
RE: reading DIP switches using few GPIO pins
Keith Cress
Flamin Systems, Inc.- http://www.flaminsystems.com
RE: reading DIP switches using few GPIO pins
RE: reading DIP switches using few GPIO pins
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!
RE: reading DIP switches using few GPIO pins
h
Notice that these are the only resistors in the E24 standard range that give the exact 1-2-4 ratios!
RE: reading DIP switches using few GPIO pins
RE: reading DIP switches using few GPIO pins
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

http://www.Hi-TecDesigns.com
RE: reading DIP switches using few GPIO pins
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.