Sleep modes and wake-up sources
About this lesson
A battery node lasts months only if its microcontroller spends almost all its time asleep. Sleeping means switching clocks off: each deeper sleep mode stops more of them and draws less current, but leaves fewer things that can wake the chip. Follow an ATmega328P from 1.7 mA awake down to 4.2 µA in Power-down, see sleep_mode() stop the CPU on one line, and wake it again with two wake-up sources: the watchdog timer every 8 seconds and a button on pin 2. Then pay the catch (millis() stops) and collect the payoff: about 4.6 µA on average. The lesson “Power budgets and battery life” turns such averages into battery life; the Hardware lessons on power supplies and batteries cover the cells themselves.
Awake: every clock ticking
At 4 MHz and 3 V an awake ATmega328P draws about 1.7 mA. Three clocks tick: the CPU clock, the I/O clock for timers and serial, and the watchdog's own oscillator. Every tick costs a little charge, even inside delay().
chip ATmega328P, 4 MHz, 3 V (datasheet Table 29-8, typical) active 1.7 mA clocks CPU on · I/O on · WDT on delay() waits awake: the clocks keep ticking
Read the lesson step by step
Awake: every clock ticking
At 4 MHz and 3 V an awake ATmega328P draws about 1.7 mA. Three clocks tick: the CPU clock, the I/O clock for timers and serial, and the watchdog's own oscillator. Every tick costs a little charge, even inside delay().
chip ATmega328P, 4 MHz, 3 V (datasheet Table 29-8, typical) active 1.7 mA clocks CPU on · I/O on · WDT on delay() waits awake: the clocks keep tickingIdle: stop the CPU clock
A sleep mode switches clocks off. Idle stops only the CPU clock. Timers, serial and the ADC (the voltage reader) keep running, so almost any interrupt wakes it: a signal that pulls the CPU into a short routine. About 0.3 mA.
sleep mode which clocks keep ticking Idle CPU off · I/O on · WDT on 0.3 mA wakes on almost any interrupt: timer, serial, pins, ADCPower-down: only the watchdog
Power-down stops every clock but the watchdog's: 4.2 µA, about 400 times less than awake. With the watchdog off too: 0.1 µA. Power-save keeps only Timer2 on its 32 kHz crystal, watchdog off: 0.9 µA. Fewer clocks, fewer ways to wake.
Active CPU on · I/O on · WDT on 1.7 mA Idle CPU off · I/O on · WDT on 0.3 mA Power-down CPU off · I/O off · WDT on 4.2 µA Power-save CPU off · I/O off · WDT off · Timer2 0.9 µA (32 kHz crystal) Power-down CPU off · I/O off · WDT off 0.1 µA compare like with like: every figure lists what still tickssleep_mode(): stop on this line
setup() chose SLEEP_MODE_PWR_DOWN. sleep_mode() sets the sleep-enable bit and runs the SLEEP instruction. The clocks stop and the CPU halts right on this line. SRAM and registers keep their values; no code runs at all.
setup() set_sleep_mode(SLEEP_MODE_PWR_DOWN); line 4 sleep_mode(); = sleep_enable(); SE bit = 1 sleep_cpu(); SLEEP instruction: clocks stop here sleep_disable(); runs only after a wake-up now 4.2 µA, SRAM and registers keptWake-up source 1: the watchdog
armWakeups() set the watchdog to interrupt mode. It counts its own 128 kHz oscillator: after 1,048,576 ticks, about 8 seconds, it raises an interrupt, and the interrupt wakes the chip.
line 3 armWakeups(); WDTCSR = WDCE | WDE unlock (timed sequence) WDTCSR = WDIE | WDP3 | WDP0 interrupt mode, 8 s WDT 1,048,576 cycles of 128 kHz -> about 8 s time-out WDT_vect interrupt -> clocks restartCarry on after SLEEP
The chip waits for its clock to settle, plus four cycles, then runs the interrupt routine, ISR(WDT_vect). Then it carries on after the SLEEP instruction: sleep_mode() returns, loop() starts again and measure() reads the sensor.
wake-up clock start-up time + 4 cycles ISR ISR(WDT_vect) {} (nothing to do but wake) resume the instruction after SLEEP: sleep_disable() line 2 measure(); analogRead(A0), then sleep againWake-up source 2: a button
A button on pin 2 also wakes it. In Power-down a pin can wake the chip only with a LOW level on pin 2 or 3, or a pin change: not a rising or falling edge. The token is that LOW level.
attachInterrupt(digitalPinToInterrupt(2), onButton, LOW); Power-down wakes on: INT0/INT1 LOW level, pin change, watchdog, TWI address match, reset not on: RISING/FALLING edges on INT0/INT1, serial data, Timer0/1/2 (Timer2 only in Pwr-save)The catch: millis() stops
In Power-down the I/O clock is off, and so is Timer0, which counts millis(). Each 8 s sleep is missing from millis(). Serial data cannot wake the chip either: bytes that arrive while it sleeps are lost.
millis() counted by the Timer0 overflow interrupt Timer0 runs on the I/O clock -> stopped in Power-down so after 10 sleeps of 8 s, millis() has gained only the awake time, not 80 s serial no wake-up from Power-down; keep time with the WDT countThe payoff: 4.6 µA on average
Say measure() takes 2 ms, then it sleeps 8 s. The average is about 4.6 µA, 370 times less than staying awake: about 41 mAh a year instead of 14,892. The watchdog's 4.2 µA is now most of the bill.
awake 1.7 mA x 2 ms = 3.4 µA·s asleep 4.2 µA x 8 s = 33.6 µA·s average 37.0 µA·s / 8.002 s = 4.62 µA a year 41 mAh (awake all year: 14,892 mAh) watchdog 91 % of the charge
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What does a sleep mode actually change inside the ATmega328P?
Each sleep mode halts a set of clocks: Idle stops the CPU clock, Power-down stops every clock except the watchdog's oscillator. SRAM and the registers keep their values.
02The chip runs sleep_mode() in Power-down and the watchdog wakes it. Where does the program carry on?
The datasheet: after the start-up time and four cycles the MCU runs the interrupt routine and resumes from the instruction after SLEEP, so sleep_mode() returns and loop() goes on. (An ESP32 in deep sleep is different: it boots again.)
03A button is wired to pin 2 and the chip is in Power-down. Which attachInterrupt mode can wake it?
In Power-down only a level interrupt on INT0 or INT1 (pins 2 and 3 on the Uno) or a pin-change interrupt can wake the chip (datasheet §10.5). Low levels and pin changes are detected without a clock, while the I/O clock the external-interrupt logic otherwise uses is stopped. Hold the level until the chip has started up.
04Your node sleeps in Power-down between readings. Why does millis() fall behind real time?
The Arduino core counts millis() in the Timer0 overflow interrupt. Timer0 is clocked from the I/O clock, which Power-down halts, so the time spent asleep never reaches millis(). Count watchdog wake-ups instead.
05Awake 2 ms at 1.7 mA, then asleep 8 s at 4.2 µA. About what is the average current?
Charge per cycle: 1.7 mA × 2 ms = 3.4 µA·s, plus 4.2 µA × 8 s = 33.6 µA·s. Divided by 8.002 s that is 4.62 µA, and the sleeping watchdog is about 91 % of it.
Glossary
- Sleep mode
- A state in which the microcontroller switches some of its clocks off and runs no code, so it draws far less current.
- Idle
- The lightest ATmega328P sleep mode: only the CPU clock stops; timers, serial and the ADC keep running.
- Power-down
- The deepest common ATmega328P sleep mode: every clock stops except the watchdog's own oscillator, if it is enabled.
- Wake-up source
- An event that can end a sleep mode, such as a watchdog interrupt, a level on an external interrupt pin or a pin change.
- Interrupt
- A hardware signal that makes the CPU pause what it is doing and run a short routine (an ISR); in sleep, it also wakes the chip.
- Watchdog timer (WDT)
- A counter on its own 128 kHz oscillator that gives an interrupt or a reset when it runs out; up to about 8 s on the ATmega328P.
- SLEEP instruction
- The CPU instruction that enters the chosen sleep mode; avr-libc's sleep_mode() sets the sleep-enable bit and runs it.
- Average current
- The charge used over a whole sleep-wake cycle divided by the cycle's length; it decides how long a battery lasts.
Notes and sources
- All currents are typical values from the ATmega328P datasheet (DS40002061B, Table 29-8) at 25 °C and 3 V, with Active and Idle at 4 MHz. They are for the bare chip with unused modules switched off. A whole Arduino Uno board draws far more, because its regulator, USB chip and power LED stay on, so low-power nodes use a bare chip or a stripped-down board.
- Sleep only stops clocks; modules that are switched on keep drawing current. The datasheet warns that an enabled ADC stays enabled in every sleep mode, so measure() turns it off after each reading. The brown-out detector, which the Uno's fuses switch on, also draws current while asleep; on the 328P it can be turned off for the sleep with sleep_bod_disable().
- The watchdog counts 1,048,576 cycles of a 128 kHz RC oscillator (8.192 s nominal). The datasheet gives the time-out as 8.0 s, typical at 5 V; the oscillator is not accurate, so the real period varies with voltage and temperature. 8 s is the longest single time-out: sleep longer by counting wake-ups.
- Waking from Power-down takes the oscillator start-up time set by the CKSEL and SUT fuses before any code runs, which the example's 2 ms awake time does not count. A LOW level used to wake the chip must be held until that start-up is over, or the chip wakes but no interrupt runs.
- The 2 ms awake time is an assumption for the example, not a measurement. The year figures assume the pattern repeats all year at 25 °C; how long a real battery lasts is the subject of the lesson "Power budgets and battery life".
- An ESP32 sleeps differently. In light sleep its CPUs are clock-gated and resume where they stopped. In deep sleep the CPUs and most RAM are powered off; a timer, an RTC GPIO (ext0, ext1), the touch sensor or the ULP coprocessor can wake it, and it boots and runs the program from the start, so esp_deep_sleep_start() never returns.
- Microchip ATmega48A/PA/88A/PA/168A/PA/328/P datasheet (DS40002061B): §10 Power Management and Sleep Modes, §11.8 Watchdog Timer, Table 29-8
- avr-libc user manual: <avr/sleep.h> Power Management and Sleep Modes
- avr-libc source: include/avr/sleep.h
- avr-libc source: include/avr/iom328p.h (SLEEP_MODE_PWR_DOWN, WDT_vect)
- Arduino language reference: attachInterrupt() (source)
- Arduino AVR core: wiring.c (millis() and the Timer 0 overflow interrupt)
- Arduino AVR core: boards.txt (Uno fuse settings)
- Espressif ESP-IDF Programming Guide (ESP32): Sleep Modes
Quiz Unlocks after the last step.