Shop OBEX P1 Docs P2 Docs Learn Events
Detecting a Switch Position — Parallax Forums

Detecting a Switch Position

Greg LaPollaGreg LaPolla Posts: 323
edited 2009-07-09 21:37 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-07-09 17:40
    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.
  • Greg LaPollaGreg LaPolla Posts: 323
    edited 2009-07-09 18:28
    Mike,


    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
  • rokickirokicki Posts: 1,000
    edited 2009-07-09 18:29
    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.
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-07-09 18:40
    > 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.


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Greg LaPollaGreg LaPolla Posts: 323
    edited 2009-07-09 20:12
    @rokicki

    could you elaborate on that a bit, I am not fully understanding.
  • rokickirokicki Posts: 1,000
    edited 2009-07-09 20:31
    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.

    -tom

    Post Edited (rokicki) : 7/9/2009 8:38:09 PM GMT
  • Greg LaPollaGreg LaPolla Posts: 323
    edited 2009-07-09 21:37
    @rokicki

    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
Sign In or Register to comment.