Push button power circuit for Arduino... trying to modify it for 12V input - Page 1
Forum section: electronics / beginners

author: doublec4
author_rank: Regular Contributor
timestamp: May 16, 2019, 11:44:51 am
content: Hi All,I found this circuit that allows for the use of a momentary push button to power on an arduino and then the arduino provides the "hold" signal to continue the power latching on.https://sites.google.com/site/wayneholder/pushbutton-power-on-off-for-arduinoI have an application where I will have a ~12.6V input power from (3) 18650 batteries in series (when fully charged). Unfortunately there are a few components not rated for this type of input voltage (max 8V). I have tried to find equivalent components with a higher max input voltage... so I replaced the TPS27081A with a Si3865DDV... same foot print and pinout... I also replaced the P channel mosfet FDV302P with BSS84 so that the max GS voltage is above 12V.I think that should do it... however, based on the circuit in the link I provided it would seem that when the pushbutton is pressed and the p-channel mosfet gate is pulled low, the "BTN" line that would go to the Arduino would see my battery voltage of ~12V ? It would also seem that pin 5 (ON/OFF) of the TPS27081A (or in my case the Si3865DDV) would also see the 12V ? The Arduino can only handle 5V input and the Si3865DDV can handle up to 8V on pin 5...Am I understanding this circuit correctly? I don't want to fry either of them...If I put a resistor ahead of the supply pin on the P channel mosfet, would it work with R2 to create a voltage divider? I'm just not sure what effect R1, the diode, and R3 would have on trying to create this voltage divider to get down to 5VIf anyone can help me out with these few details I would really appreciate it!Thank you

author: MarkF
author_rank: Super Contributor
timestamp: May 16, 2019, 02:12:20 pm
content: First:The max input voltage of the Si3865DDV is 12V. It would be suitable with a 9V battery.But, your 12.6V battery is too high and you would not drive the input to its max voltage.Second:i would put something together with discrete transistors instead. This could be a starting point.Edit- A little more thought.

author: Peabody
author_rank: Super Contributor
timestamp: May 16, 2019, 02:17:18 pm
content: I have no experience with parts like the TPS27081A, and don't really understand the example circuit. But it looks like you are right about excess voltages being applied to inputs. I don't have a solution, but offer the attached circuit which is a simpler approach. Perhaps it will give you an idea of how to solve the problem. For example, you might look at moving the BTN input over to the push button, but protected by a diode, so in effect it is active low instead of active high. Did the example circuit ever work with an Arduino at anything above a 5V power source?

author: doublec4
author_rank: Regular Contributor
timestamp: May 16, 2019, 07:22:08 pm
content: Thanks to both for the reply. So it seems that with both of these solutions they could work. Searching google for more "latching power" circuits I found a few more examples:https://randomnerdtutorials.com/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino/https://electronics.stackexchange.com/questions/193641/simple-inline-latching-power-switchHowever, with all of these examples looking similar in approach, it would seem that once the circuit is powered on, pushing the button would not yield any change in "input" to the micro controller.I'm trying to have one button that would serve as the "ON" button, and button presses after could still be read by the Arduino to perform other functions. Finally, the "long press" would turn the device off. The long press would be handled by software and then killing the pin that is holding the transistor on, so that doesn't really have anything to do with the circuit.

author: MarkF
author_rank: Super Contributor
timestamp: May 16, 2019, 08:35:50 pm
content: Take a look at this circuit. You would need to regulate your 12V battery to 5V before this circuit. LM7805 and a few capacitors to feed it.http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/electronic-circuits/push-button-switch-turn-on/microcontroller-latching-on-off

author: doublec4
author_rank: Regular Contributor
timestamp: May 17, 2019, 12:10:38 am
content: Quote from: Peabody on May 16, 2019, 02:17:18 pmAt first glance I didn't think this would detect the button press after the Arduino was powered on, but looking at it again I believe I see it will, as you said, detect the "active low"So when everything is off at first, and the button is initially held on, the P channel gate gets pulled low and it allows the 12V to reach the regulator and turn on the Arduino. First line of code should hold the output high, triggering the base on the N channel and allowing the P channel mosfet to continue to be turned on. The blocking diode doesn't allow the 12V to pass to the Arduino, but that "input" pin should have the internal pull up set. There will be a ~7V differential across the blocking diode, but I guess that doesn't matter?Now that everything is on, the next momentary button push will pull that line low, everything will remain on, and the input pin should pull low and I can detect that with the Arduino. I can time the duration of the button push and if sufficient, kill power to the output pin, turning off the base of the N channel once the button is released. The release of the long press will also set the P channel gate high again, now killing the whole circuit.Is this the correct interpretation Peabody? Also, what is the purpose of the 2.2uF cap? Is this to handle the bouncing of the switch?

