🛠 Deep sleep sur batterie avec réveil par GPIO16

ESP8266 (ESP-12E module) · avance · Schéma : IMG-esp8266-esp-12e-avance-1-esp8266-deep-sleep-batterie.svg — Module ESP-12E alimenté par régulateur LDO sur batterie LiPo, fil GPIO16 relié à RST pour le réveil, LED de test déconnectée, bouton de réveil externe optionnel.

Objectives

Transform the ESP8266-ESP-12E into a battery-powered sensor: in deep sleep, the chip consumes around ten microamperes, compared to several tens of milliamperes during Wi-Fi operation, enabling battery life of several months. Wake-up is handled by the internal RTC timer, and the mandatory wiring GPIO16 → RST also allows for external wake-up (GPIO16 generates a pulse that restarts the module via the reset pin). Crucial point: deep sleep erases all RAM — no counters or variables survive; we use RTC memory (RTC_SLOW_MEM) to preserve state between wake-ups, and everything must be reinitialized at each startup.

Hardware

Complete code

#include <ESP8266WiFi.h>

// Memoire RTC : survit au deep sleep (RAM classique : effacee !)
RTC_DATA_ATTR uint32_t compteur = 0;

extern "C" {
  #include "user_interface.h"
}

void setup {
  Serial.begin(115200);
  compteur++;
  Serial.printf("\nReveil n°%u  raison: %s\n", compteur,
                user_rtc_get_reset_reason == POWERON_RESET ? "mise sous tension" : "deep sleep");

  // --- Travail court : lecture capteur, mesure batterie ---
  uint32_t vbat = ESP.getVcc;                    // mV (si calibration AD0 faite)
  Serial.printf("Vcc=%u mV\n", vbat);

  // Optionnel : publier en Wi-Fi puis attendre 1 s que le paquet parte
  // WiFi.mode(WIFI_STA); WiFi.begin(SSID, PASS);
  // unsigned long t0 = millis;
  // while (WiFi.status != WL_CONNECTED && millis - t0 < 8000) delay(50);
  // ... publish MQTT ...
  // WiFi.forceSleepBegin;                        // radio off avant de dormir

  Serial.println("Endormissement 30 s (GPIO16 doit etre cablee a RST)");
  Serial.flush;
  delay(50);
  ESP.deepSleep(30 * 1000000ULL);                  // 30 s ; 0 = attendre un reveil externe
}

void loop {
  // Jamre atteint : deepSleep ne revient jamais
}

Expected verification

Unplug/replug: the counter restarts at 1 (RTC memory is also lost without power), then the module displays "Wake-up n°1" and sleeps. Approximately every 30 seconds, a new wake-up appears with the counter incremented (2, 3, 4...) while the module has not been touched — proof that the RTC timer and the GPIO16→RST wire are working. With a multimeter in series with the battery, the current should drop to a few hundred µA on average (excluding the dev board's regulator). With deepSleep(0), the module only wakes up if you briefly connect RST to ground.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials