View Full Version : Detecting a Switch Position
07-10-2009, 12:32 AM
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.
07-10-2009, 12:40 AM
There's no way to do this the way you're thinking about it. An input pin has only two states, high and low. There's no way to detect an unconnected pin.
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.
07-10-2009, 01:28 AM
Thanks for the info. There is a great tutorial on this PE Kit Tools: Measure Resistance and Capacitance (http://forums.parallax.com/showthread.php?p=791692)
Just replace the pot with a resistor.
07-10-2009, 01:29 AM
Or, simply use two pins, two resistor pullups, and ground the center contact. If you're using strictly logic levels, three positions require at least two inputs.
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.
07-10-2009, 01:40 AM
> There's no way to do this the way you're thinking about it. An input pin has only two states, high and low. There's no way to
> 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.
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
07-10-2009, 03:12 AM
could you elaborate on that a bit, I am not fully understanding.
07-10-2009, 03:31 AM
Sure, I'm assuming you have a three-position, two-pole switch.
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:
P0 P1 Result
0 1 Top
1 1 Center
1 0 Bottom
0 0 Your wiring is messed up
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.
Post Edited (rokicki) : 7/9/2009 8:38:09 PM GMT
07-10-2009, 04:37 AM
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.