author: Ian.M
author_rank: Super Contributor
timestamp: May 17, 2019, 12:36:04 am
content: Here's a somewhat simpler version than MarkF's one, also using discrete MOSFETs and one I/O pin. It does need two diodes, but if you useBAT54C, you get two in one package.The button turns on the P-MOSFET when pushed, and also pulls the Arduino GPIO low so button pushes can be detected. The diodes keep the GPIO level and PMOS gate level separate.To keep the power on after the user releases the button, enable the GPIO's weak pullup, which keeps the N-MOSFET gate high, which keeps the P-MOSFET on. N.B. The N-MOSFET gate pulldown *MUST* be large compared to the Arduino internal weak pullup, which is max. 50K.To turn the power off, the Arduino sets the GPIO to Output Low. Power off will be delayed if the user is holding the button down. Unlike Mark's, holding the button down wont force poweroff if the Arduino is crashed.A 330R resistor is included to protect against mistakes like setting the GPIO to Output High while the button is pushed.LTspice sim attached (including model of Arduino code controlled behaviour).

author: doublec4
author_rank: Regular Contributor
timestamp: May 17, 2019, 01:03:02 am
content: Quote from: MarkF on May 16, 2019, 08:35:50 pmThanks again Mark, I did some more poking around that website and found the same circuit being used with 5-18V input but no microcontroller attached. As soon as they integrate a uC they put the input voltage down to 5V so that the I/O pin only sees 5V max.I would rather not put a regulator in front of this circuit as there will always be some current draw. The 7805 isn't very efficient when the circuit is on, I'm not sure about when it is off, but either way, this device (warning LED system) will be in storage most of the time and I would like the regulators to be in the "switched" portion of the circuit. The MOSFETS and other components should theoretically have sub micro amp draw when everything is off.So perhaps I can make do with the circuit with the following modification, see my attachment. I've moved the input to the switch similar to what Peabody posted above. I can set it to PULL_UP and therefore would detect the button push as an active low. I would lose the ability to turn off the circuit through the software, but the long button press to drain the latching cap would still work. You can see where I also inserted my 5V buck regulator to bring the voltage down to the proper supply for the Arduino.The reason I am using a 12V source is because I have some high powered LEDs that require the 12V input to power. Elsewhere in my circuit I am driving those LEDs with solid state relays from the Arduino.Please let me know what you think about this?

author: doublec4
author_rank: Regular Contributor
timestamp: May 17, 2019, 01:29:49 am
content: Quote from: Ian.M on May 17, 2019, 12:36:04 amVery cool, thank you Ian!Some questions about this circuit, bear with me please, I'm learningWhen the circuit is "ON" and the button is pressed, I understand that the GPIO has been set to PULL_UP and will detect the button push as an active low. But what effect does this have on the N MOSFET? Will it temporarily turn it off? I understand the state of the P MOSFET won't change as the gate continues to be pulled low even with a momentary push. But now when the button is released and during the time it takes for the GPIO to switch back to 5V PULL UP high, does this create any unwanted effects? Or is this where C1 somehow comes into play? If that is not the purpose of C1, can you explain what it is doing? I understand that R1 is pulling the P MOSFET high when the circuit is "OFF".Also... let's say I wanted to "have my cake and eat it too..." and I wanted the ability to kill the whole circuit if the Arduino freezes up like in the example Mark posted...So let's say my Vin goes to my 5V buck regulator... Can we somehow implement an RC circuit from my regulated 5V line to the N MOSFET gate line? That way a long button press could drain the RC circuit, essentially forcing my regulator output low, then causing the arduino to die, thereby the GPIO and that gate to all go low... would that turn off the P MOSFET? Maybe its a silly question? EDIT: I feel like although the regulator would short to ground and the arduino would likely turn off, it would turn right back on after the long button push is released because the P channel hasn't been turned off.Thanks again to everyone for the great examples and discussion so far!

author: Peabody
author_rank: Super Contributor
timestamp: May 17, 2019, 01:38:42 am
content: Quote from: doublec4 on May 17, 2019, 12:10:38 amYes, that's right. The Arduino pins are protected from high voltage, and this method can shut off power to the regulator, so the entire circuit is truly off. Also note that the Arduino could power down the circuit entirely on its own so long as the push button is NOT being pressed.I don't remember why the capacitor is there, but here is the video from which I screen captured the circuit. It may be useful for you to follow what he's doing from the beginning, and you will see that he takes at least one wrong step, but finally arrives at the working system thanks to that second diode.You may need to pick a different MOSFET to deal with the 12V supply, but otherwise, I think this works pretty well, and I don't think it could be much simpler.

author: doublec4
author_rank: Regular Contributor
timestamp: May 17, 2019, 03:27:09 pm
content: Quote from: Peabody on May 17, 2019, 01:38:42 amFrom watching the video, one thing that I did not expect from the behavior of the P channel MOSFET was around the 5:00 minute mark, he only has to momentarily pull the gate low ("de-energize" as he says) and then it effectively latches even when floating, allowing current to flow to the LED. The same thing applies for when he pulls the gate high for a second, it energizes the gate and now stays "OFF" when floating.Obviously with the gate constantly trying to get pulled high from the 1M resistor from V+ to gate it will always default to OFF when the gate isn't pulled to GND.

author: Peabody
author_rank: Super Contributor
timestamp: May 18, 2019, 02:21:53 pm
content: Quote from: doublec4 on May 17, 2019, 03:27:09 pmYes, in theory if the gate is charged, or discharged, it will stay that way if not connected to anything. It might be interesting to see how long it would stay on. But I've never seen a design that relies on that, even for short periods. And as you say, in this case the mosfet needs to be biased off, which the 1M resistor does.A couple other comments:With regard to Ian.M's idea of using the same I/O pin to maintain power and read the state of the push button, you'll need to consider whether Section 14.2.3 of the 328P datasheet applies ("Switching between Input and Output"). The requirements included there have messed me up more than once.For turning off the power if the processor freezes up, remember that this is pretty much what the watchdog timer was invented for. So you might take a look at that option.

author: Ian.M
author_rank: Super Contributor
timestamp: May 18, 2019, 06:00:31 pm
content: Quote from: Peabody on May 18, 2019, 02:21:53 pmSection 18.2.3 in the full datasheet.That's one of the reasons I included the 330R resistor., as accidentally shorting a high output to ground via the button during development is undesirable. However, even without it, its manageable. At power on the GPIO is an input so simply set the port bit to enable the weak pullup. It remains an input till you need to power off. Disable the pullup by clearing the port bit, and it will shortly power off due to the 1Meg resistor discharging the NMOS gate. To speed up the power off, immediately after disabling the pullup, make the GPIO an output, and it will output 0 and rapidly shut off the MOSFETs.N.B. For the button to be used as an input without problems after the button is released, C1 & R1 must be chosen to provide a long enough time constant to keep the P-MOSFET on while the GPIO pullup is recharging the N-MOSFET gate to turn it back on.

author: Zero999
author_rank: Super Contributor
timestamp: May 18, 2019, 08:43:32 pm
content: The input to the regulator needs to be switched, rather than the MCU.Here's a quick sketch. I didn't bother simulating the MCU or code. It's just a voltage source which turns off, after a second from when it's turned on. A pull-down might be required on the MCU output, to stop it from floating high, after the power has been removed. The short transients over 6V are nothing to worry about, as the MCU will be designed to resist such short overvoltages, without any damage.There's no need for a MOSFET with a maximum gate voltage over the supply voltage, just use a potential divider.

author: doublec4
author_rank: Regular Contributor
timestamp: May 21, 2019, 01:04:02 pm
content: Quote from: Peabody on May 18, 2019, 02:21:53 pmThanks, I looked into the WDT and it seems like a viable solution for a hang up... I suppose on a reset, depending on the capacitance of whatever is keeping the P MOSFET gate on, the output driving the N channel would go low since the uC is resetting and the whole circuit may just turn right off. Then it would require user input (button press) to turn back on. Does this sound right?

author: doublec4
author_rank: Regular Contributor
timestamp: May 21, 2019, 01:15:58 pm
content: Quote from: Ian.M on May 18, 2019, 06:00:31 pmGood to know! Is there somewhere in the datasheet for the N MOSFET that I should be looking to see how long it would take to charge the gate at a particular voltage/current?Also, with regards to the WDT the same strategy could be applied to this circuit to kill power on a code hang up? The WDT reset could potentially take long enough to drop the high output on the GPIO and kill the whole circuit... or the decay with the 1M resistor could be slow enough that it might survive the reset and turn on in time to continue charging the gate...

