Ugenz Academy/IoT Tutorial

Power Management And Rtc

12 min read2,249 words

Reading progress

Keep learning

ESP32 Power Management and RTC

Now we'll learn how to make an ESP32 save power, sleep, wake up automatically, and keep track of time.

This is especially important for battery-powered IoT devices.

For example:

🌱 Soil Sensor ↓ ESP32 ↓ Read sensor ↓ Send data ↓ Sleep for 10 minutes ↓ Wake up ↓ Read again

Instead of keeping the ESP32 fully active 24/7.


1. ESP32 Power Modes

ESP32 has different operating modes.

The important ones are:

Active Mode Light Sleep Deep Sleep

Conceptually:

Power Consumption Active ████████████████████ Light Sleep ███ Deep Sleep █

Exact current consumption depends heavily on the ESP32 variant, board design, peripherals, Wi-Fi/Bluetooth state, regulator, LEDs, and other hardware.


2. Active Mode

This is the normal operating mode.

ESP32 │ ├── CPU running ├── Wi-Fi ├── Bluetooth ├── Sensors └── Program

Example:

void loop() { readSensor(); sendData(); delay(1000); }

The ESP32 continuously executes your program.

Good for:

  • Web servers
  • Robots
  • Remote cars
  • Real-time control
  • Displays
  • Wi-Fi dashboards

But it consumes considerably more power than sleep modes.


3. Light Sleep

In light sleep, the ESP32 reduces activity while retaining more state than deep sleep.

Conceptually:

Active ↓ Light Sleep ↓ Wake ↓ Continue

This is useful when you need relatively quick wake-up and want to retain more runtime state.


4. Deep Sleep

Deep sleep is extremely important for battery-powered IoT.

Active ↓ Read sensor ↓ Send data ↓ Deep Sleep ↓ Wake ↓ Read sensor ↓ Send data ↓ Deep Sleep

Instead of:

ESP32 ████████████████████ Running continuously

you create:

ESP32 ██ ██ ██ Sleep Sleep

This can dramatically reduce average power consumption.


5. Deep Sleep Example

A simple ESP32 Arduino example:

#include "esp_sleep.h" void setup() { Serial.begin(115200); Serial.println("ESP32 is awake!"); delay(1000); esp_sleep_enable_timer_wakeup(10ULL * 1000000ULL); Serial.println("Going to sleep..."); esp_deep_sleep_start(); } void loop() { }

The ESP32:

  1. Starts
  2. Prints a message
  3. Configures a timer
  4. Enters deep sleep
  5. Wakes after about 10 seconds
  6. Starts again from setup()

6. Important Concept: Deep Sleep Restart

One common beginner mistake is thinking:

"After deep sleep, the ESP32 continues from the next line."

Usually, deep-sleep wake behaves more like a fresh boot from the application's point of view.

So:

setup() ↓ deep sleep ↓ wake ↓ setup() again

That's why your program should be designed around the wake-up cycle.


7. Timer Wake-Up

One of the easiest wake-up methods is a timer.

Example:

esp_sleep_enable_timer_wakeup( 60ULL * 1000000ULL );

This means approximately:

60 seconds

because the API uses microseconds.

60 seconds × 1,000,000 = 60,000,000 µs

8. Sensor Monitoring Example

Imagine a soil-moisture sensor.

Instead of:

ESP32 ON Sensor ON Wi-Fi ON Forever

use:

Wake ↓ Read soil ↓ Turn on Wi-Fi ↓ Send data ↓ Turn Wi-Fi off ↓ Sleep

For example:

08:00 → Read 08:10 → Read 08:20 → Read 08:30 → Read

This can greatly improve battery life.


9. Deep Sleep + Wi-Fi

Wi-Fi is one of the major power consumers during active operation.

A battery-powered device can do:

Wake ↓ Wi-Fi ON ↓ Connect ↓ Send MQTT data ↓ Wi-Fi OFF ↓ Deep Sleep

This is a common IoT design pattern.


10. Wake-Up Sources

ESP32 can wake from sleep using different sources depending on the ESP32 variant and sleep mode.

Common sources include:

Timer GPIO Touch External events

For deep sleep, timer and GPIO wake-up are particularly useful.


11. GPIO Wake-Up

Suppose you have a push button.

ESP32 │ GPIO │ Button

The device can sleep until the button triggers the configured wake condition.

Example concept:

Deep Sleep ↓ Button pressed ↓ Wake ESP32 ↓ Run program

This is useful for:

  • Door sensors
  • Security systems
  • Remote controls
  • Battery-powered buttons

12. PIR Sensor + Deep Sleep

This is especially useful for security systems.

PIR │ ▼ ESP32 GPIO │ ▼ Deep Sleep

