🛠 Sommeil profond et watchdog pour économiser la pile

LilyPad Arduino · avance · Schéma : IMG-lilypad-avance-1-lilypad-sleep-watchdog.svg — Carte endormie sous un croissant de lune, icône watchdog avec horloge près de l'ATmega328P et batterie à faible consommation.

Objectives

Extend the battery life of the 110 mAh battery on the SimpleSnap: put the ATmega328P into power-down mode, wake it up with the watchdog, and disable the ADC before sleeping. We also address the 8 MHz vs 16 MHz pitfall: a sketch configured for 16 MHz runs everything twice as fast (incorrect delay, shifted UART).

Hardware

Complete Code

// Sommeil power-down + réveil watchdog (~4 s) — LilyPad ATmega328P @ 8 MHz
#include <avr/sleep.h>
#include <avr/wdt.h>

const uint8_t LED = 13;

ISR(WDT_vect) { /* simple réveil : le travail se fait dans loop */ }

void setup {
  // F_CPU = 8000000 (carte « LilyPad Arduino ») : jamais 16 MHz ici !
  pinMode(LED, OUTPUT);
  ADCSRA &= ~_BV(ADEN);          // ADC coupé : économie immédiate
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // mode le plus profond
  wdt_reset;
  MCUSR  &= ~_BV(WDRF);
  WDTCSR  = _BV(WDCE) | _BV(WDE);      // suite d'accès aux fuseaux watchdog
  WDTCSR  = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); // 2^15 cycles / 8 MHz ≈ 4,1 s
}

void loop {
  digitalWrite(LED, HIGH);       // « battement de cœur » bref
  delay(20);
  digitalWrite(LED, LOW);

  set_sleep_enable(true);
  sleep_cpu;                   // dort jusqu'à l'expiration du watchdog
}

Expected Verification

The LED flashes briefly, then the board sleeps for about 4 seconds; the pattern repeats indefinitely. The ammeter inserted between the battery and the board drops significantly below the awake current (LED on + CPU at 8 MHz). If the rhythm seems twice as fast, F_CPU is set to 16 MHz: re-select the LilyPad board and re-upload.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials