Latching Power Switch Circuit (Auto Power Off Circuit) for ESP32, ESP8266, Arduino

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.


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:

You can use the preceding links or go directly to 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.

power latch circuit schematics esp32 esp8266 arduino

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.

power latch circuit schematics esp32 esp8266 arduino

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.

power latch circuit schematics esp32 esp8266 arduino

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.

power latch circuit schematics esp32 esp8266 arduino

4) Therefore, we ensure that the MOSFET’s gate is pulled to GND, and current flows to the VIN terminal to power our microcontroller.

power latch circuit schematics esp32 esp8266 arduino

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.

power latch circuit schematics esp32

Auto Power Off Circuit – ESP8266

Here’s the diagram for the ESP8266.

power latch circuit schematics esp8266

Auto Power Off Circuit – Arduino

Finally, here’s how to connect the latch circuit to the Arduino.

power latch circuit schematics 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.

  Rui Santos
  Complete project details at  

// 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
  // Turns the power latch circuit off
  digitalWrite(powerLatch, LOW);

void loop() {

View raw code

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.


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.

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our weekly newsletter!

60 thoughts on “Latching Power Switch Circuit (Auto Power Off Circuit) for ESP32, ESP8266, Arduino”

    • 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.

  1. 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.

  2. 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!

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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…

  10. 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.”

  11. 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.

  12. 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?


    • 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.
      Sara 🙂

    • 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!

    • 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!
      Sara 🙂

  13. 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:

    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!
      Sara 🙂

  14. 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.
      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.

  15. 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.
      Sara 🙂

  16. 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.
      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.

  17. 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.

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.