OC
OceanRemote
Low-code IoT platform
← Back to Course

ESP32 Sleep Modes Overview

ESP32 Sleep Modes Overview

💤 ESP32 Sleep Modes - Maximizing Battery Life

🔋 What You'll Learn in This Lesson:

  • 💤 Choose the right sleep mode for your application
  • 📊 Reduce power from 150mA to 2.5μA (60,000x reduction!)
  • 🔄 Preserve data using RTC memory across deep sleep
  • ⏰ Wake ESP32 on timer, button press, or sensor trigger

ESP32 has several sleep modes that dramatically reduce power consumption. A 2600mAh battery lasts only 17 hours in active mode but can last over 10 years in deep sleep! The key is sleeping between readings.

📊 Sleep Mode Comparison

ModeCurrentWake SourcesRAM RetentionBest For
Active80-240mA
💡 Key Insight:

The 60,000x power reduction from active mode to deep sleep means a battery that lasts 1 day in active mode would last over 100 years in deep sleep! But your sensor still needs to wake up to take readings.

📊 Deep Sleep Code - Most Common for Sensors

#include <esp_sleep.h>

// RTC memory survives deep sleep!
RTC_DATA_ATTR int bootCount = 0;

void setup() {
    Serial.begin(115200);
    bootCount++;
    
    Serial.printf("Boot #%d\n", bootCount);
    
    // Read sensors (takes ~1 second)
    int moisture = analogRead(32);
    float temp = readTemperature();
    
    // Send data (connect WiFi, send, disconnect)
    sendToCloud(moisture, temp);
    
    // Go to deep sleep for 1 hour
    // 1 hour = 1 * 60 * 60 * 1,000,000 microseconds
    esp_sleep_enable_timer_wakeup(3600000000);
    esp_deep_sleep_start();
    
    // Code here NEVER runs - ESP32 is sleeping!
}

void loop() {
    // This never runs - everything in setup()
}
    

📊 Power Consumption Calculation

═══════════════════════════════════════════════════════════════════════════════
                    BATTERY LIFE CALCULATION
═══════════════════════════════════════════════════════════════════════════════

  2600mAh battery, reading every 15 minutes:
  
  Active mode (5 seconds): 150mA × 5 sec = 0.208mAh
  Deep sleep (895 sec):     2.5μA × 895 sec = 0.00062mAh
  Total per 15min cycle:                   = 0.2086mAh
  
  Cycles per day: 96
  Daily power use: 96 × 0.2086 = 20mAh/day
  
  ⚡ Battery life = 2600mAh ÷ 20mAh/day = 130 DAYS!
  
  Increase to 1 hour intervals → 2+ years battery life!

═══════════════════════════════════════════════════════════════════════════════
💡 RTC Memory - Data That Survives Deep Sleep:
  • RTC_DATA_ATTR - Variables kept in RTC memory (8KB available)
  • Use for: Boot counters, sensor calibrations, timestamps, error logs
  • Don't use for: Large arrays, strings, or non-critical data
  • Regular variables are LOST - only RTC_DATA_ATTR survives

📖 Wake Sources - Triggering from Sleep

// Timer wake (most common for sensors)
esp_sleep_enable_timer_wakeup(3600000000);  // 1 hour

// External GPIO wake (button or sensor trigger)
esp_sleep_enable_ext0_wakeup(GPIO_NUM_0, LOW);  // Wake on button press

// Touch wake (no external button needed)
touchSleepWakeUpEnable(T0, 40);  // Wake on touch

// Light sleep with timer (faster wake, more power)
esp_light_sleep_start();
    
⚠️ Common Deep Sleep Mistakes:
  • ❌ Calling delay() after sleep start → Code after esp_deep_sleep_start() never runs
  • ❌ Using Serial.print() after wake → Works but uses power, keep minimal
  • ❌ Not disabling WiFi before sleep → WiFi must be off first
  • ❌ Using analogRead() with deep sleep → Works, but takes 1-2 seconds
📖 Case Study - 2+ Years on Two AA Batteries:

A soil moisture sensor in remote Kenya needed long battery life:

  • Challenge: No power lines, batteries hard to replace
  • Solution: Deep sleep between readings (wake every 4 hours)
  • Setup: Wake → read sensor → send data (10 sec) → deep sleep
  • Result: 2,500mAh batteries lasted over 2 years!
  • Savings: No battery changes needed for entire growing season

"We check data weekly and haven't replaced batteries in 20 months." - Field Technician, Kenya

🎉 Key Takeaways:
  • ✅ Deep sleep = 2.5μA (60,000x less than active mode)
  • ✅ Use RTC_DATA_ATTR to preserve data across sleep cycles
  • ✅ 15-minute readings = 130 days battery life
  • ✅ 1-hour readings = 2+ years battery life
  • ✅ Timer wake is best for regular sensor readings

Pro tip: Always test your sleep current with a multimeter - you should see microamps, not milliamps!

Sleep ModeCurrentWake TimeBest For
Modem Sleep10-30mA<1msWiFi with low latency
Light Sleep0.8-2mA~1ms1-60 second intervals
Deep Sleep2.5-10μA~2ms1 minute+ intervals
Hibernation2.5μA~10msHours/days intervals
💡 Key Takeaways:
  • Apply these concepts directly to your farm or project.
  • Take notes on important details for the quiz.
  • Use the button below to track your progress.