author: doublec4
author_rank: Regular Contributor
timestamp: May 21, 2019, 01:31:12 pm
content: Quote from: Zero999 on May 18, 2019, 08:43:32 pmThanks for putting that together! I'm going to try and walk through my understanding of the circuit, please let me know if my interpretation is correct...Okay, so similar to the other circuits we have a P channel whose gate is being biased high when everything is "OFF" and U1 is off as a result.Then the button is pressed and this drives the gate on M2 high, pulling the gate on M1 low through the voltage divider R1/R2... C1 should also get charged in the process? Even when the button is released, C1 should keep M1 on long enough to drive the MCU_OUT high and latch M1 on through M3.Now I am a little unclear of what is happening with MCU_IN and how it would see ~5V using the shottky and R3. Is this some kind of resister/diode divider or what is happening there? Any explanation on that part would be really appreciated!Looks like I'm going to have to buy a bunch of components and bread board some stuffAlso going to have to figure out how to use LTSpice!

author: Peabody
author_rank: Super Contributor
timestamp: May 22, 2019, 01:37:23 am
content: I'm not sure what options you have for the WDT on Arduinos. The AVR datasheet says it can be configured to reset the system when it times out, or to generate an interrupt (presumably non-maskable). In interrupt mode, you could just program what you want to happen if things ever get there. But I don't remember whether the standard Arduino fuse settings lock it into reset mode. You'll have to research that.

author: Zero999
author_rank: Super Contributor
timestamp: May 22, 2019, 09:07:08 am
content: Quote from: doublec4 on May 21, 2019, 01:31:12 pmYes, that's right.Regarding MCU_IN: R3 limits the current and the Schottky diode starts conducting, if MCU_IN exceeds 5V, by about 0.3V. In other words it acts as a voltage clamping circuit.

author: doublec4
author_rank: Regular Contributor
timestamp: May 22, 2019, 01:11:01 pm
content: Quote from: Peabody on May 22, 2019, 01:37:23 amI will look into it, thanks!

author: doublec4
author_rank: Regular Contributor
timestamp: May 22, 2019, 01:23:04 pm
content: Quote from: Zero999 on May 22, 2019, 09:07:08 amThanks for the reply again. I looked up voltage clamping circuits and came across quite a few that look like the one I have attached.There is an extra diode to GND with many of these ESD clamping circuits. I'm trying to find a good explanation of how/why it works but am still searching. In this circuit, can you explain why the extra diode to GND is not present? In the meantime I am still searching online for the explanation of how this works (how to select the diode(s)/resistor to get the desired voltage/current limit to the MCU_IN pin))Thanks again for everything, this has been really helpful so far. Learning lots!

author: Zero999
author_rank: Super Contributor
timestamp: May 22, 2019, 01:59:05 pm
content: Quote from: doublec4 on May 22, 2019, 01:23:04 pmThere will be one of those clamping circuits, inside the MCU, for every IO pin. It shorts any input voltages outside the supply rails. If the current is too high, it will either destroy the diode and/or cause the MCU's power supply voltage to increase or decrease, depending on the polarity of the pulse.The idea behind using a Schottky diode was you don't have to rely on the MCU's input protection diodes. No additional diode to 0V is required, because the pulse is only positive, never negative. D1 could be removed to use the MCU's internal protection diode. R3 limits the current to a safe level.

author: doublec4
author_rank: Regular Contributor
timestamp: June 04, 2019, 01:51:23 pm
content: Okay, so I wanted to follow up on this thread. I went ahead and bread boarded all of the circuits presented here as well as a few others I found on the web... I did my best to find equivalent through hole components for the breadboardSome notes...Peadbody's circuitBC414 was replaced with BC547ATACTAfter long periods of being disconnected from any power supply, the first time the +/- leads are connected, the circuit turns on automatically.After that, everything works predictably. If you disconnect the +/- power supply leads from the circuit and reconnect them again within ~minute or so, the circuit does not automatically power onMarkF's circuitIRF7319 was replaced with IRF7309BAV99 was replaced with 1N4454uC turns on momentarily when button is pushed but does not latchPlayed with a few resistor and cap values on the switched power side to see if I could get it to latch but could not. Triple checked by circuit and everything looks good... not sure why it won't latch.Ian.M circuitIRF7207 was replaced with SFT1342-WBSS123 was replaced with TN0610N3BAT54 was replaced with BAT85SWorks as expected, no weird behaviorZero999 circuitFDC638P was replaced with NDP6020P2N7002 was replaced with VN2222LLBAT54 was replaced with BAT85SSimilar to Peabody circuit, after long periods of being disconnected from any power supply, the first time the +/- leads are connected, the circuit turns on automatically. Everything works as expected after this.Then I tried pulling down the uC output line with 10K resistor, and this somewhat eliminates the problem of automatic turn on after long periods of being disconnected. Now after long periods, connecting +/- to the circuit the uC turns on for a split second and then turns back off almost immediately. From here, everything works as expected again.Thank you to all for your contributions! If you guys have any more ideas as to why some of these circuits will "auto-on" the first time they are connected to a power supply please let me know.

