🛠 Mode basse consommation : power-down et watchdog

Arduino Micro · avance · Schéma : IMG-micro-avance-3-micro-low-power-watchdog.svg — Arduino Micro en sommeil power-down : horloge 16 MHz coupée, seul le watchdog interne réveille le microcontrôleur toutes les secondes, LED témoin d'une brève activité au réveil

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials