Design a site like this with
Get started

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:

  • Microcontroller or Development Board, for example:
    • ESP32 Dev Board (read ESP32 boards review)
    • ESP8266 NodeMCU (read ESP8266 boards review)
    • Arduino UNO (read best Arduino Starter Kits)
  • 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

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.


// 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() {

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


Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

One thought on “Latching Power Switch Circuit (Auto Power Off Circuit) for ESP32, ESP8266, Arduino

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: