张小哥

Automatic power-off circuit

 
Overview

In this article, we will show you how to build a latching power switching circuit , also known as an automatic power-off circuit . You can use this circuit to automatically shut down an ESP32, ESP8266, Arduino or any other microcontroller.

This circuit allows you to completely cut off power when the microcontroller is not performing any tasks. In other words, once the microcontroller has finished executing its task, it shuts itself down via software. This is a great way to make batteries last longer in your electronics projects.

Automatic shutdown circuit diagram

The following circuit diagram shows the latching power switch circuit (auto power off circuit) diagram.

Power latch circuit schematic diagram esp32 esp8266 arduino

The terminals numbered 1, 2 and 3 on the right side should be connected to your microcontroller board.

  • Pin 1  is connected to 5V.
  • Pin 2 can be connected to any digital pin of the microcontroller. In our example we connect this pin to GPIO 5 / Digital 5.
  • Pin 3  is connected to GND.

     

  • How the automatic shutdown circuit works

    1) When you press the switch or close the circuit, power reaches the base of the 2N3904 transistor. Therefore, the 2N3904 is pulled low, pulling the gate (G) of the MOSFET to GND.

    2)  A P-channel MOSFET turns on when its gate is negative relative to its 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 occurs whenever the gate of the MOSFET is pulled to GND. 3) To keep the gate of the MOSFET pulled to GND after releasing the button, we send a HIGH signal through the GPIO of the microcontroller. When we send a HIGH signal, power reaches the base of the transistor. 4)  Therefore, we ensure that the gate of the MOSFET is pulled to GND and the current flows to the VIN terminal to power our microcontroller. 5) When we want to turn off the circuit, we just set the GPIO to LOW. When this happens, no power reaches the base of the transistor, so the MOSFET does not allow current to flow to the VIN pin and therefore no power is dissipated. The minor changes I made to the circuit,8CNk4GCI6PQ9lGTtautJrKQeNTcKJwu8VeYnxHTT.png
  • PCB
  • 6XFxawHwywBsUbyDxnI3B4NgNMaabQlDBdWGCXhp.png

     

  • Test code based on MounRiver Studio
  • PA1 controls the GPIO pin
  • PA0 controls the LED to indicate the operating status of the microcontroller.
  • /********************************** (C) COPYRIGHT *********** ********************
    * File Name: main.c
    * Author: WCH
    * Version: V1.0.0
    * Date: 2020/04/30
    * Description: Main program body.
    ************************************************ *******************************/

    /*
     *@Note
     serial port printing debugging routine:
     USART1_Tx(PA9).
     This routine demonstrates using USART1(PA9) for printing debugging port output.

    */

    #include "debug.h"


    /* Global typedef */
    #define LED_ON GPIO_WriteBit(GPIOA, GPIO_Pin_0,0);
    #define LED_OFF GPIO_WriteBit(GPIOA, GPIO_Pin_0,1);
    #define POWER_ON GPIO_WriteBit(GPIOA, GPIO_Pin_1,1);
    #define POWER_OFF GPIO_WriteBit(GPIOA, GPIO_Pin_ 1 ,0);
    /* Global define */

    /* Global Variable */

    /****************************************************** ********************
     * @fn GPIO_Toggle_INIT
     *
     * @brief Initializes GPIOA.0
     *
     * @return none
     */
    void GPIO_Toggle_INIT(void)
    {
        GPIO_InitTypeDef GPIO_InitStructure = {0 };

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
        GPIO_Init(GP IOA, &GPIO_InitStructure);
    }

    /****************************************************** ******************************
    * Function Name: main
    * Description: Main program.
    * Input: None
    * Return: None
    * *************************************************** ******************************/
    int main(void)
    {
        u8 i = 0;
        u32 cnt=0,offPower;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2 );


        Delay_Init();
        GPIO_Toggle_INIT();
        POWER_ON;
        USART_Printf_Init(115200);
        printf("SystemClk:%d ",SystemCoreClock);

        printf("This is printf example ");


    // GPIO_WriteBit(GPIOA, GPIO_Pin_0,0);


            while(1)
            {
                POWER_ON;
                Delay_Ms(100);
                GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET));
               if(cnt==100)
                {
                   GPIO_WriteBit(GPIOA, GPIO_Pin_0,1);
                   POWER_OFF;
                   Delay_Ms(20);
                   cnt=0;
                }

               cnt++;

            }
    }

     

参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-19 05:08:34

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号