Detecting a Switch Position
Greg LaPolla
Posts: 323
I may be over thinking this. If I have a 3 position toggle switch position 2 being neutral, I can connect position 1 to VDD and position 3 to GND and detect those positions, however I would like to know if there is a way to detect the switch in the neutral position. Where there would be no current flow.
For position 1 and 3 I can set the pin to input and check for high and low signal but for neutral I am not sure there is a way to detect it because the signal will not be driven high or low.
Greg
For position 1 and 3 I can set the pin to input and check for high and low signal but for neutral I am not sure there is a way to detect it because the signal will not be driven high or low.
Greg
Comments
There are examples for the Stamp for testing for multiple switches using the RCTIME statement to measure the value of a resistor connected to each of several switches. It requires one capacitor and a resistor for each switch. In your case, you'd have a different value resistor for each switch position. There's an RCTIME subroutine (method) in the BS2 Compatibility Library from the Object Exchange that you could use as a model.
Thanks for the info. There is a great tutorial on this PE Kit Tools: Measure Resistance and Capacitance
Just replace the pot with a resistor.
Greg
If you want to use analog tricks like Mike suggested, there are any number of such tricks you can play, typically involving switching the pin from
output, charging a capacitor, and then switching it back to input and seeing how long it takes to discharge that capacitor.
But simplest is just to use two pins.
> detect an unconnected pin.
Don't say that!
You can switch the pin to output (supposedly you do have a current-limiting resistor) and see wether you can drive it low and high. If both is true, the pin is open. This requires a small cap on the input that keeps the charge for a short moment. It could work without cap, but this way it is safer.
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
could you elaborate on that a bit, I am not fully understanding.
There are three wires, one for "top", one for "bottom", and one ground (shared).
More precisely, I am assuming three pins, 1-2-3. When the switch is Top, 1-2 are
connected; center, all are isolated; bottom, 2-3 are connected. If your switch is
different, let me know.
(Some three-position switches have four wires, 1-2-3-4, and the connections are
1-2, 2-3, and 3-4 respectively. In this case just connect wires 2-3 together and
treat it as a 1-2-3 switch as I describe above.)
Ground the ground. Tie "top" to pin 0, and "bottom" to pin 1.
To both pin 0 and pin 1, also run a 10K resistor to +3.3V.
Leave both pins 0 and 1 as input.
When you want to read the switch position, you will get:
You need two pins, if you use standard digital logic, because there are more than
two states, and one pin can only measure one state, unless you use R/C effects
to measure time delay.
If you can afford two pins, this is probably the easiest and most effective solution.
-tom
Post Edited (rokicki) : 7/9/2009 8:38:09 PM GMT
This is perfect, I thought that was what your saying but I wanted to make sure. I have already breadboarded it and it works perfectly.
Thanks
Greg