Introduction: On Off Latch Circuit With UC. One Push Button. One Pin. Discrete Component.
Hello everybody, was looking for a on/off circuit on the net. Everything I found was not what I was looking for. I was talking to myself, there are necessarily a way to that. That's what I needed.
-Only one push button to do on and off.
-Must only use one pin on uC. Not 2.
-Must work with battery.
-From 3.3v to 20v
-Working with or without regulator. (Remove regulator from 3.3 to 5v in)
-No special i.c.
I designed a schematic and code to do that. This is working very well. Very handy schematic to have in many projects.
Let's begin the lab...
Step 1: Schematic Explanation
Here i'm using an atmega328. But any uC can do the same. In this exemple i'm using 20V in. It's the maximum voltage i can. Why ? because mosfet vgs max as per datasheet is -20v maximum. I tried to go to 30v. it was workin. I rise to 35v and it was working... for a while. Mosfet as blow :) Thing is, schematic is good to go higher. But you will need to find an mosfet for that.
I'm using a P mosfet to allow current to pass or not. Vgs threshold for the Si2369ds is -2.5v.
When the push button isn't pressed. Vgs is 0v. R1 resistor 1M pull up the gate to Vcc. So Vgs (volt gate vs volt source) is 0v. At Vgs 0v, current doesn't flow.
When we push the button. Current is flowing by R1, R2 and T1.
T1 2n3904 is closed by r2 resistor and put the gate to gnd. 0v is now on the transistor collector. Vgs is now -20v and current is flowing throw the mosfet and turn on the uC.
Here is the magic happen, uC turn on, we put the interrupt pin in input mode but, we active the internal pull-up, so 5v is coming from uC to R2. But keep in mind this pin is in input mode to sense interrupt on falling edge.
We release the button, but uC is sending 5v on R2 the circuit stay on. T1 stay closed, mosfet gate is at 0v.
So far so good. Circuit is on. Transistor is closed, we have 0v on transistor collector. And a 5v come out from interrupt pin.
When we push a second time the button, we are sending a low (0,7v) to the uC and an interrupt appears. Because, collector transistor is 0v (this one is closed). Interrupt occurs at falling edge.
ATTTENTION: In some case 0,7v can be see as High or not enough to trig a low. Do your experiment. In my case, this has always working. If you need 0v. See the mosfet schematic.
In the interrupt sub routine we turn the pin in output mode and we send a low on that pin.
When we release the button, T1 will open and the whole circuit will shut down.
Yes but if i have 20v in i'll send 20v on the interrupt pin and the uC will explode!! ?
Not really. Interrupt pin never go higher than 3.7v. Because of the transistor and R2.
More explanation at next step.
When the device is off, we aren't consume current anymore (a few pa). At this scale we can run on battery for years...
I added another schematic i did and tested. This one is all mosfet. P type and a N type instead a transistor. We must add a zener diode 5.1v to protect the uC from Vbatt. We can use separate mosfet or all in one ic package like DMC3021LSD-13, DMG6601LVT, IRF7319TRPBF.
Both method works fine. But 2n3904 leakage is better than mosfet. 50nA vs 1uA as per datasheet. Also in the mosfet version, we have C1 always hot. So if this capacitor is leaking, battery will be drain.
Step 2: What Is Going on On the Interrupt Pin. Why It's Safe With 20v in ?
Current is flowing by the more easy way. It pass by R1(1M) R2 (100k) and T1 (0,7v). As you can see on photo. Interrupt pin never goes higher than 3,7v even if we have 20v in.
If you look the first picture. Rise time is 163ms. As soon i press power on. uC turn on. Waiting time fuse bit is set to 65ms. We are around 0,68v for this time. After, 65ms we are around 0,7v because uC is sending 5v with a pull up we have a 0,1v of rising. But button is pushed so it can't go higher than 0,7v. Soon i release the push button, voltage rise to 3,7v.
When you power off the mosfet, we can see that the interrupt pin goes to 0v in 33us. So the pin is low but device stay on by the push button to low. As soon we release the button device turn off.
I did a little video on next step to show the whole process.
Step 3: Demonstration
Step 4: The Code.
Here is the lab code in C.
Step 5: Conclusion:
I hope you have enjoyed this lab. If you liked or better, used this method, just leave a comment. Thanks for watching.