🛠 Mode basse consommation : sleep profond et watchdog
Objectives
Turn the Esplora into an autonomous thermometer-watchdog: wake up every second via the hardware watchdog, read the temperature sensor using the library, trigger a buzzer alert if the threshold is exceeded, then return to SLEEP_MODE_PWR_DOWN (the deepest sleep mode of the ATmega32u4). You will learn how to turn off the ADC and disable interrupts during sleep — and especially the limitations specific to this board: it is powered ONLY by USB (no documented jack), so the regulator and USB transceiver consume power continuously.
Hardware
- Arduino Esplora + micro-USB cable (or a USB battery such as a power bank for a portable setup).
- Esplora library; avr/sleep.h, avr/wdt.h (core).
- No external components.
Complete code
/* Veilleur de temperature en sommeil profond.
Reveil 1 s (watchdog) -> lecture capteur -> bip si > 30 degres -> re-dormir.
Attention : sur Esplora, seule l'alimentation USB est documentee ; le
courant de la carte en veille reste domine par le regulateur/USB, pas par
l'ATmega32u4 seul. */
#include <Esplora.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
volatile bool reveille = false;
ISR(WDT_vect) {
reveille = true; // simple drapeau : le traitement se fait dans la loop
}
void dormir {
Esplora.writeRGB(0, 0, 0); // tout eteint avant de dormir
Esplora.noTone;
ADCSRA &= ~(1 << ADEN); // ADC off (conseille avant sleep)
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable;
MCUCR |= (1 << BODSE) | (1 << BODS); // eteind BOD (2 cycles)
MCUCR = (1 << BODS);
sei;
sleep_cpu; // ici on dort jusqu'au watchdog
// --- reprise d'execution ---
cli;
sleep_disable;
ADCSRA |= (1 << ADEN);
}
void setup {
// Watchdog en mode interruption seule : 1 s
wdt_reset;
WDTCSR = (1 << WDCE) | (1 << WDE);
WDTCSR = (1 << WDIE) | (1 << WDP2); // 1 s, interruption sans reset
Serial.begin(9600);
}
void loop {
dormir;
if (!reveille) return;
reveille = false;
int t = Esplora.readTemperature(DEGREES_C);
Serial.print("T="); Serial.print(t); Serial.println("C (reveil watchdog)");
if (t > 30) {
Esplora.writeRGB(255, 0, 0);
Esplora.tone(880, 300);
delay(400);
}
}
Expected verification
The serial monitor displays a line approximately every second. Squeeze the board: above 30 °C, a beep and a red flash follow the wake-up. Between wake-ups, the CPU is in power-down (the L LED blinks if your sketch does so — here it does not). >
Common errors
- Forgetting to clear the sleep flag: without
sleep_disableupon waking, any futuresleep_cpuwill put the board to sleep forever; always structure your code as enable → sleep → disable. - Watchdog in reset mode: without the WDTCSR sequence above (WDIE without WDE), a timeout will restart the board instead of waking it up — the Caterina bootloader may then be re-triggered.
- Processing inside the ISR: reading the multiplexer (settling time ~50 µs) inside an interrupt blocks everything; only the flag should be set within the ISR.
- Expecting battery autonomy: the Esplora has neither a jack nor documented external power guidelines; for portable use, provide a USB battery cable, and know that sleep does not cut off the upstream regulator's consumption.
- The serial monitor "breaks" sleep: on some platforms, keeping the CDC open prevents deep sleep; test on battery, with the cable unplugged, to measure the actual behavior.
Other tutorials for this board
Code examples for this board
joystick-couleur.inoArduinojeu-boutons.inoArduinoslider-luminosite.inoArduinothermometre-couleur.inoArduinosonometre.inoArduinoaccelerometre-tilt.inoArduino