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.
The following circuit diagram shows the latching power switch circuit (auto power off circuit) diagram.
The terminals numbered 1, 2 and 3 on the right side should be connected to your microcontroller board.
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,
/********************************** (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++;
}
}
All reference designs on this site are sourced from major semiconductor manufacturers or collected online for learning and research. The copyright belongs to the semiconductor manufacturer or the original author. If you believe that the reference design of this site infringes upon your relevant rights and interests, please send us a rights notice. As a neutral platform service provider, we will take measures to delete the relevant content in accordance with relevant laws after receiving the relevant notice from the rights holder. Please send relevant notifications to email: bbs_service@eeworld.com.cn.
It is your responsibility to test the circuit yourself and determine its suitability for you. EEWorld will not be liable for direct, indirect, special, incidental, consequential or punitive damages arising from any cause or anything connected to any reference design used.
Supported by EEWorld Datasheet