{"id":73704,"date":"2018-09-23T10:58:44","date_gmt":"2018-09-23T10:58:44","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=73704"},"modified":"2019-09-11T10:08:42","modified_gmt":"2019-09-11T10:08:42","slug":"latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino\/","title":{"rendered":"Latching Power Switch Circuit (Auto Power Off Circuit) for ESP32, ESP8266, Arduino"},"content":{"rendered":"\n<p>In this article we&#8217;ll show you how to build a <strong>Latching Power Switch Circuit<\/strong>, also know as an <strong>Auto Power Off Circuit<\/strong>. You can use this circuit to auto-power off the ESP32, ESP8266, Arduino, or any other microcontroller.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/auto-power-off-circuit-thumbnail.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-73767\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/auto-power-off-circuit-thumbnail.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/auto-power-off-circuit-thumbnail.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/auto-power-off-circuit-thumbnail.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/auto-power-off-circuit-thumbnail.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Overview<\/h2>\n\n\n\n<p>Before proceeding with this tutorial, here&#8217;s a high-level overview of what we&#8217;re going to do:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"126\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/lach-power-circuit-overview.png?resize=750%2C126&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-73772\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/lach-power-circuit-overview.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/lach-power-circuit-overview.png?resize=300%2C50&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>When you press the circuit&#8217;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.<\/li><li>You set the LATCH pin (set in the code) to HIGH to keep the circuit powered on.<\/li><li>The microcontroller executes its tasks. In our example, it does nothing &#8211; it simply waits 10 seconds. You can modify the code to execute a useful task.<\/li><li>Set the LATCH pin to LOW, so the microcontroller auto powers off.<\/li><li>When the LATCH pin is set to LOW, the power is cut off.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Auto Power Off vs Deep Sleep<\/h2>\n\n\n\n<p>The auto power off circuit cuts off the power completely. So, there is no power consumption when the microcontroller is not executing any task.<\/p>\n\n\n\n<p>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: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-deep-sleep-with-arduino-ide\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266 Deep Sleep with Arduino IDE<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Microcontroller or Development Board, for example:\n<ul><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 Dev Board<\/a> (<a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">read ESP32 boards review<\/a>)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266 NodeMCU<\/a>&nbsp;(<a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">read ESP8266 boards review<\/a>)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino UNO<\/a> (<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">read best Arduino Starter Kits<\/a>)<\/li><\/ul>\n<\/li><li><a href=\"https:\/\/ebay.to\/2OTdlZ5\" target=\"_blank\" rel=\"noreferrer noopener\">NDP6020P Transistor P-Channel MOSFET<\/a><\/li><li><a href=\"https:\/\/ebay.to\/2IhMdk4\" target=\"_blank\" rel=\"noreferrer noopener\">2N3904 Transistor&nbsp;BJT NPN<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Resistors: 220K Ohm, 2x 100K Ohm, 10K Ohm, and 220 Ohm<\/a><\/li><li><a href=\"https:\/\/ebay.to\/2xTBvvd\" target=\"_blank\" rel=\"noreferrer noopener\">2x Diodes (for example: 1N5819)<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\" rel=\"noreferrer noopener\">Switch\/Pushbutton<\/a><\/li><li>5V Power Supply<\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Auto Power Off Circuit Diagram<\/h2>\n\n\n\n<p>The following circuit diagram shows the Latching Power Switch Circuit (Auto Power Off Circuit) diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1049\" height=\"690\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/power-latch-circuit-schematics-esp32-esp8266-arduino-new.png?resize=1049%2C690&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp32 esp8266 arduino\" class=\"wp-image-74074\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/power-latch-circuit-schematics-esp32-esp8266-arduino-new.png?w=1049&amp;quality=100&amp;strip=all&amp;ssl=1 1049w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/power-latch-circuit-schematics-esp32-esp8266-arduino-new.png?resize=300%2C197&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/power-latch-circuit-schematics-esp32-esp8266-arduino-new.png?resize=768%2C505&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/power-latch-circuit-schematics-esp32-esp8266-arduino-new.png?resize=1024%2C674&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1049px) 100vw, 1049px\" \/><\/figure><\/div>\n\n\n\n<p>The terminals at the right numbered with 1, 2, and 3 should then be connected to your microcontroller board.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Pin 1<\/strong>&nbsp;connects to 5V.<\/li><li><strong>Pin 2<\/strong> can be connected to any digital pin of the microcontroller. In our example, we&#8217;ll connect that pin to GPIO 5 \/ Digital 5.<\/li><li><strong>Pin 3<\/strong>&nbsp;connects to GND.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How the Auto Power Off Circuit Works<\/h3>\n\n\n\n<p><strong>1)<\/strong> 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.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1056\" height=\"593\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-1.png?resize=1056%2C593&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp32 esp8266 arduino\" class=\"wp-image-74128\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-1.png?w=1056&amp;quality=100&amp;strip=all&amp;ssl=1 1056w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-1.png?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-1.png?resize=768%2C431&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-1.png?resize=1024%2C575&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1056px) 100vw, 1056px\" \/><\/figure><\/div>\n\n\n\n<p><strong>2)<\/strong> 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&#8217;s gate is pulled to GND.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"412\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-how-it-works-2-1.png?resize=750%2C412&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp32 esp8266 arduino\" class=\"wp-image-74071\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-how-it-works-2-1.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-how-it-works-2-1.png?resize=300%2C165&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3)<\/strong> To keep the MOSFET&#8217;s gate pulled to GND after releasing the pushbutton, we send a HIGH signal through a microcontroller&#8217;s GPIO. When we send a HIGH signal, there is power reaching the base of the transistor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1112\" height=\"596\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-3.png?resize=1112%2C596&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp32 esp8266 arduino\" class=\"wp-image-74129\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-3.png?w=1112&amp;quality=100&amp;strip=all&amp;ssl=1 1112w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-3.png?resize=300%2C161&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-3.png?resize=768%2C412&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-3.png?resize=1024%2C549&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-3.png?resize=280%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 280w\" sizes=\"(max-width: 1112px) 100vw, 1112px\" \/><\/figure><\/div>\n\n\n\n<p><strong>4)<\/strong>&nbsp;Therefore, we ensure that the MOSFET&#8217;s gate is pulled to GND, and current flows to the VIN terminal to power our microcontroller.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1185\" height=\"583\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-4.png?resize=1185%2C583&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp32 esp8266 arduino\" class=\"wp-image-74130\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-4.png?w=1185&amp;quality=100&amp;strip=all&amp;ssl=1 1185w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-4.png?resize=300%2C148&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-4.png?resize=768%2C378&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/latch-power-circuit-explained-4.png?resize=1024%2C504&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1185px) 100vw, 1185px\" \/><\/figure><\/div>\n\n\n\n<p><strong>5)<\/strong> When we want to power off the circuit, we simply need to set the GPIO to LOW. When that happens, there isn&#8217;t power reaching the base of the transistor, so the MOSFET doesn&#8217;t let the current flow to the VIN pin, and there isn&#8217;t power consumption.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Auto Power Off Circuit &#8211; ESP32<\/h3>\n\n\n\n<p>Here&#8217;s how to wire the circuit if you&#8217;re using an ESP32.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1092\" height=\"704\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP32-power-Latch-Circuit.png?resize=1092%2C704&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp32\" class=\"wp-image-73708\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP32-power-Latch-Circuit.png?w=1092&amp;quality=100&amp;strip=all&amp;ssl=1 1092w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP32-power-Latch-Circuit.png?resize=300%2C193&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP32-power-Latch-Circuit.png?resize=768%2C495&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP32-power-Latch-Circuit.png?resize=1024%2C660&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1092px) 100vw, 1092px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Auto Power Off Circuit &#8211; ESP8266<\/h3>\n\n\n\n<p>Here&#8217;s the diagram for the ESP8266.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1086\" height=\"574\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP8266-power-Latch-Circuit.png?resize=1086%2C574&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics esp8266\" class=\"wp-image-73709\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP8266-power-Latch-Circuit.png?w=1086&amp;quality=100&amp;strip=all&amp;ssl=1 1086w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP8266-power-Latch-Circuit.png?resize=300%2C159&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP8266-power-Latch-Circuit.png?resize=768%2C406&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/ESP8266-power-Latch-Circuit.png?resize=1024%2C541&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1086px) 100vw, 1086px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Auto Power Off Circuit &#8211; Arduino<\/h3>\n\n\n\n<p>Finally, here&#8217;s how to connect the latch circuit to the Arduino.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"723\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/Arduino-power-Latch-Circuit.png?resize=1200%2C723&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power latch circuit schematics arduino\" class=\"wp-image-73710\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/Arduino-power-Latch-Circuit.png?w=1428&amp;quality=100&amp;strip=all&amp;ssl=1 1428w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/Arduino-power-Latch-Circuit.png?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/Arduino-power-Latch-Circuit.png?resize=768%2C463&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/Arduino-power-Latch-Circuit.png?resize=1024%2C617&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading Code<\/h2>\n\n\n\n<p>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\u2019t already. If you&#8217;re using an Arduino you don&#8217;t need to install anything else.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Installing the ESP32 Board in Arduino IDE:\n<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Windows instructions<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions\/\">Mac and Linux instructions<\/a><\/li><\/ul>\n<\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Installing ESP8266 Board in Arduino IDE<\/a><\/li><\/ul>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Define power latch pin for ESP32 (GPIO 5) \/ ESP8266 (GPIO 5) \/ Arduino (Digital 5)\nconst int powerLatch = 5;\n\nvoid setup() {\n  \/\/ Define pin as an OUTPUT\n  pinMode(powerLatch, OUTPUT); \n  \n  \/\/ Keeps the circuit on\n  digitalWrite(powerLatch, HIGH);\n  \/\/ Waits for 10 seconds\n  delay(10000);\n  \/\/ Turns the power latch circuit off\n  digitalWrite(powerLatch, LOW);\n}\n\nvoid loop() {\n  \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Power_Latch_Circuit.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>Let&#8217;s take a closer look on how the code works and how you can use it in your projects.<\/p>\n\n\n\n<p>We start by defining the power latch pin. We&#8217;re using <span class=\"rnthl rntcblue\">GPIO 5<\/span>, but you can use any other pin. This GPIO is connected to the latch power circuit pin 2 terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int powerLatch = 5;<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we define the power latch pin as an output.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(powerLatch, OUTPUT);<\/code><\/pre>\n\n\n\n<p>Next, we set the power latch pin to <span class=\"rnthl rntliteral\">HIGH<\/span>. When we set it to high, we ensure that there is power coming to feed the microcontroller.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite(powerLatch, HIGH);<\/code><\/pre>\n\n\n\n<p>Next, we wait 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(10000);<\/code><\/pre>\n\n\n\n<p>After that, we set the power latch pin to <span class=\"rnthl rntliteral\">LOW<\/span>. When it is set to low, the power is cut off, and the microcontroller turns off.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite(powerLatch, LOW);<\/code><\/pre>\n\n\n\n<p>You should add the task you want to perform after setting the power latch pin to <span class=\"rnthl rntliteral\">HIGH<\/span> and before setting it to <span class=\"rnthl rntliteral\">LOW<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>The<strong> Latching Power Switch Circuit<\/strong>, also know as an <strong>Auto Power Off Circuit&nbsp;<\/strong>allows you to turn off your microcontroller via software, whether you&#8217;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.<\/p>\n\n\n\n<p>In our example, we&#8217;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 &#8211; like a <a href=\"https:\/\/randomnerdtutorials.com\/monitor-your-door-using-magnetic-reed-switch-and-arduino\/\" target=\"_blank\" rel=\"noreferrer noopener\">reed switch<\/a> for example. We hope you&#8217;ve found this tutorial useful and you&#8217;re able to use it in your electronics projects to save power.<\/p>\n\n\n\n<p>For more electronics projects, you can take a look at our repository of more than <a href=\"https:\/\/randomnerdtutorials.com\/projects\/\" target=\"_blank\" rel=\"noreferrer noopener\">+200 Electronics projects and Tutorials<\/a> or <a href=\"https:\/\/randomnerdtutorials.com\/courses\/\" target=\"_blank\" rel=\"noreferrer noopener\">enroll in our premium courses<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we&#8217;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 &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Latching Power Switch Circuit (Auto Power Off Circuit) for ESP32, ESP8266, Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino\/#more-73704\" aria-label=\"Read more about Latching Power Switch Circuit (Auto Power Off Circuit) for ESP32, ESP8266, Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":73767,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[267,3,281,299,265,269,264,11],"tags":[],"class_list":["post-73704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-project","category-circuits","category-esp32-project","category-0-esp32","category-esp8266-project","category-guide-project","category-project","category-c-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/auto-power-off-circuit-thumbnail.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/73704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=73704"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/73704\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/73767"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=73704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=73704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=73704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}