# 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. Aug 18, 2010 at 13:24
• It also depends on where this output switch is and what voltages (states!) you have available.
– XTL
Aug 18, 2010 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. Aug 18, 2010 at 17:54
• I like the solution with 2 inverters. Just wondering what would be required to enable this to switch 30V @ 1A? Apr 1, 2012 at 21:33
• @Overlow: Replace the output transistor with a power transistor or relay Apr 4, 2012 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. Apr 16, 2017 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. Apr 16, 2017 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. Apr 16, 2017 at 3:56 • @CraigMcQueen The last circuit shown will start with 0.01 μF capacitor 0 V, so Vout will be 0. Apr 16, 2017 at 3:57 • @endolith Where in the last image is the VCC connected? I see ground and Vout. – Mike Jun 17, 2017 at 10:51 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) Aug 18, 2010 at 4:55 • Could you not also use an RC filter in the feedback path to prevent bouncing? Aug 18, 2010 at 14:10 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. Aug 18, 2010 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? Aug 18, 2010 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. Aug 18, 2010 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, 2010 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, 2010 at 0:50

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, 2010 at 5:21 •$0.03 is an awful lot like \$0.02 in small quantities... Aug 18, 2010 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. Aug 18, 2010 at 14:02
• It looks as though transistor Q2 needs a resistor on the base, to limit the current flowing through the base. Apr 25, 2017 at 23:55
• Instead of adding a resistor, transistor Q2 and R1 can be removed. Connect the high end of R5 to the collector of Q3 and it should work pretty much the same. The time constant will be a bit different because of the high value of R2. So either adjust R2 or adjust C1. Feb 23, 2021 at 3:15

Make a T flip-Flop by shorting the inputs of a positive edge-triggered j-k flipflop and connect the pushbutton output to the clock input of j-k flip flop.

simulate this circuit – Schematic created using CircuitLab

• With the capacitor on the clock input this circuit is suseptable to double clocking on noise. Some devices have Schmitt trigger inputs on the clock, if not one should be added as a buffer between the cap and the clock input. An alternative is to use a D FF with the Qnot driving the D input with an RC longer than the button debounce. Then it won't matter how many times the input is clocked, the output won't change after the first one until the RC times out. However... buttons bounce on release as well as press. So... it's complicated. Feb 25, 2021 at 13:25

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, 2010 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) Aug 20, 2010 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.

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 = false; // store for toggle state, false = 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. Aug 18, 2010 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. Apr 25, 2017 at 23:58