author: Peabody
author_rank: Super Contributor
timestamp: June 04, 2019, 03:45:07 pm
content: In the case of my circuit, I suspect the problem is that if the capacitor is completely discharged, then when power is connected it will take some time for the charge to build up through the 1M resistor. During that period, the mosfet will be ON, and if that takes long enough, the processor will turn on and stay on. My understanding from the video is that the capacitor is there only so you won't need to hold the button down until the processor boots up. If that's not important to you, then you might try it without the capacitor and see if that fixes the auto-turnon problem.

author: Zero999
author_rank: Super Contributor
timestamp: June 04, 2019, 06:32:11 pm
content: Quote from: Peabody on June 04, 2019, 03:45:07 pmMy circuit has the same issue, for the same reason.No need to omit the capacitor, just move it.

author: doublec4
author_rank: Regular Contributor
timestamp: June 05, 2019, 11:38:18 am
content: Thanks to both.Zero999 I moved the cap as per your suggestion and success, that worksPeabody, I basically did the same thing, I took the 2.2uF cap and moved it in parallel with the 1M resistor and it works flawlessly now as well.

author: Zero999
author_rank: Super Contributor
timestamp: June 05, 2019, 12:34:14 pm
content: Yes, I should have set the simulator to start with the supply voltages at 0V, which would have shown this issue.

author: Peabody
author_rank: Super Contributor
timestamp: June 15, 2019, 10:38:51 pm
content: I see that doublec4 (Chris) has put all these designs into a video, and posted a link to that in the Tutorials section of the Arduino Forum. Nice work, Chris. Here's the video link:He likes Ian.M's circuit best, and I do too.I guess the next step is to integrate a battery charging circuit into this. :-)

author: doublec4
author_rank: Regular Contributor
timestamp: June 16, 2019, 03:01:51 am
content: Thanks! Couldn't have done it without you guys!

author: chris.munc
author_rank: Newbie
timestamp: July 24, 2019, 02:25:33 pm
content: Oops I am trying to design for a 9 volt battery, as i said I'm a newbie. Will start looking for that on the forum.Hi all, I’m a bit of a newbie here, dabbled a bit, but have always been keen to try microcontrollers. Just bought myself an Arduino beginners kit, with the intention of building a simple ultrasonic distance comparator. In my wanderings of the internet I came across the above video (many thanks doublec4), and also found that I liked, Ian.M's circuit, so much so that I have reproduced it in tinkercad. I’m not sure if this hyperlink will work for you but here it ishttps://www.tinkercad.com/things/k5xWaD0dLv1-attiny-soft-latching-power-supply-, otherwise just search for “ATTiny Soft Latching”, in the circuits section.Now for the real purpose of my visit, please could you chaps help me with alternative components. Would an IRF9530 work for the p mosfet, a 2N7000 work for the n mosfet, and two 1N5818’s for the Schottky diodes (again apologies, I would like to do this project with a 9v battery not 12v).Many thanks.PS here is a screen shot of the tinkercad project

author: Ian.M
author_rank: Super Contributor
timestamp: July 24, 2019, 07:31:23 pm
content: As long as the Arduino (or other MCU) is running at 5V, your choice of MOSFETs will be O.K. If you need to run at 3.3V, you'd need something with a lower max. gate threshold voltage than a 2N7000.Small signal Silicon diodes (e.g. 1N4148) would be better than 1A Schottky diodes - the leakage current and junction capacitance of 1N5818 may be excessive.

author: MrFollies
author_rank: Contributor
timestamp: October 28, 2021, 01:47:54 am
content: Quote from: Ian.M on July 24, 2019, 07:31:23 pmHello Ian..In your design (and the 9v idea above), what voltage is the GPIO seeing?I'm keen to use this circuit for both rPi and esp32 MCUs both of which are happily powered by 5V but don't want anything higher than 3.3V on the inputs.I can use a logic shifter I guess, but that might break the output design.I'd like to understand how to convert your design to work with a 5V power supply but with 3.3V tolerant GPIO pins.Is it possible?

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2021, 03:08:39 am
content: Due to the diodes, the GPIO pin can only be pulled low by the circuit, so it never 'sees' more than its normal logic '1' voltage no matter what the supply voltage is. With appropriate choice of MOSFETs you can use the same circuit for any supply voltage from 3.3V up to about 15V. Above 15V you'll almost certainly need a modified circuit to limit M1's Vgs to keep it well within its abs. max limits, just due to the limitations of having to find a P-MOSFET rated for >20V Vgs.The key parameter for low voltage operation is the MOSFETs threshold voltage - M1's needs to be well under Vbatt/2 so it can deliver enough current when the gate is grounded (i.e. when Vgs=-Vbatt, see datasheet curves to check it wont be edging into saturation), and M2's needs to be under half the logic '1' voltage.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 28, 2021, 08:26:45 am
content: Just replace the 2n7002 with a small signal bjt and a 10K resistor to its base. 2n2222, bc547, c945... will work down to ~1V

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2021, 10:55:32 am
content: Quote from: DavidAlfa on October 28, 2021, 08:26:45 amWhile true for most of the general power circuits with seperae GPIO pins for power control and button sensing, a BJT will specifically *NOT* work in my single GPIO pin circuit.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 28, 2021, 01:59:24 pm
content: No idea what you're talking about...

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2021, 03:54:17 pm
content: Quote from: DavidAlfa on October 28, 2021, 01:59:24 pmFrom reply #6 aboveIts not possible to replace M2 with an ordinary BJT as per your circuit, as its base current will be significantly higher than that available through the GPIO weak pullup when its set as an input, which will pull down the GPIO, so it will no longer be possible to use the same GPIO to sense the switch state.Buffering the GPIO so it doesn't get pulled down by the BJT base current is possible, (e.g. add a super-Beta emitter follower to drive the base resistor), but adds more complexity vs simply choosing an appropriate MOSFET.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 28, 2021, 07:21:41 pm
content: Circuit complexity? That circuit is already over-complicated.So your gpio sets the internal weak pullup, M2 will be turned on all the time, which will also turn M1 on.When S1 is shorted, M1 will keep turned on as long as the button is pressed, without any way to turn it off until it's released, which is a bit scary depending on what M1 is controlling.After that, you read the gpio, if read low, you set the gpio as output, low level (or turn off the internal pullup), switching off M2, and opening M1 when S1 is released.Now you'll never now when S1 is pushed again...?Anyways, you can do the same thing much easier... Use a high gain npn bjt like BC547C, which is extremely common.100K is the internal pullup, being the GPIO directly connected to the base.There's a small difference: The switch instantly turns off the output.If you want to keep M2 on while the switch is pressed, then add the diodes like in your circuit.

author: Peabody
author_rank: Super Contributor
timestamp: October 28, 2021, 07:51:23 pm
content: DavidAlfa, I don't think you were around for the original posts. The idea was to have the pushbutton turn on the power, after which the processor would maintain it, and the button could then be used for general purpose input, but with a long press the processor would shut down the power. And all of this would be done with a single GPIO pin. I aways assumed the processor would wait until the button was released before doing anything so it could tell whether the press was long or short. Anyway, I tried to develop a circuit using an NPN instead of an N-channel mosfet, but wasn't able to do it. If you have such a circuit, I'd like to see it.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 28, 2021, 08:45:32 pm
content: I see problems in your circuit, but maybe it's me.The only input voltage source is its own pullup, so you need to have it enabled all the time... thus the mosfet will be always on.Yeah I seethe bjt problem....You would need a second transistor, making an AND gate, so the pullup only turns on the output when there's power on it.To turn off the output, turn off the pullup or set the pin as output, low level, for a short time.

author: Peabody
author_rank: Super Contributor
timestamp: October 28, 2021, 10:19:37 pm
content: Looking back at Ian's circuit, if the GPIO is configured as INPUT_PULLUP, that will be enough to turn on the N-channel and keep the power on. Then when you short the switch, that pulls the GPIO low via D1 which turns off the N-channel. However, via D2 it also directly grounds the P-channel gate, which keeps the power on even though the N-channel is off. C1 prevents any glitching as the P-channel gate is pulled low via the two different paths. So it actually works quite well, and I don't see another way to have the same functionality with fewer parts.While the internal pullup would probably be enough to turn on an NPN, the voltage at the base would be 0.6V or so, and I was not able to come up with a way to preserve the input function of the switch. I think Ian's circuit is the best solution I've seen for this, but you can watch the OP's video covering it and four other alternatives and see what you think.

