0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Put Microcontroller in Sleep Mode in Embedded C

To put a microcontroller in sleep mode in embedded C, you typically set specific bits in power management registers and then execute a sleep instruction or function. This reduces power consumption by stopping the CPU until an interrupt or reset wakes it up.
📐

Syntax

Putting a microcontroller to sleep usually involves these steps:

  • Configure sleep mode by setting bits in a power management register.
  • Enable sleep mode by setting the sleep enable bit.
  • Execute the sleep instruction to enter sleep mode.

For example, on many AVR microcontrollers:

  • SMCR is the Sleep Mode Control Register.
  • SE bit enables sleep.
  • SM[2:0] bits select the sleep mode type.
  • sleep_cpu() is a function that executes the sleep instruction.
embedded_c
/* Example syntax for AVR microcontroller */
#include <avr/sleep.h>

int main(void) {
    // Set sleep mode
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Choose power-down mode

    // Enable sleep
    sleep_enable();

    // Enter sleep mode
    sleep_cpu();

    // CPU sleeps here until interrupt wakes it up

    // Disable sleep after waking
    sleep_disable();

    return 0;
}
💻

Example

This example demonstrates putting an AVR microcontroller into power-down sleep mode and waking up on an external interrupt.

embedded_c
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

// Interrupt service routine for INT0
ISR(INT0_vect) {
    // Interrupt wakes the MCU from sleep
}

int main(void) {
    // Configure INT0 (external interrupt 0) on falling edge
    EICRA |= (1 << ISC01);  // ISC01=1, ISC00=0 for falling edge
    EICRA &= ~(1 << ISC00);
    EIMSK |= (1 << INT0);   // Enable INT0 interrupt

    sei(); // Enable global interrupts

    // Set sleep mode to power-down
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);

    while (1) {
        sleep_enable();
        sleep_cpu();  // MCU sleeps here
        sleep_disable();

        // MCU wakes here after INT0 interrupt
        // Add code to run after waking up
    }

    return 0;
}
Output
No output (microcontroller sleeps and wakes on interrupt)
⚠️

Common Pitfalls

  • Not enabling global interrupts (sei()) before sleeping prevents wake-up.
  • Forgetting to disable sleep after waking can cause unexpected behavior.
  • Choosing the wrong sleep mode may not reduce power as expected.
  • Not configuring the wake-up source (interrupt) properly means the MCU won't wake.
embedded_c
/* Wrong way: No interrupts enabled, MCU never wakes */
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
// MCU stays asleep forever

/* Right way: Enable interrupts and configure wake-up */
sei();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
sleep_disable();
📊

Quick Reference

StepDescriptionExample (AVR)
1Select sleep modeset_sleep_mode(SLEEP_MODE_PWR_DOWN);
2Enable sleepsleep_enable();
3Enable global interruptssei();
4Enter sleepsleep_cpu();
5Disable sleep after wakesleep_disable();

Key Takeaways

Set the correct sleep mode before enabling sleep to save power effectively.
Always enable global interrupts to allow the microcontroller to wake up.
Use sleep_enable() and sleep_disable() to control sleep state properly.
Configure and enable an interrupt source to wake the microcontroller from sleep.
After waking, disable sleep mode to resume normal operation safely.