🛠 Sommeil profond et watchdog pour économiser la pile
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
- 1 LilyPad SimpleSnap (ATmega328P @ 8 MHz;
F_CPUmust be set to 8000000, board "LilyPad Arduino") - FTDI adapter for uploading and measuring
- Multimeter set to ammeter mode between the battery and the board (optional, to measure sleep current)
- >
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
- Board or fuses set to 16 MHz:
delayand watchdog are twice as short, UART is shifted — the #1 pitfall of the LilyPad line (8 MHz, versus 16 MHz on Uno/Leonardo). - Forgetting to disable the ADC before sleep: idle consumption is multiplied.
- Trying to use
delayduring sleep: timers are frozen in power-down mode; only the watchdog (or an external interrupt) wakes the board. - Believing the entire board is asleep: the regulator/charger MCP73831 also consumes power; the bare MCU figure from the datasheet is an underestimate.
- Leaving a
wdt_enablefrom a previous sketch: the board restarts in a loop; callwdt_disableinsetupif needed.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinopwm-respiration.inoArduinoluxmetre-couture.inoArduinointerrupteur-tilt.inoArduinoservo-balayage.inoArduino