🛠 Sommeil profond et réveil par watchdog

Arduino Nano Every · avance · Schéma : IMG-nano-every-avance-2-nanoevery-sleep-watchdog.svg — Nano Every endormie alimentée par une pile, croissant de lune au-dessus de la carte, petit chien de garde (watchdog) dessiné à côté d'un chronomètre une seconde, LED 13 allumée brièvement

Objectives

Reduce the Nano Every's power consumption for a battery-powered sensor: put the core to sleep in SLEEP_MODE_PWR_DOWN and wake it up periodically using the ATmega4809 watchdog. On this family, the watchdog triggers a reset by default: we use this reset as an alarm, distinguishing a watchdog restart from a normal startup.

Hardware

Complete code

// Sommeil profond, reveil ~1 s via watchdog (reset comme alarme)
#include <avr/sleep.h>
#include <avr/wdt.h>

void setup {
  pinMode(LED_BUILTIN, OUTPUT);
  wdt_reset;
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  // mode le plus profond
  sleep_disable;                       // armement a la volee
  wdt_enable(WDTO_1S);                   // delai 1 s
}

void loop {
  digitalToggle(LED_BUILTIN);            // signal d'activite brève
  delay(50);
  wdt_reset;                           // eviter le reset immediat
  sleep_enable;
  sleep_cpu;                           // dort jusqu'au reset WDT
  // A ce point : le watchdog a reinitialise la carte, setup tourne
  // a nouveau avant de revenir ici.
}

Expected verification

LED 13 blinks briefly every second. Between two blinks, the core is off: when powered by battery and with USB disconnected, a microammeter should show an average significantly lower than active mode (the exact gain depends on the mode and any peripherals left active).

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials