STM32 Sleep Mode & Wakeup Pin (Example Code)

In this tutorial, we’ll discuss The STM32 Sleep Mode, how to enter sleep mode, how to exit from sleep, Wakeup Pin in STM32 microcontrollers, and other wakeup sources with some code examples and a full test project. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 Sleep Mode
  2. STM32 Enter Sleep Mode
  3. STM32 Wakeup (Exit) Sleep Mode
  4. STM32 Sleep On Exit
  5. STM32 Sleep Mode Example Code Project
  6. Wrap Up

STM32 Sleep Mode

In Sleep mode, the STM32 microcontroller’s CPU clock is turned OFF but all the other clock sources are active. All peripherals will continue to operate normally and can wake up the CPU when an interrupt/event occurs.

STM32 Sleep Mode

For the STM32L432KC target microcontroller, in Sleep Mode, the current consumption is:

  • At voltage scaling Range 1 & SysCLK=80MHz, the current draw is 2.23mA
  • At voltage scaling Range 2 & SysCLK=100kHz, the current draw is 0.11mA

As you might have noticed, reducing the SysClk frequency will significantly reduce the current consumption in sleep mode. However, it might not meet the timing requirements of certain applications, so you need to reach a compromise.

To put things into perspective, this same target microcontroller (STM32L432KC) draws 8.5mA of current while operating in the Run Mode at full speed (80MHz). Therefore, going into Sleep Mode at the same clock of 80MHz feeding all buses/peripherals except for the CPU, will already set you at a quarter of the current consumption of the normal Run mode.

❕ Note

While debugging a target MCU in sleep mode, make sure you’re working in Range 1 by checking the voltage regulator scale configuration in CubeMX.


STM32 Enter Sleep Mode

By entering sleep mode, the STM32 microcontroller is placed in a low-power state while retaining the contents of SRAM and registers. In this mode, the CPU stops executing instructions, but peripherals, interrupts, and the system clock can remain active depending on the configuration.

To enter Sleep Mode in STM32 microcontrollers, we should first stop the Systick timer. Because if we leave it ON, it’ll wake up the CPU with its interrupt signals every 1ms. So we need to call the HAL_SuspendTick() function first.

Then we can use the HAL_PWR_EnterSLEEPMode() function to enter the sleep mode. You can configure whether the system wakes up via WFI (Wait For Interrupt) or WFE (Wait For Event) instructions.

Here is a code example for entering the STM32 Sleep Mode:

❕ Note

You need to Stop The SysTick Timer as it will wake up the CPU every 1ms through its interrupt signal. And don’t forget to resume its operation upon waking up to make sure any systick-dependent software components will continue to operate normally.


STM32 Wakeup (Exit) Sleep Mode

Any interrupt signal will cause the CPU to exit from the sleep mode and go back to normal operation. Therefore, we can use something like EXTI or a specific timer to wake up the CPU on purpose and resume the Systick timer operation by calling the HAL_ResumeTick() function.

This is an example code for exiting the sleep mode upon receiving an EXTI interrupt on the GPIO1 line.


STM32 Sleep On Exit

The Sleep On Exit feature in STM32 microcontrollers is a low-power mechanism that allows the device to automatically enter Sleep mode after exiting an interrupt service routine (ISR).

This means the CPU will run only in interrupt mode and anything in the main while(1) loop will not be executed at all.

This is how to enable the STM32 Sleep on Exit:

Call this function below to disable the Sleep On Exit feature:


STM32 Sleep Mode Example Code Project

In this example project, we’ll configure our STM32L432KC microcontroller to run at full speed (80MHz) for 1 second. Then it’ll automatically switch to sleep mode.

Upon receiving an EXTI interrupt on the GPIO1 pin from a push button, the system will switch back to the Run Mode. And it’ll keep toggling between both modes each time the interrupt button is pressed.

The behavior of the system can be easily observed in a debug session by observing a counter variable and a single breakpoint.

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller. For me, it’s (STM32L432KC)

Step #2

Configure a GPIO pin in EXTI input pull-down mode (button). I’ll use A1 pin (EXTI1), you can choose any EXTI-capable pin.

Enable The EXTI interrupt from the NVIC configuration tab.

STM32 Low Power Run Mode EXTI Pin Exit

Step #3

Go to the Clock configuration page and select the internal HSI+PLL as a clock source to give you the maximum SysClk of 80MHz (in my case). This is the base full-speed run mode.

You can however, select the internal MSI which can give you a low-frequency clock source (2MHz down to 100kHz), if needed.

STM32 Low Power Run Mode Example Clock Configuration

Step #4

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 Sleep Mode Example Code

Here is The Application Code For This LAB (main.c)

STM32 Sleep Mode Example Testing

To test this project, you can place one break point in the location indicated below and keep clicking on the run button in the debug menu while monitoring the counter variable’s value in the live expressions window.

You can see that the system starts in run mode, then after 1s, it goes into sleep mode, therefore, the counter variable stops counting and the breakpoint will not be reached again.

By pressing the A0 (EXTI1) push button, your system will get out of sleep mode and back to run mode. Now, any click on the debug continue button will increment the counter variable by 1 and it’ll keep counting each time will click on the “continue” debug button.

One more click on the push button will bring the system back to sleep, the counter will stop counting, and the breakpoint won’t be reached again. Further clicks on the push button will keep toggling the system between run & sleep modes.

STM32 Sleep Mode & Wakeup (Sources - Pin) Example Code Project

❕ Note

Sleep Mode can’t be entered from the interrupt handler context. Therefore, the flag variable EnterSleepFlag was used to handle the sleep mode entry operation within the main function context.

We could’ve used the STM32 Sleep-On-Exit feature to achieve similar behavior, but it would have been much harder to observe since we’re depending on a counter variable’s change in the main while(1) that won’t be executed if we were to use the sleep-on-exit feature.


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored the STM32 Sleep Mode, what it does to reduce the current consumption, and what are the current consumption numbers expected from this mode using the (STM32L432KC) target microcontroller.

We’ve also explored how to enter and exit (wake-up) from sleep mode in STM32 microcontrollers. And how to use the STM32 Sleep-On-Exit feature to run an interrupt-based system with minimal power consumption automatically without worrying about entering and exiting sleep mode manually.

You can build on top of the provided example code project and integrate it into your system. You can also check the rest of the tutorials in this series to learn more about other low-power modes in STM32 microcontrollers.

This Tutorial is Part of The Following Multi-Part Tutorial Series:

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment