Shop OBEX P1 Docs P2 Docs Learn Events
Sense a switch closure using least amt of current — Parallax Forums

Sense a switch closure using least amt of current

LarryGLarryG Posts: 50
edited 2010-06-07 19:48 in Propeller 1
Hopefully just a quick question. My project is battery operated. At power up, the first thing is to read switch (momentary push button) to see if it was closed during power up. If closed, then do something. My question is this: what method uses the least amount of current?

A) Have the input pin float and then goes to GND by the switch
B) Have the input pin with a pullup, then goes to GND by the switch
C) Have the input pin to GND by default, then the switch opens to float the pin
D) Some other better way

I am just trying to make sure I use the best method that is reliable, but also uses the least amt of mA since I am running on battery power.

Thanks for any suggestions.

-Larry

Comments

  • LeonLeon Posts: 7,620
    edited 2010-06-06 20:26
    Inputs shouldn't be floating.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Leon Heller
    Amateur radio callsign: G1HSM
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-06-06 20:40
    My guess is:
    1. Will only work if you charge the pin, as a floating pin alone does not help, because you can read anything from that pin if the switch is open. Therefore you need a resistor to not produce a short.

    2. Current goes through the resistor.

    3. Again ... float means anything!

    Connecting both sides of the switch to a pin will do best. You periodically switch between floating and supply on one side and floating and GND on the other side - alternating.

    1. PIN1 = Us, PIN2 = float => charge PIN1
    2. PIN1 = float, PIN2 = GND => discharge if switch is closed
    3. read PIN1
    4. goto 1

    The sense of that? The current needed is only the current needed to charge/discharge the pin. No current will go through a resistor. Charging makes sure that you will read a 1 in case the switch is open and PIN1 is floating again. If the switch is closed the PIN2 having ground level will discharge PIN1 before you read it, which then will be 0.

    Of course PIN2 can be used for more than one switch.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-06-06 20:42
    A high value pullup resistor, like 100K, would work fine. That would not drain any current when the switch was open and at most 33uA when the switch was closed.
  • LarryGLarryG Posts: 50
    edited 2010-06-06 20:50
    Got it, thanks to all for the advice. I am at the point where I am working on optimized energy consumption, and did not want to waste a mA.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-06-07 02:45
    What event triggers the switch, and what is the Propeller to do when the event takes place? Just curious... I built a data logger several years ago that needed to record the change in state of a switch, meaning not only did it need to record a closure, but also when it opened. The data logger needed to last at least 5 months on a single battery... recording the date, time, and switch state. This was accomplished, using a battery that only had 220mAH to start with (CR2032), so the circuit had to be very power efficient. Of course this project was done years before the Propeller was ever introduced, but some of the same circuit principles I used could more than likely be applied to your design.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-06-07 04:52
    I'll second Mike Green's suggestion - 100k pullup.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-06-07 05:27
    For the absolute minimum current draw, connect the pullup to a pin, instead of Vdd. Then pulse it high only when you read the switch state.

    -Phil
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2010-06-07 05:28
    For extremely low power operation even a 100K resistor would still continually draw 33uA while the switch is closed. There is absolutely no reason why you couldn't use a 1M pullup and it is advantageous to add a small capacitor (100nf) across the switch. This cap supplies the "wetting current" for the switch contacts plus it helps debounce the signal and it also minimizes glitches and interference.

    Alternatively you could skip the pullup and run a 1K resistor from the Prop to the switch with a 10nf across the switch. Every time you want to sense the switch you simply output a high long enough to charge the capacitor to a high normally (a few microseconds). On reverting to input if the input is low then the switch is closed, otherwise it is open. The input never really floats either as it is either pulled down by the switch or it is holding a charge which only drains away extremely slowly.

    Of course all this might be totally redundant if your battery circuit is drawing 10's of milliamps of even just milliamps constantly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    *Peter*
  • LarryGLarryG Posts: 50
    edited 2010-06-07 15:27
    @Beau - Cool, data logger running on just a coin battery. My project is a nav system that will be used on my bicycle. With the OLED display, GPS, accelerometer, etc. I am starting to pull some current.

    @Phil - That is an interesting idea, use a second pin to source the current to the sensing pin, only when needed. This could work because I am only going to see if the button is pushed for a few seconds upon powerup.

    @Peter - Thanks for the ideas, as well as the debounce comment. I am hoping that since the button will be depressed before power up that debounce will not be an issue for me.

    Thanks for the ideas guys.

    -Larry
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-06-07 18:56
    Now that I have the propeller board in front of me I could not withstand trying out what I suggested:
    con
            _clkmode        = xtal1 + pll16x
            _xinfreq        = 5_000_000
    obj
      term:  "FullDuplexSerial"
      
    var
      long pin
      long stack[noparse][[/noparse]50]
      
    pub main | x
      term.start( 31, 30, 0, 57600 )
      waitcnt( clkfreq*10+cnt )
      cognew( send, @stack )
      
      outa := %01_00000000
      repeat
        pin := ina[noparse][[/noparse] 8 ]
        dira[noparse][[/noparse] 9 ] := 0
        dira[noparse][[/noparse] 8 ] := 1
        dira[noparse][[/noparse] 8 ] := 0
        dira[noparse][[/noparse] 9 ] := 1
    pub send
      repeat
        if pin
          term.tx( "1" )
        else
          term.tx( "0" )
         
    
    

    ... and it works. No other components needed but the switch. Connect one side to pin 8 and the other to pin 9. Did not measure the current, but as it's only for charging the·pin capacity it must be very low. Maybe someone else want's to measure.·
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2010-06-07 19:36
    MagIO2,

    The I/O pin by itself should be close to about 6pF.


    The '4x4 keypad decoder' in the OBEX uses this same principle to determine which keys have been pressed.

    Reference: obex.parallax.com/objects/279/

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • LarryGLarryG Posts: 50
    edited 2010-06-07 19:48
    Very clever MagIO2. Yet another reason why I should hide a demo board in my desk drawer so I can try these things out at work during the day [noparse]:)[/noparse] Now if I could just disguise my bench meter as a telephone...
Sign In or Register to comment.