How to switch power of microcontroller by button?

I have a microcontroller device with battery power. Currently I toggle power by simple on/off switch. I want to toggle power by one pushbutton with minimal modification of the schematic (and microcontroller program probably) and with no consumption when device is off. How can I do it?

Here at start microcontroller sets PB3 to high and thus hold power for device. But this is not a solution for my problem, because I need also to off the device by pressing S1.

ADDED. Can I exclude VT2 from circuit (i.e. microcontroller drive base of VT1 directly)?

• Would latching push-buttons be acceptable solution? – AndrejaKo Jul 1 '12 at 17:31
• @AndrejaKo: No. – user54579 Jul 1 '12 at 17:45
• Good luck then! One way I see would be to charge a capacitor while the push-button is presses and then try to activate something with it, like a MOSFET, but that would depend on the time the button is pressed. – AndrejaKo Jul 1 '12 at 17:51
• @tcrosley It's not a duplicate, the question you refer does not require that the power is toggled when the button is pushed, it just requires that it turns on allowing the microcontroller to shut down itself. – Bruno Ferreira Jul 2 '12 at 21:45
• @BrunoFerreira ok, agree -- I deleted my earlier comment. I added the line to the micro in this answer but I didn't include the zener as you did in your answer which is a good addition. – tcrosley Jul 3 '12 at 6:29

Based on the circuit you provide, you could just add a diode in series right after the switch (S1) (cathode connected to the switch) and them you could use an input to detect if the switch was pressed again, if so, turn off PB3.

The zener diode protects the PIC input from the voltage coming from the power supply.

• What's the role of the diode? – m.Alin Jul 20 '12 at 11:45
• @m.Alin The diode here allows the microcontroller to detect when the switch is released. If we didn't have the diode, when VT1 starts conducting we would always have a high level on the microcontroller input. – Bruno Ferreira Jul 20 '12 at 11:56

How off do you really need off to be? Many modern microcontrollers have sleep current well below the self-discharge current of even small batteries. You can have the pushbutton simply drive the I/O pin of the micro, which then toggles itself between sleep and active mode each button press. Some debouncing will be needed, but that all doable in the firmware too.

This sort of on/off method is getting pretty common nowadays. When it only takes a µA, a microcontroller doesn't need to be truly off, just asleep, which it can do under its own control. The button line has to be wired to something that can cause the micro to wake up from sleep, but just about every micro has at least one of those, usually several.

• Agreed, a lot of my designs nowadays don't even have a on/off button, they just wake up on a button push. – Oli Glaser Jul 2 '12 at 1:22
• Me device contain also LCD and other peripheral. Thus I must power down it too. – user54579 Jul 2 '12 at 2:00
• @user: That doesn't mean you have to power down the micro, only the other logic. With the micro doing the on/off thinking, the micro can control the power to the other devices so that they are off when the micro is sleeping. This means you have a deliberate digital output that can be whatever polarity is convenient to switch things on and off, and you don't need to implement toggling in analog hardware. – Olin Lathrop Jul 2 '12 at 14:07
• Even though many microcontrollers have very low sleep currents, quality regulators cost more than crummy ones. A regulator that uses an extra 250uA (or even 2.5mA) may be fine if it only has to be active when a device is in use, but would be totally unacceptable if it had to be active even when a device was "switched off". One product I designed some years back didn't even use a regulator--instead it used three transistors and about seven resistors to provide a nominal 5-volt supply with processor-controlled shutdown. By "regulator" standards its voltage accuracy was really crummy... – supercat Jul 3 '12 at 15:38
• @OlinLathrop: Two 2N3094's and a 2N3906 cost less than $0.07. Even if one figures ten components cost a penny each to assembly, that still saves a dime per unit--if 100,000 units are manufactured, that's$10,000. If a product isn't going to have that sort of volume, it may not be worth trying to squeeze out that \$0.10; if today's regulators were available when I designed the thing, and if I didn't know in advance how well it would sell, I probably would have simply used a regulator with standby control, but my circuit works and is cheaper than anything else; no reason not to keep using it. – supercat Jul 3 '12 at 16:26

EDIT - on reflection, the circuit below (which I'll leave for reference) is probably best suited for use in circuits without a micro. AS mentioned in the other answers, unless you really can't afford the few uA, it doesn't really make sense not to use the micro to control the power toggle, as it uses less components and can be controlled accurately.
The simplest version can be something like a IOC (interrupt on change) input with pull up, with button to ground. The micro has power applied all the time, and controls a P-channel MOSFET (with pullup from gate to source) for the rest of the circuit. When it sleeps it lets the gate float to turn the circuit off.

Reference circuit:

At first The P-MOSFET is off, so there is no base current at Q2, which is also off. Q1 is off, so Q1c is at 5V. The circuit is static.

When S1 (ignore the + and - nodes, they are there for SPICE triggering purposes) is pressed the 5V at Q1c is connected to Q2 base, turning it on. This pulls the P-MOSFET gate to ground, turning it on as well.
R4 now sees 5V and when S1 is released, it provides Q2s base with the current needed to keep it open (and therefore the MOSFET on too) Q1 is also turned on when current through R2 charges C1 to ~600mV, at which point Q1c is <200mV (i.e. Q1 is turned on)
The circuit is now static again.

When S1 is pressed again, Q1 sinks the current from R4 (which is keeping Q2 on) turning Q2 off. R1 pulls the MOSFET base up to 5V and switches it off again.

Here is the simulation (V(push) high represents when the button is pushed):

Also we can see after power off the current heads to zero (as C1 discharges and Q1 turns off) so the circuit consumes no power in the off state (the cursor for I(V1) is at 19.86s and measures 329nA):

The original circuit idea is not mine, it comes from Dave Jones over at EEVblog.

As Bruno Ferreira suggested, the easiest way to allow the button to act as an "off" switch is to change your circuit is to allow the processor to know when the button is pushed. I think one can reasonably well use resistors to protect the processor's input against voltages in excess of VDD without needing a Zener for that.

Here's a rough sketch of a circuit design you might use. The right half represents the behavior of the processor, and I used a combination of a transistor, Zener, and resistor to stand in for a regulator. The processor's output is represented using an analog switch its VDD, rather than a gate, since gates in this simulator always generate +5V output.

A key aspect of the circuit, which can cause trouble if ignored, is that it is designed so that the processor cannot turn the circuit on unless its VDD is at least ~3.6 volts; I have also rigged the simulator so that the processor will always try to turn on its output whenever its VDD is below 3.5 volts. I've seen a lot of designs which assume that processors will not try to output a logic high as their power goes away. That assumption may work out okay with some batches of chips used in testing, but then fail with other batches of chips used in full-scale production. The behavior of most processors is unspecified during undervoltage conditions; a good design should be engineered so that the behavior of a processor during such conditions won't matter (slight note: it is probably safe to assume that a processor which isn't explicitly designed to generate voltages which are higher than any applied voltage won't magically start doing so; I don't think there's an explicit spec for that, but I think in most cases it may be safely inferred).