In this article we’ll show you how to build a Latching Power Switch Circuit, also know as an Auto Power Off Circuit. You can use this circuit to auto-power off the ESP32, ESP8266, Arduino, or any other microcontroller.
This circuit allows you to cut off power completely when the microcontroller is not executing any task. In other words, as soon as the microcontroller finishes executing a task it turns itself off via software. This is a great way to make batteries last longer in your electronics projects.
Overview
Before proceeding with this tutorial, here’s a high-level overview of what we’re going to do:
- When you press the circuit’s pushbutton or close the circuit using any other components, there is power driven to the microcontroller. So, your ESP32, ESP8266 or Arduino turn on.
- You set the LATCH pin (set in the code) to HIGH to keep the circuit powered on.
- The microcontroller executes its tasks. In our example, it does nothing – it simply waits 10 seconds. You can modify the code to execute a useful task.
- Set the LATCH pin to LOW, so the microcontroller auto powers off.
- When the LATCH pin is set to LOW, the power is cut off.
Auto Power Off vs Deep Sleep
The auto power off circuit cuts off the power completely. So, there is no power consumption when the microcontroller is not executing any task.
In deep sleep mode there is much less power consumption than the active mode. However, there is always power consumption because your microcontroller is always being powered on (for an introduction to deep sleep with the ESP8266, you can read the following article: ESP8266 Deep Sleep with Arduino IDE).
Parts Required
To follow this tutorial you need the following parts:
- Microcontroller or Development Board, for example:
- NDP6020P Transistor P-Channel MOSFET
- 2N3904 Transistor BJT NPN
- Resistors: 220K Ohm, 2x 100K Ohm, 10K Ohm, and 220 Ohm
- 2x Diodes (for example: 1N5819)
- Switch/Pushbutton
- 5V Power Supply
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Auto Power Off Circuit Diagram
The following circuit diagram shows the Latching Power Switch Circuit (Auto Power Off Circuit) diagram.
The terminals at the right numbered with 1, 2, and 3 should then be connected to your microcontroller board.
- Pin 1 connects to 5V.
- Pin 2 can be connected to any digital pin of the microcontroller. In our example, we’ll connect that pin to GPIO 5 / Digital 5.
- Pin 3 connects to GND.
How the Auto Power Off Circuit Works
1) When you press the switch or close the circuit, there is power reaching the base of the 2N3904 transistor. So, the 2N3904 is pulled low taking the gate (G) of the MOSFET to GND.
2) The P-Channel MOSFET turns on when its gate is negative relative to the source. When you press the button, the gate of the MOSFET is pulled to GND, allowing current to flow to the VIN pin, which will power the microcontroller. This happens as long as the MOSFET’s gate is pulled to GND.
3) To keep the MOSFET’s gate pulled to GND after releasing the pushbutton, we send a HIGH signal through a microcontroller’s GPIO. When we send a HIGH signal, there is power reaching the base of the transistor.
4) Therefore, we ensure that the MOSFET’s gate is pulled to GND, and current flows to the VIN terminal to power our microcontroller.
5) When we want to power off the circuit, we simply need to set the GPIO to LOW. When that happens, there isn’t power reaching the base of the transistor, so the MOSFET doesn’t let the current flow to the VIN pin, and there isn’t power consumption.
Auto Power Off Circuit – ESP32
Here’s how to wire the circuit if you’re using an ESP32.
Auto Power Off Circuit – ESP8266
Here’s the diagram for the ESP8266.
Auto Power Off Circuit – Arduino
Finally, here’s how to connect the latch circuit to the Arduino.
Uploading Code
Before proceeding with this tutorial you should have your Arduino IDE prepared. Follow one of the following tutorials to install the ESP32 or ESP8266 on the Arduino IDE, if you haven’t already. If you’re using an Arduino you don’t need to install anything else.
- Installing the ESP32 Board in Arduino IDE:
- Installing ESP8266 Board in Arduino IDE
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// Define power latch pin for ESP32 (GPIO 5) / ESP8266 (GPIO 5) / Arduino (Digital 5)
const int powerLatch = 5;
void setup() {
// Define pin as an OUTPUT
pinMode(powerLatch, OUTPUT);
// Keeps the circuit on
digitalWrite(powerLatch, HIGH);
// Waits for 10 seconds
delay(10000);
// Turns the power latch circuit off
digitalWrite(powerLatch, LOW);
}
void loop() {
}
How the Code Works
Let’s take a closer look on how the code works and how you can use it in your projects.
We start by defining the power latch pin. We’re using GPIO 5, but you can use any other pin. This GPIO is connected to the latch power circuit pin 2 terminal.
const int powerLatch = 5;
In the setup(), we define the power latch pin as an output.
pinMode(powerLatch, OUTPUT);
Next, we set the power latch pin to HIGH. When we set it to high, we ensure that there is power coming to feed the microcontroller.
digitalWrite(powerLatch, HIGH);
Next, we wait 10 seconds.
delay(10000);
After that, we set the power latch pin to LOW. When it is set to low, the power is cut off, and the microcontroller turns off.
digitalWrite(powerLatch, LOW);
You should add the task you want to perform after setting the power latch pin to HIGH and before setting it to LOW.
Wrapping Up
The Latching Power Switch Circuit, also know as an Auto Power Off Circuit allows you to turn off your microcontroller via software, whether you’re using an ESP32, ESP8266, Arduino, or any other board. This circuit is specially useful to save power: the microcontroller turns itself down after executing a task.
In our example, we’ve used a pushbutton to close the circuit to provide power to the microcontroller, but you can use any other component to close the circuit – like a reed switch for example. We hope you’ve found this tutorial useful and you’re able to use it in your electronics projects to save power.
For more electronics projects, you can take a look at our repository of more than +200 Electronics projects and Tutorials or enroll in our premium courses.
I think there is a problem in the schematics digram.
The point is related to the npn transistor and the P channel Mosfet.
What do you think ?
Thanks
Ambrogio is right. You need to connect the NPN emitter (1) to the GND to set the gate to zero to open the PMOS.
Thank you both Jiri and Ambrogio.
I has a small mistake and the schematics are now fixed! Thanks for letting me know
hey i have a doubt that this circuit can only be off by microcontroller only …this don’t go off when push button is pressed 2nd time after on
i am right or not ??? plss clearify
I haven’t built it, but it doesn’t look right to me. Normally in an NPN transistor, current flows from collector (pin 3 in the schematic) to emitter (pin 1) in the direction of the arrow. So I don’t understand how it would work with pin 3 at the lower voltage (ground).
Exactly. It’s now fixed!
Isn’t that MOSFET a bit of an overkill?
24A continuous seems a bit too much headroom, even for the pulse current of an ESP…
This my opinion of the circuit:
The circuit is correct and the mosfet is a perfect solution. If you should use a transistor, there is always a voltage drop across the transistor of about 0.7V. The resistance of the used mosfet is 0.05 ohm . Thus almost no voltage drop across the mosfet .The used type is a low voltage type.
All together it is a good choice.
Hello,
Wonderful tutorial. Now suppose I want to create this on a PCB, what can I use instead of NDP6020p? As i searched, it is available only in TO-220 and TO-263 package which is very big. Can you suggest any other SMD MOSFET? I can design the PCB and open-source it to the RTN community so it may become useful.
Thanks! You can try to use a different P-Channel MOSFET with similar specs and it should work exactly the same
Smaller SMT… DMG2305UX-7
ebay.co.uk/itm/5-X-MOSFET-P-CH-20V-SOT-23-3-DMG2305UX-7-Fnl/272684012161?hash=item3f7d3bd281:g:~6EAAOSwlMFZJIpe
Wrong current flow!!! And you must change transistor emitter with collector! Current will flow to ground and gate voltage will be low to turn on!
Thanks. It’s now fixed!
Hi Rui,
Thanks for the circuit, it’s exactly what I was looking for.
Where do I find the schematics with all the fixes ?
Also do you have layout for the latch circuit ?
Thanks,
Danny
I think the collector and emitter of the npn transistor are reversed.
Thanks. It’s now fixed!
C and E of 2N3904 must be swaped of course. 220 ohm in B is too small (cca 20mA base current?), 10k will be OK.
Thanks. It’s now fixed!
The p-channel mosfet turns on when its gate is negative with respect to its source. This occurs when the gate is pulled to ground. For this to happen, the 2n3904 emitter should go to ground and the collector should go to the gate of the fet. When the button is pushed the 2n3904 will turn on and tie the fet gate to ground which will turn the fet on and supply power to the microcontroller. Before pushing the button for the first time, the transistor is off and there will be no current flowing through the 100K gate to source resistor. Therefore the gate to source voltage is zero and the p-channel fet is off.
Exactly. We made a small mistake while designing the schematics and it’s now fixed! Thanks for letting me know
the battery needs to be turned over – to swap places + on –
The battery is correct…
Nice subject I hop next Tuto is How to start esp32 via web site for example (domotics)
How to turn this circuit on with Domotics? I’m not sure if I understood your question…
Probably, pin1 (emitter) and pin3 (collector) of 2N3904 have to be exchanged. Moreover, I ‘d recommend to place additional resistor 10-20kOhm between base of the 2N3904 and ground.
Exactly. We’ve made a small mistake while designing the circuit, but it’s now fixed and all the schematics should be correct.
Thanks for letting us know.
With this circuit I can switch the micro off…. If I need to turn it on manually, I could also switch it off afterwards 🙂 Normally I would use an ESP for doing things automatically, repeatedly….
I would look for a real time clock chip (RTC, normally extremely low standby current) with a pin that toggles/goes up/down on alarm… to allow the micro to turn on again after a programmable time. This would have the advantage of having a good source for time (stamps) too.
As I understand the controller is doing one task or more and than switches itself off and it can only be started manually…….. Could be ok for some applications but not for many as the controller has to be started on manual again or by another device. Somebody knows a good pointer in how to get an esp8266 in deep sleep?
Hi.
For a deep sleep tutorial with the esp8266, you can read:
https://randomnerdtutorials.com/esp8266-deep-sleep-with-arduino-ide/
Nice article and good topic! Couldn’t you simplify the circuit by just connecting the switch between the PMOS gate and ground, then putting the collector-emitter of the NPN in parallel with the switch, adding a resistor on the base, or else substituting an nmos for the npn without the need for a resistor. Then you can eliminate 2 diodes and 3 or 4 resistors.
One limitation of the circuit is that you can only use the button to power on the MCU. It would be nice to use the same button to power off. One option would be to use a DPST switch and connect the second pole to an MCU input, but DPST switches are not so common and more expensive. You could put a diode in front of an MCU input (to prevent current flowing to the MCU) and use an analog input to detect either the 0V switch or .2V VCE of the transistor. But then you probably need a pull up resistor on the analog input. Or you can use 2 diodes, one between the PMOS gate and the switch to ground, and another between the switch and MCU input. I think you would also need a separate pullup to the switch and diodes. Then you have a digital readout of the pushbutton (using internal pullup) by adding 2 diodes and a resistor.
Oops, no you shouldn’t need a second pullup to get a separate digital input of the switch state, just 2 diodes (the internal pullup on the digital input is all you need). Software to power off is a little tricky, as you first need to wait a bit on power on for the switch to be off for a while, then when detecting a power off request, wait for the switch to be released for some time (to debounce) then power off.
What’s the purpose of the 220K ???
It’s just a pull-down resistor for the pushbutton
I must be misunderstanding something. In figures 1 and 3, you show current flowing out of pin 3 (collector) of the 2N3904. How can current flow in that direction in an NPN transistor? If pin 1 (base) of the 2N3904 it will switch “on” and pin 3 will essentially be a ground level, as would the gate of the NDP6020P.
In the circuit description, current doesn’t flow “up” from the transistor to the mosfet gate, it flows “down” from the gate through the collector/emitter junction to ground.
This is a generic circuit concept seen in inexpensive switching regulators(flyback convertors) like mobile chargers, oscillating(high frequency) transistor circuit with both current and voltage(feedback) control with another transistor…
How the Auto Power Off Circuit Works
1) When you press the switch or close the circuit, there is power reaching the base of the 2N3904 transistor. So, the 2N3904 emitter goes to GND. When this happens, the gate (G) of the MOSFET is pulled to GND.
Correction the 2N3904 emitter is already bonded to ground. Should say “the 2N3904 collector is pulled low taking gate of the MOSFET to ground.”
Hi Philip.
Thank you for you suggestion.
I’ve updated the explanation.
Thank you.
Regards,
Sara 🙂
EXactly what I designed in a CCT. to use a clock chip (don’t remember it’s number) but it pulsed for one second every minute. Then, the micro (XMEGA) woke up, and determined – by reading the time – whether it had something to do that minute – if not, it’d shut down. Whole process took about a millisecond. Incidentally, I’d change the N-transistor for an N-fet. In my application, power was EVERYTHING! A microamp here would let the thing run longer.
Nice circuit.
Would you have to press the switch long enough for the processor to boot up and initialize pin#5? Alternatively, do you need a capacitor to keep the PNP on long enough for this to happen?
Thanks!
Mike
Hi Mike.
The circuit works great as it is presented.
When use this circuit with the Arduino you have to press for some time and wait for it to initialize.
When using the ESP32 or ESP8266 it works great with a simple press, it instantly initializes when you press the pushbutton.
Regards,
Sara 🙂
Great tutorial!
Have you measured current flow during off state?
Hi.
Thank you.
Yes, we’ve measured current flow. And there isn’t current flow in off state.
There are other circuits that also work great, but they consume a little bit of current.
This circuit doesn’t consume – which is great!
Thanks.
Regards,
Sara.
To turn on, use TPL5111.
Hi.
That’s also a great way to manage power.
I wasn’t aware of that. Adafruit has a TPL5111 Low Power Reset Timer breakout board, and it looks awesome.
Thank you for sharing!
Regards,
Sara 🙂
I thought I would take the time to learn how to use CircuitMaker … decided to use Rui’s “Latching Power Circuit” as a test run. If anyone wants the Gerber files you can get them here: circuitmaker.com/Projects/Details/G-E/Latching-Power-Circuit
You will need to download CircutMaker (free) and create an account. I changed things up a bit by adding a power-on LED and a mini USB as a power connector. Also, lumped some of the resistor values to reduce the BOM count.
Hi Geato.
That’s great! We’ve also been thinking about building a PCB with that circuit.
I’m sure that will be useful for many of our readers!
Thank you for sharing and keep up the good work!
Regards,
Sara 🙂
Hello, this is a great project.
I was looking for a solution to save as much as possible the battery of my connected button.
I got to make a system similar to yours but using a small 5v bistable relay and of course I’m not very proud to have used a relay even if it works!
I will test your system but I would have liked to know the minimum and maximum battery voltages that your system can support. Thank you.
Hi Serge.
We haven’t tested the minimum and maximum battery voltages.
We’ve used a 5V power supply and it worked well.
You have to experiment with other voltages and see how it goes.
Regards,
Sara 🙂
Thank you for your answer, I will try some batteries with different voltages (9v, 7.5v, 4.5v) because I have not found a 5v.
what is the current used? when not on?
Hi Jorge.
There isn’t current flow in off state.
Regards,
Sara 🙂
I have tried similar circuitswith arduinos, the problem I found was the arduino runs through the boot code and it is not guaranteed that any GPIO pin is high until the boot code is complete and your code runs. So unless you press the button until the boot code has finished, the power is removed and the arduino drops out.
Hi John.
Yes, in case of the Arduino, you need to hold the pushbutton for a while instead of just pressing.
For ESP32 and ESP8266 we just need to press and it works.
Regards,
Sara 🙂
I was about to post that i had the same issue because I am using a nano with this circuit and have to hold the button for approximately 2 seconds for the function to work. Do you think there is a way to add a capacitor to somewhere to overcome the necessity of a 2 second button press?
Hi, would this be compatible with ATtiny 85?
Regards
Geoff
Useful circuit.
I’ve found that it requires the switch to be held closed for several seconds to allow an Arduino to power up properly so that it latches on.
You can get round this by putting a capacitor (200uF)across the 220K resistor to ground. This charges when the switch is closed and has enough power to hold the 2N3904 on until the GPIO pin input can be brought high.
Hi Pete.
Yes, in case of Arduino you need to hold the button to power it up properly.
Thank you for sharing your solution!
We need to try it out.
Regards,
Sara 🙂
Hi Sara
Also, if you feed the power from the circuit into the RAW (VIN) pin on the Arduino rather than the 5v pin you can increase the input voltage as you will be using the built in power regulator.
I’m currently running mine off a 9v supply.
I have tried this with a 220uf cap and it only knocks off a split second. I took it all the way to 2000uf and it only knocked off 1 second.. What am I missing?
Does it also work with 3,3V power supply ? For example with an attiny85
Yes, it should definitely work with any microcontroller.
Hi, ive got this all working on a 5v power source with ESP8266 (wemos D1 R2 mini). but 3.3v doesnt work. Esp8266 wont power on. Anything i should test or thing about. Ideallt i;d like to use this for a postbox notifyer running off single 18650 battery with 3.3v LDO.
Hello James, unfortunately I can’t give you a good answer, because I’ve tested your exact setup and it worked on my end. I’ve used 3.3V with a WeMos D1 Mini…
Just use espdeepsleep man. You’ll get years out of that 18650. The heat in the summertime will kill that battery before the WeMos drains it.
Hi Rui, always thanks for your time. I am trying to make this circuit, however, using alternative components, since I do not have the ones you indicated in the house. My modifications concern the 2N3904 transistors with BC547b and the 1N5819 diode with 1N5817 or 1N4007. Do you think I may have problems?
Ciao Rui, grazie sempre per il tuo tempo. Sto provando a realizzare questo circuito usando però dei componenti alternativi, dal momento che non dispongo in casa di quelli da te indicati. Le mie modifiche riguardano i transistor 2N3904 con BC547b e il diodo 1N5819 con 1N5817 oppure 1N4007. Pensi che potrò avere problemi?
Can you somehow invert this? That power gets fed only when circuit is open? Useful for using magnetic door sensor for triggering an alarm.
because reed switch is closed when magnet is near (door closed) but I would rather have this inverted
Hello,
I am making some test with the NDP6020P p-mosfet with a simple resistor (19.5ohm) as you can see here:
circuitlab.com/circuit/bcc7d46ym8qc/p-channel-mosfet-ndp6020p-test/
the problem is that if i use a 3.3v dc voltage the max current i get is only 60mA and not 169mA as espected, any idea on why?
60mA is really tho small to power an ESP8266 i think.
I have attempted this setup with an esp01 (3.3v battery). The red led flashes on when the button is pressed but no program seems to be executed – and the latching circuit never seems to turn of – i’m using the GPIO2 of the esp which coincidentally is HIGH on boot. If i however only connect GND, VCC and the CH_VD pin, that is leaving out the GPIO2 pin and press the button for the duration of the code execution (sending and email) everything seems fine.. anythoughts?
Can this work with a 12V power source?
I’m working on a Raspberry Pi project which I would like to fully turn off via software. I want to put the soft latch mechanism before the DC-DC converter that I’m using for powering all components (Raspberry Pi, 10” screen, etc), and I’m powering that DC-DC converter with a 12V power source. The maximum load will be less than 30W.
Thanks!
With Rui’s suggested hardware, then yes, the P-channel MOSFET used as the high side switch is good for 20V.
The DC-DC converter is supplied its input power by this mosfet, make sure you don’t feed 20v to your Pi! Once your device has booted and supplying a base current to the 2N3904 transistor, this will keep the mosfet on. The problem as I see it is the Pi takes some time to boot and drive the GPIO pins, so the “start” button will have to be held closed until this point!
G
I’d like to use this as a power saving circuit for a mailbox notifier – it works great, but there is one problem – if the trigger stays high (e.g. mailbox lid stays open for some reason) there is no power off as it gets constant triggering. Any ideas to alter the design to trigger only on rising edge? The objective would be that it triggers on high, but would then need to go low first before next triggering again…
have you found a solution ?
I added ESP.deepsleep(0) to the end of the code like this
digitalWrite(powerLatch, LOW);
ESP.deepSleep(0);
That way if the switch is held closed – I’m using a microswitch – the wemos will at least go to deep sleep instead of powered on. Once the switch is opened again the mosfet switches off.
Cheers
It look very interesting. But I would like to use the RTC alarm to trig the latch but it low active. How can I modify the circuit to use low active RTC alarm instead of push button?
Any suggestion would be appreciate.
Am designing a “busy box” for my 15 month old grandson and going to use this schematic but need to use smd.
Will include the 200uf cap across the 220k resistor.
any suggestions on a suitable mosfet using smd.
Hey,
In this circuit you will be never able to switch off power supply if you use normal switch or reed switch ? If you hold push button, or you bring magnet close to reed switch or you use power on switch, your board will wake up execute program turn off the power supply, but in same moment you put GPIO low, current will again flow to base of transistor and then all will repeat. Im I right?
There is a problem. When I press the switch the circuit power ups the micro controller as expected but when GPIO is set and power switch is released the power gets cut off. In order to debug. I set the GPIO pin and circuits output the set 4 volts.
The problem is when switch is continuously pressed and GPIO is set in that case releasing power switch cut off the power.
What is going wrong here with me. GPIO voltage is 3v and all components assembled as is shown in the diagram.
It seems like Sudden voltage change at the output due to different GPIO voltage and Battery voltage. This sudden change in voltage reset the ESP32. Even though out put goes through LDO
if the button is held down, the npn transistor is powered by both a 5v battery source and a 3.3v gpio pin source. Is it safe to power a transistor from 2 different voltages?
Hello,
Wonderful tutorial.
The diagram is up to date?
Thanx
Great tutorial! I found a way to do it without needing diodes that seems to work pretty well.
Basically, when the tactile switch is closed and the MCU turns on, the GPIO pin turns on an optocoupler. The optocoupler acts as a switch, providing an alternative route for the current, around the tactile switch. i.e. the collector of the optocoupler is connected to +5V, and the emitter is connected to the output side of the tactile switch. When you want to turn the MCU off, just set the GPIO to low and the optocoupler turns the whole circuit off. Also, if using an Arduino, just disable the bootloader so the GPIO activates immediately and you wont need to hold it down.
Once again, can you please tell me how do you use a reed switch to trigger the circuit? In the video there is a brief shot near the end of this happening when someone removes the magnet and the circuit comes on. I gather this is because it causes the trigger signal to go high on the base of the first transistor Q3 which in turn does the same of the second transistor Q2 turning on the MOSFET etc. But wouldn’t the trigger signal at the base of Q3 stay high so the circuit never turns off? The video only shows it coming on but it does not show it going off again. I suspect that you really can’t use a reed switch at all?
Hi David,
You would need to use a magnetic reed switch with a normally closed contact “Held Open” by the magnet but closes when the magnet moves away. This configuration is “fail safe” as when the magnet is removed (or missing for any reason) the contact of the reed switch closes and energises the primary side of Rui’s circuit. Using a switch that will close in ANY EVENT that removes the magnet is fundamentally safe; a contact which opens when the magnet moves away you can never tell when or why the contact is open?
The reed switch contact when closing will power the device which must run code that then “latches” the circuit in the “ON” position, meaning that the magnet/reed switch can return to it’s open state but your microcontroller will mantian the latch until it has finished processing whatever code you need it to do. It then kills power to itself by releasing the latch input to Rui’s circuit when all the code is done and it is time to sleep again.
HTH, Garry
Thanks Garry but I really don’t understand this. Let’s say I want to put a reed switch (with ground, NO and NC terminals) on a door and I want to fire up the microcontroller whenever the door is opened OR closed. How would this work? It seems to me that if opening the door closes the NC contact this triggering the latching circuit, the latching circuit won’t cut the power to the microcontroller when it has done its job if the door is left open and, in fact, the power will continue to supply the microcontroller until the door is closed. So this would not be a solution to my problem where I want the microcontroller to send an MQTT message each time the door is opened or closed then power itself off until the next such event.
Hi, nice circuit.
Is it possible to change this circuit a little bit to power on the ESP with the 1Hz outputsignal from an RTCDS3231? I think to implement an LMC555, what do you mean?
Hi,
I don’t conpletely understand this circuit.
When I press the pushbutton the arduino is powered on, but it is powered off ass soon as I release it as there is a HIGH signal comint through the 100k resistor.
Could you explain?
After arduino turn on, the code run and it will active the latch circuit instead. So, it will not turn off when you release the button.
If you try doing this but found that arduino still turn off (assume that everything prepare correctly). One reason is that arduino still on boot stage and then still not active the latch circuit when you release the button. Just try pushing the button a little bit longer.
Hi,
I found this circuit useful for my battery powered system. My system is composed of a 3.7V 1200mAh Li-ion battery, Your Auto Power Off Circuit, A dc – dc boost converter circuit and a standalone microcontroller circuit with atmega328p. Circuit block goes from 1, 2, 3, 4.
Li – ion battery
Auto power off circuit
dc – dc boost converter (set to 7V)
atmega328p circuit (Has an input voltage regulator, 78M05)
My problem is that if I go 1, 2, 3, 4 in cascade, the atmega328p circuit is not getting proper 5V. But if go 1, 3, 2, 4 in cascade (boost converter adjusted to 5V and 78M05 is removed from atmega circuit), the atmega328p circuit is working as expected but the battery is getting discharged even when not in use. This discharge problem is not shown in 1, 2, 3, 4.
So what I think is that if some components are changed in the Auto power off circuit I may get 1, 2, 3, 4 to work as expected. Can you help me with this?
thank you
If i put trigger PIN always on .. do the esp say on?
Or.. even if i put 3.3v on trigger for long time .. i still need to set the GPIO to high ?
Hi , think something wrong with the circuit. When i power on, power never reaches beyon 10k resistor before diode. But when i trigger npn trnsistor with mcu, circuit works.
I found an other nice use for this latching circuit for a car that is parked, unsupervised for long periods. The car’s own security system/immobiliser brings the 12v battery down in a matter of weeks so is no use for long term protection. I remove the starter battery and instead use this:
I have a GSM/GPS tracker unit hidden in the car and a lithium 12v, 2500mAh battery isolated by this latching circuit, enough for about 36 hours tracking at least. I use this circuit with a vibration switch in parallel with a tilt switch to detect any tampering with the car. The latching circuit on a PCB powers up my tracker via the output pin of the PCB latch unit in the event of tampering and I loop this power through to the latch (pin2) input through a 1k resistor. The unit could be triggered by a false alarm in which case the tracker never moves but the lithium battery would exhaust itself.
To fix this, the tracker has a NpN circuit internally which may be commanded by SMS or an APP to drive a relay (switching the low side of the relay coil), usually to cut-off fuel supply or ignition and stop the engine. As I have no starter/ignition battery in the car, I use this GPIO to pull down the latch input through a 100R resistor. This pull down is only momentary as once the 2N3904 transistor base voltage drops, the P-Channel MOSFET switches off and everything goes back to sleep.
The reason I am posting here is that all the examples of Rui here and on the supporting video show programatically releasing the latch using an event or timer in your code. This is fine if an event like this is available but it is very simple using MQTT or ESPNOW for example to command your device remotely to release the latch.
I also mention my looping the output power to the latch input with either a transistor, relay or other switch to drop the latch as I had an issue using uPython to latch this circuit. I am not entirely sure but I believe the time taken to start uPython on the microcontroller and run through initialisation may have missed a very short transient trigger. I found it was not robust but didn’t persist long enough to know for sure.
I hope these ideas help some of you, best of luck.
Garry
Hi, I had some problems getting this working.
First of all the sketches are correct but the wiring in the photo with the breadboard is still wrong.
Adding a multimeter to follow up the current finally helped me to get it running.
I can confirm it works with a simple arduino uno and an esp8266.
And yes waiting until the count of 2 or even 5 (so pressing the button somewhat longer) does the trick.
But still in some cases he circuit does not work and sometimes several attemps are necessary.
The direction of the arrows in the 2N3904’s collector and emitter terminals is wrong. Aside some specific scenarios, the current flows into the collector, not out of the collector, and current doesn’t flow from 0V to VCC, not with a NPN transistor.
I have replaced the 2N3904 with IRFZ44N, this works fine for my ESP-01s but does not work for ESP-12e. I also an MCP1700T LDO to get the voltage from the battery to 3.3V. Any Ideas why this does not work for ESP12e?
Hi all,
Good project. I want to use this latch circuit to follow my letters box .
I want to put a magnetic reed switch (instead of the switch on the schematic) on my door’s letter box and be informed when the postman open and close this door’s letter box.
My problem is that i would like to invert the signal of that switch. In sleep mode, reed switch is “on” and when the postman insert a letter, he opens the door, signal is “off”.
So i want my ESP32 to be wake up when signal is off
Is it possible to invert that on the latching circuit ?
thx
You don’t need to invert if you use a clever trick!
I am guessing that you use an ESP32 to message you somehow via WiFi/ESP-NOW/MQTT?
If your microcontroller can boot and set the “Latch” ouptput quickly enough then you can use a physically large magnet that is parked behind the reed tube. As the door opens, the magnet will pass by the reed and close the switch (hopefully for long enough for the ESP32 drive the latch output high and latch Rui’s power circuit “on”). Your code on the ESP32 could then notify you that the postman has called, this is pretty easy using Arduino or MicroPython code on the ESP and a protocol like MQTT. Telegram and Pushbullet are two other apps that will run on your phone that you can message directly from your ESP32
It is easy (assuming there is WiFi around your ESP32) to send a command from your mobile phone for exampele with a MQTT message to reset the ESP32 and drive the latch output low again to go in to full “no load” sleep again. It is also possible to watch for your reed switch going low-high-low suggesting that the postman closed the door then simply wait for a set time to be sure and then shut down as Rui does in his example.
I have used this circuit in many projects. The last one uses a mercury tilt switch and a mechanical vibration sensor to trigger GPS/3G tracking unit on a car in long term storage. The pulse from the vibration sensor is so short, literally milliseconds but is long enough for the output of the latch pcb to feed back in to the latch input and turn on the GPS tracker. There is no battery that could power any circuit even in very deep sleep for months at a time but this zero current solution is ideal. If there is a false alarm and the GPS doesn’t move then I can sleep the power latch circuit by sending an SMS to the tracker. It breaks the input signal to the latch and sleeps everything again.
This is a very versatile circuit,
Tey do make Both NC and NO reed switches.
Hello, this is a really great tutorial. I want to build it by myself, but have problems to find the mosfet NDP6020P or an equivalent p-channel logic level mosfet at electronic-parts-shops in germany. I’ve searched for 2 hours and can’t find a NDP6020P or a replacement. Could you post some popular replacement mosfets that do the same job. Thank you very much.
Useful description, thank you very much! My question is how should I modify the circuit so that after turning on the circuit, I can use the switch as a digital input on one of the GPIO inputs?
is it possible to order the board with the components from you?
Hi.
Unfortunately, we don’t sell the components.
Regards,
Sara
Great tutorial! Would you please provide a sketch that works with a 3.7 V LiPo battery and also name an alternative NDP6020P mosfet as this one is not available anywhere. Would be great to get this to work with a WEMOS D1 mini (pro).
Built the circuit using a IRF9Z34NPBF mosfet. Lokal vendor suggested a BC 546 transistor and SR 106 diodes which worked for me.
Will any thing bad happen if you plug in usb power to the wemos mini (or Esp32) while the battery is attached?
A functional circuit for a very useful purpose.
However, upon consulting my personal inventory of random parts, the market of random part kits, and some other light research, I have to question the way the problem was approached.
Specifically the use of a P channel mosfet to bring the board’s VCC up seems to be straightforward logically but not an idea solution. Designing around a N channel mosfet to disconnect the board from ground seems like it may be more economical.
Thank you for providing a useful starting building block for me regardless.
Hi!
Thanks for your great explanation!
I have tried to make this circuit in an electronics simulator, and when running it, when the switch is off, I can see there is a current of 214nA and 746.9mV going to the VIN of the MCU. So according to the simulation it looks like there’s some Amperage drain in the Mosfet. Is this normal? If so, how could we avoid that?
Thanks!
The circuit is correct I used 3,3V as an input (from 3,7V battery with 3,3V step down regulator) and I get 3,3V on OUTput but when I use it for esp12 it didn’t work. When I used the battery and regulator (without this circuit) then ESP12 booted. Do you have any ideas why?
Low voltage MOSFET (Vgate-dren need to be about 6 or 8 or 12 V) solved the problem.
Not sure what I am doing wrong but the mosfet is always on, won’t turn off at all. Powering the circuit with a 4.2 LiIon battery.
Ignore the above, fake mosfets from AliExpress, branded P channel where they are actually N.