# Make a momentary switch control a toggle

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.

• 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. – J. Polfer Aug 18 '10 at 13:24
• It also depends on where this output switch is and what voltages (states!) you have available. – XTL Aug 18 '10 at 15:17
• 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. – endolith Aug 18 '10 at 17:54
• I like the solution with 2 inverters. Just wondering what would be required to enable this to switch 30V @ 1A? – stevenrcfox Apr 1 '12 at 21:33
• @Overlow: Replace the output transistor with a power transistor or relay – endolith Apr 4 '12 at 0:19

Here's another alternative with two inverters and debouncing:

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

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: • 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. – Craig McQueen Apr 16 '17 at 3:51 • @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. – endolith Apr 16 '17 at 3:53 • @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. – endolith Apr 16 '17 at 3:56 • @CraigMcQueen The last circuit shown will start with 0.01 μF capacitor 0 V, so Vout will be 0. – endolith Apr 16 '17 at 3:57 • @endolith Where in the last image is the VCC connected? I see ground and Vout. – Mike Jun 17 '17 at 10:51 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.

• +1 - But I believe that part only comes in SMT though. – J. Polfer Aug 18 '10 at 13:00
• 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? – endolith Aug 18 '10 at 13:19
• 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. – endolith Aug 18 '10 at 13:56
• @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. – davr Aug 19 '10 at 0:46
• @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. – davr Aug 19 '10 at 0:50

One possible method:

Use an RC to debounce the switch

and then feed it into a D flip-flop with the output feeding back to the input to implement a T 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?

• 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) – Connor Wolf Aug 18 '10 at 4:55
• Could you not also use an RC filter in the feedback path to prevent bouncing? – endolith Aug 18 '10 at 14:10

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?

• $0.02...maybe if you buy 27,000 of them like here: search.digikey.com/scripts/DkSearch/… – davr Aug 18 '10 at 5:21 •$0.03 is an awful lot like \$0.02 in small quantities... – SingleNegationElimination Aug 18 '10 at 13:24
• 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. – endolith Aug 18 '10 at 14:02
• It looks as though transistor Q2 needs a resistor on the base, to limit the current flowing through the base. – Craig McQueen Apr 25 '17 at 23:55

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.

• Personally, I need a standalone solution, but the question is meant to be general. – endolith Aug 18 '10 at 13:57
• 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. – Craig McQueen Apr 25 '17 at 23:58
• flase? You spelled it right further down... – Nic Hartley Jul 21 at 20:11

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.

• How would you "un-latch" it though? Bringing the magnet close to the sensor a second time wouldn't dis-engage it, would it? – davr Aug 19 '10 at 18:28
• 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) – onaclov2000 Aug 20 '10 at 14:02

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.

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.

## 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).