Sense a switch closure using least amt of current
LarryG
Posts: 50
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
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
A) Have the input pin float and then goes to GND by the switch
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.smarthome.viviti.com/propeller
-Phil
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*
@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
... 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.·
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.