Random Nerd Tutorials
Shares

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

Shares

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:

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.

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.

cloud-download

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.

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.


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registrations are open, so SIGN UP NOW »

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course