🛠 Mode basse consommation : power-down et watchdog
Objectives
Put the ATmega32U4 to sleep in power-down mode (microampere-level consumption on the MCU) and wake it up every second using the watchdog, using direct AVR C programming. Understand what this mode implies on a board: USB is disconnected during sleep and the Serial Monitor sees nothing.
Hardware
- 1 Arduino Micro + micro-USB cable (or power supply on Vin)
- The onboard LED (pin 13) as a wake-up indicator
- Optional: an ammeter in series to measure current during sleep
Complete code
// Sommeil power-down + reveil par watchdog (~1 s), en AVR C direct.
// Attention : en power-down, l'USB CDC est mort ; pas de Serial pendant le sommeil.
#include <avr/sleep.h>
#include <avr/wdt.h>
volatile bool reveil = false;
ISR(WDT_vect) { // le watchdog reveille : travail minimal
reveil = true;
}
void dormir1s {
MCUSR &= ~(1 << WDRF); // effacer la raison de reset
wdt_reset;
WDTCSR |= (1 << WDCE) | (1 << WDE); // autoriser la modification
WDTCSR = (1 << WDP0) | (1 << WDP1) | (1 << WDIE); // ~1 s, mode interruption
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // mode le plus profond
sleep_enable;
sei;
sleep_cpu; // dort jusqu'au watchdog
sleep_disable;
}
void setup {
pinMode(LED_BUILTIN, OUTPUT);
wdt_disable;
}
void loop {
digitalWrite(LED_BUILTIN, HIGH); // temoin bref du reveil
delay(30);
digitalWrite(LED_BUILTIN, LOW);
dormir1s; // ~1 s de sommeil profond
}
Expected verification
LED 13 blinks briefly every second. By inserting an ammeter between the power supply and the board, a significant drop in current is observed during sleep compared to wake-up. >
Common errors
- Expecting serial messages during sleep: the 32U4 is asleep, USB is disconnected; the Monitor displays a port error.
- Forgetting to re-enable sleep_disable or disarm the watchdog elsewhere: the wake-up behavior becomes inconsistent.
- Configuring the watchdog in reset mode instead of interrupt mode (WDIE bit): the board restarts instead of waking up.
- Relying on USB wake-up: in power-down, an external interrupt (pins 0, 1, 2, 3, 7) is better for wake-up via button.
- Leaving pins as floating inputs: they consume power; set them as outputs in a stable state or with pull-ups before sleeping.
Other tutorials for this board
- Faire clignoter la LED embarquée (et comprendre les LED TX/RX)
- Lire un bouton avec une résistance de pull-up interne
- Lire un capteur analogique sur le convertisseur 10 bits
- Transformer le Micro en clavier USB (HID) avec bouton de sécurité
- Pont Serial vers Serial1 : passerelle USB vers TTL
- Piloter un servo moteur avec le PWM et un potentiomètre
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinoclavier-auto-tape.inoArduinosouris-cercle.inoArduinotelecommande-media.inoArduinobouton-clic-souris.inoArduino