Index
Whether you’re building a battery-powered IoT device or just want your ESP32 to consume less power when idle, Deep Sleep mode is your best friend. With MicroPython, it’s surprisingly easy to implement deep sleep on both the ESP32 and ESP32-C3 chips.
In this blog post, we’ll walk you through:
- What Deep Sleep is
- Why and when to use it
- How to use Deep Sleep in MicroPython
- A working example
What is Deep Sleep?
Deep Sleep is a low-power mode where the ESP32 shuts down most of its components, keeping only the RTC (Real-Time Clock) and a few essential peripherals powered. This drastically reduces power consumption — ideal for devices that wake up periodically (e.g., temperature sensors, motion detectors).
When in deep sleep:
- The main processor and most peripherals are off.
- Only the RTC memory and RTC peripherals stay active.
- The ESP32 can wake up based on timers, external GPIO triggers, or even capacitive touch (on regular ESP32).
ESP32 vs ESP32-C3: Key Differences in Deep Sleep
- ESP32 (dual-core): Supports wakeup via timer, GPIO, touch, and ULP (Ultra Low Power) co-processor.
- ESP32-C3 (single-core RISC-V): Supports wakeup via timer and GPIO (no touch sensor or ULP).
MicroPython provides built-in modules to manage Deep Sleep on both variants.
Deep Sleep with MicroPython: Getting Started
✅ Required Modules
You’ll use the machine
and time
modules:
import machine
import time
Basic Deep Sleep Example (Timer Wakeup)
This example puts the ESP32/ESP32-C3 into deep sleep for 10 seconds.
import machine
import time
print("Waking up...")
time.sleep(1) # simulate some task
# Set a deep sleep timer for 10 seconds (10,000 milliseconds)
print("Going to deep sleep for 10 seconds...")
machine.deepsleep(10000)
What happens?
- The code prints a message, simulates a task with
time.sleep(1)
, then enters deep sleep. - The board will restart after 10 seconds. The script starts from the beginning when it wakes up.
How to Know It’s a Wake from Deep Sleep?
You can check the reset cause:
from machine import reset_cause, DEEPSLEEP_RESET
if reset_cause() == DEEPSLEEP_RESET:
print("Woke up from deep sleep")
else:
print("Power on or hard reset")
Combine it with the earlier code:
import machine
import time
from machine import reset_cause, DEEPSLEEP_RESET
if reset_cause() == DEEPSLEEP_RESET:
print("Woke up from deep sleep")
else:
print("Power on or hard reset")
time.sleep(1)
print("Going to deep sleep for 10 seconds...")
machine.deepsleep(10000)
GPIO Wakeup Example (Advanced)
Let’s say you want to wake up the board when a button is pressed (connected to GPIO 0):
import machine
import time
wake_pin = machine.Pin(0, mode=machine.Pin.IN, pull=machine.Pin.PULL_UP)
# Configure the pin to wake from deep sleep on falling edge
machine.wake_on_ext0(pin=wake_pin, level=machine.Pin.WAKE_LOW)
print("Going to deep sleep. Press button to wake up.")
time.sleep(1)
machine.deepsleep()
⚠️ wake_on_ext0()
works on ESP32. ESP32-C3 uses wake_on_ext1()
with different behavior and requires esp32
module for config.
Use Case: Battery-Powered Temperature Logger
Imagine a weather sensor that wakes up every 10 minutes, reads temperature, logs it to SD or sends via Wi-Fi, then sleeps again. Deep sleep ensures that your battery lasts for months!
Pro Tip: Avoid Repeating Setup Code on Wake
import machine
import time
rtc = machine.RTC()
wake_count = rtc.memory() or b"0"
wake_count = int(wake_count) + 1
print("Wake count:", wake_count)
rtc.memory(str(wake_count))
machine.deepsleep(10000)
Conclusion
Deep Sleep is essential for any energy-efficient ESP32/ESP32-C3 application. With MicroPython, it takes just a few lines to cut down power usage drastically.
✅ Use machine.deepsleep()
for timer-based sleep.
✅ Use wake_on_ext0()
or wake_on_ext1()
for GPIO-based wakeups.
✅ Combine both for smarter control.