Note: The soft switch circuit introduced in this article does not refer to the concept of "soft switching" in switch-mode power supplies. Instead, it pertains to a system's power supply switch that, in conjunction with an MCU or other control chips, controls the turning on or off of the system's power.
In some electronic products, we often need features like a long press to power on, followed by another long press to power off. Alternatively, for devices with a touch screen, a long press might bring up a shutdown menu to initiate the power-off sequence. This entire process is managed by a soft switch circuit.
For example, a wearable smart band might have a single button that powers the device on with a long press, and the device remains powered even after the button is released. After startup, a long press of the same button could either trigger a shutdown option on the UI or be detected by the MCU to directly cut off the system's power. The operation of turning the device off after it has been powered on is controlled by this soft switch circuit.
2. Hardware DesignBased on the introduction above, a soft switch cannot be implemented purely through software. This is because when the system power is off, the MCU itself is not powered. Therefore, the power-on sequence must be initiated by a physical button press, which powers up the MCU. Once the MCU is running, it can take over control of the power supply, ensuring the system remains powered even after the power button is released.
To summarize, the design of a soft switch circuit requires the following conditions:
- When the entire system is off, a button must be available to control power-on.
- After the system is on, a GPIO pin must be able to control the state (on/off) of the system power.
- After the system is on, it must be able to detect a long press of the button to initiate a shutdown action (or, if a UI is present, the shutdown can be controlled via the UI).
Based on these conditions, I will now share a soft switch circuit design controlled by an APM32F411 MCU.
2.1 Circuit SchematicThere are many ways to design a soft switch circuit. My design philosophy is to control the EN (Enable) pin of the power management IC. By controlling the voltage to this enable pin, we control the output of the power IC, which in turn supplies power to the entire system.
The following is the overall schematic for the soft switch circuit:
Overall Introduction:
- The circuit has two power inputs: a 3.7V battery and USB power.
- The soft switch control logic works by managing the power to the enable pin of the SPX3819 power chip. When there is voltage on the enable pin, the SPX3819 outputs 3.3V, supplying power to the entire system.
- When the SW1 button is not pressed, the source and gate of the PMOS transistor Q2 are at the same potential, so Vgs = 0V. The transistor does not conduct, no voltage is supplied to the SPX3819's enable pin, and thus there is no 3.3V output for the system.
The core of the soft switch control is the circuit in the bottom-left corner. I will now analyze how this circuit achieves the soft switch functionality in different states.
1) System Off, SW1 Button Not PressedWhen the system is in a powered-down state and the SW1 button is not pressed, the control signal SYS_POWER_LOCK
for the gate of the NMOS Q3 is at a low level, so Q3 is off. Consequently, the gate (G) and source (S) of the PMOS Q2 are at the same potential, meaning Vgs for Q2 is 0V. This keeps Q2 turned off, so the VIN voltage cannot reach the enable pin of the SPX3819 power chip. As a result, there is no 3.3V output, and the system remains unpowered.
The equivalent circuit for this state is shown below:
2) System Off, SW1 Button PressedWhen the system is off but the SW1 button is pressed, the gate voltage of the PMOS Q2 is determined by the forward voltage drop of the diode D3 (1N5819). Assuming this drop is 0.7V, the gate potential of Q2 becomes Vg = 0.7V. Meanwhile, the source voltage of Q2 is VIN (the input voltage, either 3.7V or 5V). This results in a Vgs of -4.3V (or -3.0V for a 3.7V input), which turns Q2 on. Voltage is now supplied to the enable pin of the SPX3819, which in turn provides 3.3V to power the entire system.Therefore, pressing the SW1 button turns the system on.
The equivalent circuit for this state is as follows:
3) System On, SYS_POWER_LOCK
High, SW1 Button ReleasedOnce the system is powered on by pressing SW1, the MCU software begins to execute. It immediately sets the SYS_POWER_LOCK
control signal to a high level, which turns on the NMOS transistor Q3.Now, when the user releases the SW1 button, Q3 remains conducting, pulling the gate (G) of Q2 to a voltage near GND (depending on the on-state resistance of Q3). As a result, Q2 remains turned on, and the system power is maintained.
The equivalent circuit for this state is shown below:
4) System On, SW1 Button PressedAfter the system is already on and supplied with 3.3V, pressing or releasing the SW1 button no longer affects the power supply's state. This is because the MCU is now holding the power on by keeping the NMOS Q3 turned on.At this point, the SW1 button can be used as a standard input button for the MCU, which can detect its state (e.g., pressed, long-pressed). It effectively functions as a normal user button.
The equivalent circuit for this state is as follows:
A point of caution here is the forward voltage drop of the diode D2 (1N5819). You must check the MCU's datasheet for its low-level voltage threshold (VIL). If the voltage drop is too high, the MCU might not register the button press as a low-level input.
5) System On, SYS_POWER_LOCK
Control Signal Goes LowAfter the system is on, if the MCU detects a long press of the SW1 button (or if a shutdown command is issued from a touch interface), it will set the SYS_POWER_LOCK
signal to a low level. This turns off the NMOS Q3. When the user subsequently releases the SW1 button, the system will be completely powered down, returning to the initial state: system off, SW1 button released.
Compared to the hardware, the software for the soft switch circuit is actually simpler. It primarily involves controlling the SYS_POWER_LOCK
signal (high or low) via an MCU GPIO pin. If the button is also used for shutdown, code to detect the SW1 button press is also required.
Below is a simple code example for controlling the soft switch using an APM32F411.
- Code to control the
SYS_POWER_LOCK
signal, thereby controlling the system power.
void power_control_gpio_config(void)
{
GPIO_Config_T gpioConfigStruct;
/* Enable GPIO clock */
RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
gpioConfigStruct.pin = GPIO_PIN_15;
gpioConfigStruct.mode = GPIO_MODE_OUT;
gpioConfigStruct.speed = GPIO_SPEED_100MHz;
gpioConfigStruct.otype = GPIO_OTYPE_PP;
gpioConfigStruct.pupd = GPIO_PUPD_NOPULL;
GPIO_Config(GPIOA, &gpioConfigStruct);
GPIO_ResetBit(GPIOA, GPIO_PIN_15); // Default to off
}
/* System Power On */
void power_enable(void)
{
GPIO_SetBit(GPIOA, GPIO_PIN_15);
}
/* System Power Off */
void power_disable(void)
{
GPIO_ResetBit(GPIOA, GPIO_PIN_15);
}
- Code to detect a press or long press of the SW1 button to decide whether to execute the shutdown action.
static struct Button key1;
// ... other buttons if any
void key_gpio_config(void)
{
GPIO_Config_T gpioConfigStruct;
/* Enable GPIO clock */
RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOB);
/* KEY1 : PB0 */
gpioConfigStruct.mode = GPIO_MODE_IN;
gpioConfigStruct.pin = GPIO_PIN_0; // Assuming SW1 is connected to PB0
gpioConfigStruct.pupd = GPIO_PUPD_UP;
GPIO_Config(GPIOB, &gpioConfigStruct);
}
uint8_t read_key_gpio(uint8_t key_id)
{
// This function reads the state of the specified key.
// Assuming key_id 0 corresponds to SW1 on PB0.
if (key_id == 0) {
return GPIO_ReadInputBit(GPIOB, GPIO_PIN_0);
}
return 1; // Default to not pressed
}
void key1_callback(void *button)
{
uint32_t btn_event_val;
btn_event_val = get_button_event((struct Button *)button);
switch(btn_event_val)
{
case PRESS_DOWN:
printf("************* key1 press down! *************\n");
break;
case PRESS_UP:
printf("************* key1 press up! *************\n");
break;
// ... other cases like SINGLE_CLICK, DOUBLE_CLICK ...
case LONG_PRESS_START:
printf("************* key1 long press start! *************\n");
LCD_Fill(0,0,LCD_W,LCD_H,BLACK);
LCD_ShowString(60,LCD_H/2,(uint8_t*)"Shutdown",WHITE,BLACK,32,1); // Display shutdown message on LCD
delay_ms(1500);
power_disable(); // Detected long press, power off the system
// while (1); // Loop here if needed
break;
case LONG_PRESS_HOLD:
printf("************* key1 long press hold! *************\n");
break;
}
}
void multi_button_register(void)
{
// Assumes a button library is used
button_init(&key1, read_key_gpio, 0, KEY1_ID);
button_attach(&key1, (PressEvent)LONG_PRESS_START, (BtnCallback)key1_callback);
button_start(&key1);
}
4. Effect DemonstrationI had intended to upload a video demonstrating the result, but I will supplement with images for now.
The soft switch circuit described above has been built into a physical prototype and has been tested. No issues were found during testing, and it functions correctly.
However, the analysis above may not be entirely correct, or there might be flaws in the circuit that I have not yet noticed. If there are any errors, please feel free to discuss them.
Below are supplementary images of the demonstration:
First, in the system's off state, a long press of the button powers on the device. The MCU powers up and runs the program, displaying a boot screen and outputting the control signal to latch the power, keeping the system supplied. When the button is released, the system's power remains stable.
After power-on, another long press of the button is detected by the MCU, which then executes the shutdown procedure. The LCD displays a shutdown message. When the user sees this and releases the button, the entire system powers down.
Comments