author: Ian.M
author_rank: Super Contributor
timestamp: October 28, 2021, 10:24:18 pm
content: Yep, you don't read the input when M1 is off, as the MCU's supply is from downstream of M1!@Peabody: that's as good an explanation as any of my circuit.@all,If you are more visually oriented, see 11:22-14:43 in doublec4 (Chris)'s video for an explanation of its function.Chris's video, queued to my circuit:https://youtu.be/7D9L9oS4AJM?t=682I can *think* of a few ways of doing something similar with BJTs, but they are all horribly complex.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 28, 2021, 10:36:33 pm
content: Even more simple: Use self-latching. The MCU can turn off the mosfet at any moment by setting the gpio low.No fancy electronics, very readily available and cheap parts.Q2 will we switched on by the switch. The output will turn on Q1 and lock it up.Together, D3+Q1 b-e junction have a drop of 1.2-1.4V, thus not affecting the gpio input.When the gpio is turned low, there's only D1 in series with R4, so there'll be 0.7V, turning Q1 off and disabling Q2.There's only a drawback: You'll have to ensure the output discharges BELOW 1.2v or it will latch again,You could use an adittional fet to automatically discharge the output quickly(attached).Edit: Oh I see it's a power-on circuit for the mcu itself, not for switching loads.Then, likely you won't he discharging circuit. Just set the ouput low until it dies.You might replace D3 with a 2.4V zener to increase the turn off voltage.

author: z555
author_rank: Newbie
timestamp: November 11, 2021, 09:14:55 pm
content: There are some power management ICs out there which provide soft switch functionality at greatly reduced circuit complexity and footprint at the expense of well expense...Checkout:$1.80 per 100Maxim Integrated's MAX16150Quote$4.39 per 100Analog Devices' LTC2955Quote

author: CBF
author_rank: Newbie
timestamp: November 19, 2021, 10:16:06 am
content: I cannot get this circuit to work. It turns on correctly but when the arduino sets the output low to turn off the input floats to high. I cannot think of a solution and believe the problem is because the arduino is tri-state or my mosfet? Datasheet and my schematic:https://www.farnell.com/datasheets/2303831.pdf

author: Ian.M
author_rank: Super Contributor
timestamp: November 19, 2021, 12:03:43 pm
content: The Arduino output may not hold its state during loss of Vcc. If it glitches high it will re-trigger. If you've got a decent scope, look for glitches on the out pin.If you find a glitch, try splitting the 4K7 resistor into two 2K2 ones with a cap to ground in between to make a low pass filter, with a time constant an couple of orders of magnitude longer than the glitch so it has negligible effect.

author: Peabody
author_rank: Super Contributor
timestamp: November 19, 2021, 07:03:58 pm
content: Quote from: CBF on November 19, 2021, 10:16:06 amIf you mean the GPIO pin labeled INPUT in your schematic, that should always be high unless you are pushing the button. There's nothing pulling INPUT down. But if you mean the power doesn't shut down, then that shouldn't be happening. Are you keeping the output pin low for a while? Remember that you have to recharge the gate capacitor through that 1M resistor before the mosfet will turn off. After bringing the output low, try adding a 10 second Delay afterwards. If that still doesn't work, instead of taking the output low, try changing it to INPUT mode, still with the Delay.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: November 19, 2021, 07:29:54 pm
content: Just to belatedly add my own to this old thread --I did this, with the additional constraint that it run from 24V, with low leakage.The 24VEN goes to an LDO enable, which powers the MCU's converter (24-3.3V buck) and other hardware (12V for SMPS controllers). This circuit draws essentially MOS leakage current until enabled, and the load's minimum current draw is still many times the bias drawn by this when on, so it's no matter at all. It also monitors RS-232, so that serial commands can wake up the MCU directly. Filtering not shown; ESD protection is shown.Oh, the serial wake-up part is a bit ambiguous, isn't it... the input NMOS is wired-OR with the latching NMOS I believe? So the right half of that is duplicating the respective part of the right hand side. Yeah I don't have this thing in front of me right this instant to confirm, tsk tsk.Tim

author: Peabody
author_rank: Super Contributor
timestamp: November 20, 2021, 04:18:50 am
content: Just looked at the schematic again, and I think the 2.2uF gate capacitor shown in red is way too big. Its only purpose is to prevent the circuit from powering up when power is first connected. But for that I think .01uF or even smaller would be enough. Try removing that cap, and then see if the circuit powers down properly. And the 1M resistor may be too high if there's any leakage through the NPN. Try 220K or even 100K and see if that makes a difference. I think the circuit is sound, and should work, but you need to experiment a bit with different values.

author: CBF
author_rank: Newbie
timestamp: November 21, 2021, 05:14:13 am
content: I tried adjusting the gate cap values and the resistor with no luck. I successfully with a different circuit. I think a low pass filter on the base of the transistor may have worked as the output did spike to negative momentarily, as Ian P suggested. Thanks anyway.

author: Ian.M
author_rank: Super Contributor
timestamp: November 21, 2021, 09:31:50 am
content: @CBF: For the record, what circuit did you end up with that was successful?

author: CBF
author_rank: Newbie
timestamp: November 22, 2021, 05:09:57 am
content: Quote from: Ian.M on November 21, 2021, 09:31:50 amYes for the record! I took out the delay element and went with a circuit I found similiar to this with the mosfets I had handy and ditched the timing component for a hold down to turn on. My arduino boots up almost instantly. I added a pull-down resistor on the sense:

author: Ian.M
author_rank: Super Contributor
timestamp: November 22, 2021, 07:41:57 am
content: Yes, with a 6K8 pulldown on the SENSE input to keep the max. signal just under 5V, that looks reasonable. The only flies in the ointment are that the switched power isn't debounced so is likely to glitch during power up, and the switch has to handle the entire load current till the MCU turns the MOSFET on. An Arduino has enough onboard decoupling to ride through the power glitches, and if it doesn't have other significant loads on Vin or +5V is unlikely to draw enough current to be problematic for most small button switches, but it may not work so well for other applications.

author: Peabody
author_rank: Super Contributor
timestamp: November 22, 2021, 03:55:55 pm
content: I'd still like to understand why the first circuit in the video doesn't work for CBF. Below is a bit cleaner schematic but essentially the same except the capacitor is missing. The circuit worked in Chris' video and in the video the circuit came from, so I wonder what the problem is with CBF's circuit. Did Chris wait until the button was released before taking the output pin low? It doesn't look like that in the video. Anyway, is there any way to get to the bottom of this?

author: CBF
author_rank: Newbie
timestamp: November 22, 2021, 11:54:40 pm
content: Quote from: Peabody on November 22, 2021, 03:55:55 pmYes I would too. When I get some time I will play around with it a bit more, but first I have to finish this project.

author: Peabody
author_rank: Super Contributor
timestamp: November 23, 2021, 04:02:34 pm
content: Could it be switch bounce? I wouldn't think there would be much bounce when releasing the button. Well if I have time over the weekend, I'll set up the circuit with a Nano and see what happens.It seems the safest thing might be to wait until the button is released before turning off the NPN. It really shouldn't make any difference as long as you bring the output pin low and keep it low forever. Then everything should shut down when the button is released. But waiting for button release, if it still misbehaves, that would indentify the Nano as the problem - output pin glitching high as it powers down.

author: CBF
author_rank: Newbie
timestamp: November 26, 2021, 06:18:59 pm
content: Quote from: Ian.M on November 22, 2021, 07:41:57 amI can't believe I am still having problems with such a simple circuit. I have the arduino connected to a MOSFET controlled motor driver than can spike up to 1.5A. The circuit is unable to deliver this. Could this be the Rds of the MOSFET? The voltage seems to drop and the motors stall. When I remove the above circuit and power directly from the powerbank there are no problems.I need a solution quickly for this project and I'm struggling to get anywhere. I'm sure there is lots wrong thats why I'm here:

author: Peabody
author_rank: Super Contributor
timestamp: November 27, 2021, 12:11:40 am
content: I couldn't find an IRZ44N. If it's really IRFZ44N, then it's not a logic level mosfet, and if the source is at 5V, then the gate threshold voltage is way too high to turn on the mosfet fully when the GPIO controlling it goes to ground.

author: CBF
author_rank: Newbie
timestamp: November 27, 2021, 04:04:36 am
content: Quote from: Peabody on November 27, 2021, 12:11:40 amThanks so much, I had it lying around and remembered it being logic level, doh! I should have checked the datasheet - this explains my probblems with my first circuit. Will update

author: zbyhu
author_rank: Newbie
timestamp: December 14, 2021, 03:25:46 pm
content: Hi All,I have a question to Ian.M's circuiit.I used DMG1013UW as M1 and BSS214 as M2.Vbatt is around 3.8V.What I observe is that M2 is turned on right after power is connected.D1 and D2 are SKM14 from DIOTEC.Double checked the MOSFETs pinout and it is correct.Why circuit behaves like this?