When motion occurs:

Motion detected ↓ ESP32 wakes ↓ Wi-Fi ON ↓ Send alert ↓ Return to sleep

This can save a lot of battery compared with keeping Wi-Fi active continuously.


13. Wake-Up Cause

You can determine why the ESP32 woke up.

The API provides wake-up-cause information.

Conceptually:

esp_sleep_wakeup_cause_t cause; cause = esp_sleep_get_wakeup_cause();

Then you can check:

if (cause == ESP_SLEEP_WAKEUP_TIMER) { Serial.println("Timer wake-up"); }

This allows different behavior depending on what caused the wake-up.


14. RTC

Now let's understand RTC.

RTC means:

Real-Time Clock

It is used to keep track of time.

For example:

10:30:25

and:

2026-10-08

15. Why RTC Matters

Suppose your ESP32 is recording sensor data.

Instead of:

Temperature = 29°C

you want:

2026-10-08 10:30:00 Temperature = 29°C

Now you can create meaningful logs.

Time Temperature 10:00 28.5°C 10:10 29.1°C 10:20 29.8°C 10:30 30.2°C

16. ESP32 RTC vs External RTC

There are two common approaches.

Internal RTC functionality

ESP32 has RTC-related hardware used for low-power operation and timekeeping functions.

External RTC module

You can also connect a dedicated module such as:

DS3231 DS1307

Example:

ESP32 │ I2C │ ▼ DS3231

An external RTC can provide very accurate timekeeping and backup-battery operation.


17. RTC + Battery

An external RTC such as DS3231 can continue keeping time even when the main ESP32 is powered down, assuming its backup supply is available.

ESP32 OFF │ X │ DS3231 │ Backup battery │ Keeps time

When ESP32 starts again:

ESP32 │ │ I2C ▼ DS3231 │ ▼ Current date/time

18. NTP Time

If your ESP32 has Internet access, you don't necessarily need an external RTC just to obtain the current time.

It can synchronize with an NTP server.

NTP:

Network Time Protocol

Architecture:

ESP32 │ │ Wi-Fi ▼ Internet │ ▼ NTP Server │ ▼ Correct Time

This is very useful for IoT projects.


19. NTP Example

ESP32 Arduino code commonly uses:

#include <WiFi.h> #include "time.h"

Then configure NTP:

configTime( 0, 0, "pool.ntp.org" );

The exact timezone configuration should be set for your location.

For Nepal, the standard offset is:

UTC +05:45

20. Getting Current Time

After synchronization:

struct tm timeinfo; if (getLocalTime(&timeinfo)) { Serial.println(&timeinfo, "%Y-%m-%d %H:%M:%S"); }

Example:

2026-10-08 23:30:15

Now you can attach timestamps to sensor readings.


21. NTP vs RTC

FeatureNTPExternal RTC
Internet neededUsually yes for syncNo
AccuracyExcellent after synchronizationExcellent
Battery backupNetwork dependentYes
Hardware requiredNo extra RTC moduleRTC module
IoT projectsExcellentExcellent
Offline operationLimited unless time was retainedExcellent

A common approach is:

Wi-Fi available ↓ NTP synchronization

and for systems that must keep accurate time without Internet:

External RTC ↓ DS3231

22. Battery-Powered ESP32

Suppose you're designing:

🌱 Smart Agriculture Sensor

Hardware:

ESP32 DHT22 Soil Moisture Sensor Battery

A poor design:

Wi-Fi ON ↓ ESP32 always ON ↓ Continuous measurement

A better design:

Wake ↓ Read sensors ↓ Wi-Fi ON ↓ Send MQTT data ↓ Wi-Fi OFF ↓ Deep Sleep

23. Example Battery IoT Cycle

Suppose you measure every 15 minutes:

00:00 → Wake → Read → Upload → Sleep 00:15 → Wake → Read → Upload → Sleep 00:30 → Wake → Read → Upload → Sleep 00:45 → Wake → Read → Upload → Sleep

Instead of running continuously.

This is called duty cycling.


24. Duty Cycle

Suppose a device is active:

10 seconds

every:

10 minutes

Most of its time is spent sleeping.

10 min ├─ 10 sec ACTIVE └─ 9 min 50 sec SLEEP

This can significantly reduce average power consumption.


25. Power Budget

When designing battery IoT, don't only look at ESP32.

Consider:

ESP32 + Wi-Fi + Sensors + Display + Relay + LED + Voltage regulator

For example, a continuously powered:

OLED

or:

relay

can consume power even when the ESP32 is sleeping.


26. Power-Saving Techniques

Turn off Wi-Fi when not needed

Wi-Fi ON ↓ Send data ↓ Wi-Fi OFF

Turn off Bluetooth when not needed

Reduce sensor measurement frequency

Instead of:

Every 1 second

use:

Every 10 minutes

when your application allows it.

Avoid unnecessary LEDs

Development boards often have LEDs and regulators that consume power.


27. Deep Sleep Smart Agriculture

A realistic architecture:

Battery │ ▼ ESP32 │ ┌───────┼────────┐ │ │ │ DHT22 Soil Light │ ▼ Read Data │ ▼ Wi-Fi │ ▼ MQTT Cloud │ ▼ Dashboard │ ▼ Deep Sleep

After the timer expires:

Deep Sleep ↓ Wake ↓ Repeat

28. Deep Sleep + Your Smart Home

For your wall-mounted smart-home project, deep sleep isn't ideal for everything.

For example:

Remote-control car

Deep Sleep ❌

because it needs continuous control.

Web server

Deep Sleep ❌

because the server wouldn't remain continuously available.

Battery temperature sensor

Deep Sleep ✅

Door sensor

Deep Sleep ✅

Soil sensor

Deep Sleep ✅

So power mode depends on the application.


29. Active vs Sleep

ApplicationRecommended
Robot carActive
Web serverActive
Real-time motor controlActive
Smart displayActive/light sleep
Door sensorDeep sleep
Soil sensorDeep sleep
Weather stationDeep sleep
Battery sensor nodeDeep sleep
PIR security nodeDeep sleep/light sleep

30. Important Deep Sleep Behavior

When ESP32 wakes from deep sleep, don't assume all normal RAM state is still available.

For example:

int counter = 10;

is not a reliable persistent counter across deep-sleep cycles.

For persistent values, use:

Preferences / NVS

or appropriate RTC memory mechanisms when suitable.

Architecture:

Deep Sleep ↓ Wake ↓ Read saved state ↓ Do work ↓ Update state ↓ Sleep

31. Combining Storage + RTC + Sleep

Now we can combine the previous topic with this one.

Imagine a battery-powered weather station:

ESP32 │ ┌───────────┼───────────┐ │ │ │ DHT22 LittleFS NTP/RTC │ │ │ └───────────┼───────────┘ │ Read Data │ Timestamp │ Save/Upload │ Deep Sleep

This is a complete IoT design pattern.


32. Battery Monitoring

You can also monitor your battery.

For example:

Battery │ Voltage Divider │ ▼ ESP32 ADC

Then ESP32 estimates:

Battery = 3.9V

or a percentage estimate.

⚠️ You must design the voltage-divider ratio so the voltage reaching the ESP32 ADC stays within the safe input range for the specific board/ADC setup. Never connect a battery voltage directly to a GPIO/ADC pin if it can exceed the pin's allowed voltage.


33. RTC + Sensor Logging

Now your data can look like:

2026-10-08 08:00:00 Temperature: 27.2°C Humidity: 64% 2026-10-08 08:15:00 Temperature: 27.8°C Humidity: 63% 2026-10-08 08:30:00 Temperature: 28.5°C Humidity: 62%

This is much more useful than:

27.2 27.8 28.5

34. Practical Project

🔋 Battery-Powered Farm Sensor

Components:

ESP32 DHT22 Soil Moisture Sensor Battery

Operation:

1. Wake from timer ↓ 2. Read DHT22 ↓ 3. Read soil moisture ↓ 4. Connect Wi-Fi ↓ 5. Publish MQTT data ↓ 6. Disconnect Wi-Fi ↓ 7. Save required state ↓ 8. Deep Sleep ↓ 9. Wake after 15 minutes ↓ 10. Repeat

This is a very realistic IoT architecture.


⚠️ Common Mistakes

1. Thinking deep sleep is the same as delay()

delay(60000);

keeps the processor running.

Deep sleep actually changes the power state.


2. Expecting the web server to work during deep sleep

It won't continuously respond while the ESP32 is sleeping.


3. Leaving Wi-Fi running unnecessarily

For battery devices:

Read → Upload → Disconnect → Sleep

is often much better.


4. Writing flash continuously

Avoid:

Read sensor ↓ Write flash ↓ Read sensor ↓ Write flash

unless there's a real need.


5. Ignoring peripherals

A sleeping ESP32 doesn't guarantee a low-power system if:

LCD Relay Sensor LED Regulator

is still consuming significant current.


🧠 Quick Summary

Active

Full operation ↓ Higher power

Light Sleep

Reduced activity ↓ Quick wake

Deep Sleep

Very low-power state ↓ Wake when needed

RTC

Timekeeping

NTP

Internet → Accurate time

External RTC

DS3231 → Accurate offline time

Battery IoT pattern

Wake ↓ Sense ↓ Connect ↓ Send ↓ Sleep

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy