15
\$\begingroup\$

What are the simplest, cheapest, smallest ways to make a momentary switch produce a 2-state toggling output (latching momentary switch)?

In other words, the output is continuously low, and when you momentarily press the button/tact switch, the output changes to continuously high, and then when you press it again, it switches back to low.

\$\endgroup\$
  • \$\begingroup\$ I think the best answer to this depends on the asker and whether or not you are more comfortable with a hardware or a software solution. \$\endgroup\$ – J. Polfer Aug 18 '10 at 13:24
  • \$\begingroup\$ It also depends on where this output switch is and what voltages (states!) you have available. \$\endgroup\$ – XTL Aug 18 '10 at 15:17
  • \$\begingroup\$ Just two states, high and low, like any digital logic. If you want to do multiple states that you're cycling through, I think that would require a microcontroller. You'd probably have one in your circuit anyway for that. \$\endgroup\$ – endolith Aug 18 '10 at 17:54
  • \$\begingroup\$ I like the solution with 2 inverters. Just wondering what would be required to enable this to switch 30V @ 1A? \$\endgroup\$ – stevenrcfox Apr 1 '12 at 21:33
  • \$\begingroup\$ @Overlow: Replace the output transistor with a power transistor or relay \$\endgroup\$ – endolith Apr 4 '12 at 0:19
9
\$\begingroup\$

Here's another alternative with two inverters and debouncing:

Two inverters

For logic output, only the two inverters, RC, and feedback resistor are needed:

enter image description here

NC7WZ14 is $0.06 with 2 inverters, small 6-pin package, and Schmitt trigger inputs, though I think this circuit doesn't even need Schmitt inputs because of the RC. Also it can drive 24 mA, so you could connect the LED directly to it if you're using an LED for whatever reason.

Also shown here and here as a toggle touch switch.

I think the feedback resistor has to be much smaller than the center resistor to prevent it from oscillating if you hold down the button, because the low-impedance connection to the output will prevent the capacitor from charging to the middle voltage until you let go.

Variants are described at Press ON - Press OFF soft latching circuits:

AND gate variant

\$\endgroup\$
  • \$\begingroup\$ When this is first powered on, will it start up with the output on, off or random? Just inspecting the circuit, I'm guessing it will power up with the output on. \$\endgroup\$ – Craig McQueen Apr 16 '17 at 3:51
  • \$\begingroup\$ @CraigMcQueen When first powered on, the capacitor will have 0 V across it. So the first circuit will start in the ON position. Connect the capacitor to the supply instead of ground, and it will start in the OFF position. \$\endgroup\$ – endolith Apr 16 '17 at 3:53
  • \$\begingroup\$ @CraigMcQueen Actually is that guaranteed? I'm not sure. The inverter's low output impedance will dominate over the 220k resistor, so it might start in a random state. \$\endgroup\$ – endolith Apr 16 '17 at 3:56
  • \$\begingroup\$ @CraigMcQueen The last circuit shown will start with 0.01 μF capacitor 0 V, so Vout will be 0. \$\endgroup\$ – endolith Apr 16 '17 at 3:57
  • \$\begingroup\$ @endolith Where in the last image is the VCC connected? I see ground and Vout. \$\endgroup\$ – Mike Jun 17 '17 at 10:51
7
\$\begingroup\$

Or you could do it with a single tiny 6-pin microcontroller and no other components. It might cost a tiny bit more, but it's simpler and takes up less space. Debouncing a switch in software is simple, then it's just a manner of storing a boolean if the output should be high or low. The smallest version of this mcu comes in a 2mm x 2mm surface mount package. In large quantities it might be $0.50.

toggle schematic

\$\endgroup\$
  • \$\begingroup\$ +1 - But I believe that part only comes in SMT though. \$\endgroup\$ – J. Polfer Aug 18 '10 at 13:00
  • \$\begingroup\$ Yeah but even the cheapest microcontroller is going to cost more than simple digital logic. It's a pretty complex system to do a simple task, and you're opening yourself up to the world of software bugs. Also, does it increase production costs to be programming every micro? \$\endgroup\$ – endolith Aug 18 '10 at 13:19
  • \$\begingroup\$ So you'd use the internal pull-up resistor on PB0 set up as a digital input and then ground it to trigger it, right? You shouldn't need a resistor in series with the switch that way. \$\endgroup\$ – endolith Aug 18 '10 at 13:56
  • 1
    \$\begingroup\$ @TokenMacGuy No current should flow from PB0 to GND, since PB0 would be an input, not an output. @endolith correct, using the internal pull-up means no need for external pull-up. (In addition, the pull-up is not in series with the switch, with either an internal or external pull-up). @sheepsimulator coming in SMT is only a problem for the hobbyist. In that case, an extra dollar or two wouldn't be a problem, and you can easily step up to the 8-pin DIP ATtiny25. \$\endgroup\$ – davr Aug 19 '10 at 0:46
  • \$\begingroup\$ @endolith It depends on the application I guess. There are always tradeoffs. Everything but the very most basic of device has some sort of logic in it. Saying you open yourself up to the world of software bugs isn't that big of a deal...without the mcu you open yourself up to hardware bugs, since you are constructing some sort of digital/analog circuit to perform logic. \$\endgroup\$ – davr Aug 19 '10 at 0:50
7
\$\begingroup\$

One possible method:

Use an RC to debounce the switch

RC network connected to momentary switch to debounce

and then feed it into a D flip-flop with the output feeding back to the input to implement a T flip flop.

D flip-flop configured as toggle flip-flop

The D flip-flop needs to be positive edge-triggered only, and needs an inverted output.

  • The 8-pin NC7SZ74 should work, is very small, and costs $0.12 in quantity.
  • The 74HC74 is a dual D flip-flop with 14 pins for $0.05.

Are there any issues with this? The switch would need to be held down for a short period of time for the rise time to trigger the clock input, which is probably good. If you hold down the button, it will only trigger once, which is good. Is there a better way to debounce in the feedback loop or something instead?

\$\endgroup\$
  • 5
    \$\begingroup\$ Really needs a schmitt trigger buffer on the input from the button (or if the flip-flop has schmitt trigger inputs that would work too). Otherwise, the FF may oscillate momentarily when the clock input rises slowly when the button is released. (This can be somewhat mitigated with careful selection of the debouncing cap and resistor, but never eliminated) \$\endgroup\$ – Connor Wolf Aug 18 '10 at 4:55
  • \$\begingroup\$ Could you not also use an RC filter in the feedback path to prevent bouncing? \$\endgroup\$ – endolith Aug 18 '10 at 14:10
1
\$\begingroup\$

This circuit (also described here) is very cheap, but is more complex and takes up some space with all the components. If transistors are like $0.02 in large quantities, it would be maybe $0.10 total?

3-transistor latching switch

\$\endgroup\$
  • \$\begingroup\$ $0.02...maybe if you buy 27,000 of them like here: search.digikey.com/scripts/DkSearch/… \$\endgroup\$ – davr Aug 18 '10 at 5:21
  • 1
    \$\begingroup\$ $0.03 is an awful lot like $0.02 in small quantities... \$\endgroup\$ – SingleNegationElimination Aug 18 '10 at 13:24
  • \$\begingroup\$ 3904s for 1.4 cents: newark.com/on-semiconductor/mmbt3904lt1g/… At high quantities, capacitors are like 1 cent, and resistors are like 1/10th of a cent. \$\endgroup\$ – endolith Aug 18 '10 at 14:02
  • \$\begingroup\$ It looks as though transistor Q2 needs a resistor on the base, to limit the current flowing through the base. \$\endgroup\$ – Craig McQueen Apr 25 '17 at 23:55
0
\$\begingroup\$

Could you not just do it in software?

Will the button be an input on a programmable device, or do you need to do the toggle in the hardware realm?

By storing a button state in memory it's quite easy to use a momentary switch as a toggle.

boolean buttonState = flase; // store for toggle state, flase = off true = on

if(yourButton == HIGH && buttonState == false) // if button is pressed and toggled off
{
     Serial.println("Button Toggled On"); // do something while button is on

     buttonState = true // set button state to on
}

if(yourButton == HIGH && buttonState == true) // if button is pressed and toggled on
{
     Serial.println("Button Toggled Off");  // do something else while button is off

     buttonState = false // set button state to off
}

Sorry if this is off the mark, obviously this answer depends on you using an MCU or programmable chip.

\$\endgroup\$
  • \$\begingroup\$ Personally, I need a standalone solution, but the question is meant to be general. \$\endgroup\$ – endolith Aug 18 '10 at 13:57
  • \$\begingroup\$ Yes, that's certainly the sensible solution when you've got a microprocessor. Note, you should also implement "debounce" since switches and buttons are noisy when switching. \$\endgroup\$ – Craig McQueen Apr 25 '17 at 23:58
0
\$\begingroup\$

I know it's kinda an "indirect" way of doing it, but you can get a hall effect sensor like sparkfun has: http://www.sparkfun.com/commerce/product_info.php?products_id=9312

and just use a magnet, I found out (the hard way) that this particular hall effect is latching, so it won't work for what I need it to, but looks like it could in theory work for what you're looking for.

I just plugged it into a breadboard, I think I may have used a resistor (It's been probably a month so I'm a little fuzzy on the details), and when I move the magnet close (momentary) it closes the sensor and there you go.

\$\endgroup\$
  • 1
    \$\begingroup\$ How would you "un-latch" it though? Bringing the magnet close to the sensor a second time wouldn't dis-engage it, would it? \$\endgroup\$ – davr Aug 19 '10 at 18:28
  • \$\begingroup\$ I believe you would have to reverse the polarity of the magnet to "clear it" I will haave to hook it up again at home to test it out to be sure though, however I'm looking into how long it takes to "reset" if you remove power. I might be able to use it for my project that I was going to, by killing power for the specified time (provided it's not too long) \$\endgroup\$ – onaclov2000 Aug 20 '10 at 14:02
0
\$\begingroup\$

The easiest alternative is to use an alternating pushbutton switch.

If you ignore the fact that it latches at different positions, it's almost exactly like a momentary switch. I understand that this isn't the spirit of the question, but it is a simple and compact solution requiring no software and no external hardware.

\$\endgroup\$
0
\$\begingroup\$

Not sure exactly what the end goal is but I thought I'd put this out there:

This is a great little circuit I like to use to drive bistable relays. It's pretty cheap to build as well.

Pressing the momentary switch toggles the state of the relay and the LED. It's not really picky about how long you hold down the button. Since the relay is latching, it won't draw much current most of the time.

In this configuration, the LED will turn on when the relay is in its set condition and off when the relay is reset. You can also connect R7 to the + terminal of C3 to make the LED turn on when the relay is reset instead.

I like to use this in guitar effects so I can bypass or engage the device with a momentary footswitch. Of course a 3PDT switch would do the same thing on its own, but the switching is quieter this way (no big pop) and momentary soft-touch footswitches feel much nicer than the big 3PDT ones.

Hope this helps.

Latching Relay Driver

\$\endgroup\$

protected by Dave Tweed Sep 27 '15 at 1:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.