Writing · How-to

How to wake an ESP32 from deep sleep at a specific time of day

To wake an ESP32 at a set time, such as 07:00, compute the seconds between now and the next target time and pass that to the RTC timer before deep sleep. The chip has a countdown timer, not an alarm clock. The work is in the edge cases: a clock that drifts, a clock that is gone after a brownout, time zones, and a schedule that changes from the server. Below are the four ways to do it, the code pattern, and what our ESP32-S3 e-paper firmware does in each edge case.

Published 2026-11-18. Written for firmware engineers and hardware founders building battery devices on ESP32. The firmware behaviour described is from our own ESP32-S3 + E Ink Spectra 6 sign firmware; ESP32 facts are from Espressif’s documentation, linked inline.

Why this matters for battery devices

A battery device that wakes on a fixed interval wastes energy on wakes nobody needs. A shop sign that changes its offer at opening time and again at lunch needs two wakes a day, not 24. On our stack with a 3,000 mAh cell, hourly check-ins last about 40 days and four a day about 7.8 months; the full table is in how long battery e-paper signs last. Waking at the right wall-clock times is how you get the long end of that range without the content changing late.

RTC timer wakeup: The ESP32’s RTC controller has a built-in timer that wakes the chip after a set amount of time. Its resolution depends on the RTC slow clock source, according to Espressif’s sleep modes documentation. You give it microseconds, not a time of day.

Four ways to wake at a time of day

ApproachHow it worksExtra partsBest for
Compute the timer from a synced clockRead time(), find the next target time, sleep for the differenceNoneMost products; resync each time you connect
Server tells the device when to wakeServer sends the schedule and current time with each sync; device computes the timerNoneFleets where operators change schedules centrally
ESPHome deep_sleep untiluntil: "16:00:00" plus a time_id pointing at a time sourceNoneESPHome and Home Assistant devices
External RTC alarm (e.g. DS3231)RTC holds the time on a coin cell and pulls a pin low at the alarm timeRTC module, coin cell, an RTC-capable GPIODevices that must wake on time without ever reaching a network

ESPHome has this built in: its deep_sleep component accepts until and time_id on ESP32. An external RTC such as the DS3231 keeps time on a coin cell with a temperature-compensated oscillator and signals its alarm on the SQW pin, as Random Nerd Tutorials’ DS3231 guide shows. For a connected product, the first two rows are usually enough, and they need no extra parts.

How to do it in firmware, step by step

1. Get a trustworthy time

Set the clock from SNTP or from your own server whenever the device is online. Our signs do not run SNTP: the cloud sync response includes now_utc, and the firmware calls settimeofday() with it on every successful sync. One round trip gives the device its schedule and the current time together.

2. Store the schedule as minutes since midnight

Our schedule is a JSON array of "HH:MM" strings, up to 24 of them, or a single "every:N" entry for interval mode (capped at 1,440 minutes). The parser converts each time to minutes since local midnight, skips invalid entries, and sorts them.

3. Find the next boundary in local time

Convert UTC to local time with the device’s offset, find the minutes since local midnight, and take the first scheduled time later than now. If none is left today, use the first time tomorrow. Convert that back to UTC epoch seconds.

time_t localNow = nowUtc + tzOffMin * 60;
struct tm t; gmtime_r(&localNow, &t);
int nowMin = t.tm_hour * 60 + t.tm_min;
time_t localDay0 = localNow - (nowMin * 60 + t.tm_sec);

for (int i = 0; i < n; i++)            // mins[] sorted ascending
  if (mins[i] > nowMin)
    return localDay0 + mins[i] * 60 - tzOffMin * 60;
return localDay0 + 86400 + mins[0] * 60 - tzOffMin * 60;  // tomorrow

4. Arm the timer and sleep

Subtract now from the next wake, multiply by 1,000,000 and pass it to esp_sleep_enable_timer_wakeup(), then call esp_deep_sleep_start(). If you also want a button to wake the device, arm ext0 on an RTC-capable GPIO; both wake sources can be active at once, and esp_sleep_get_wakeup_cause() tells you which fired.

5. Log why the device chose that duration

Print the schedule, offset, current time and the reason for the duration on every sleep. Ours logs one of wall-clock, interval, no-clock-retry, empty-schedule or fallback-60m. That one line is how we found the bug in the next section.

The edge cases that break scheduled wakes

Clock drift in deep sleep

The RTC timer keeps time during sleep, but by default it runs from an internal RC oscillator whose frequency, in Espressif’s words, is affected by temperature. The same page calls drift of several seconds per day acceptable for suitable designs, and lists an external 32 kHz crystal as the more stable option at about 1 µA more deep-sleep current. Our firmware does not add a crystal; it resyncs the clock on every wake that reaches the cloud, so drift never accumulates past one sleep.

No clock after a brownout or power loss

A brownout or a battery swap wipes the time. If the firmware then computes a wall-clock wake from an epoch near zero, the result is nonsense. Ours treats any time before late 2023 as “no clock”. Interval schedules still work because they do not need wall time. Anything else retries in five minutes so the next sync can restore the clock. That retry exists because our black-box log showed a device on a one-minute schedule sleeping a full hour after a brownout, which looked like a stuck sign; our brownout debugging checklist covers the logging.

Time zones and daylight saving

A fixed offset in minutes is the simplest thing to store, and it is what our device settings use today. It is also wrong for half the year in any region with daylight saving, unless someone updates it. Two fixes: store a POSIX TZ string on the device so the C library applies the rules, or have the server compute the current offset from a named time zone and send it with every sync. For a fleet, the server-side option is easier to correct when rules change.

No schedule yet

A freshly set-up device may not have a schedule. Ours falls back to waking every 60 minutes, so it still checks in and picks up a schedule when one is assigned.

Waking with no network

If the device wakes on time but cannot reach Wi-Fi, decide what it shows. Our signs keep a local image cache; in cached-slot mode the schedule and the RTC choose which stored image to show without touching Wi-Fi at all, and in cloud mode an offline wake moves to the next cached image. Either way the sign keeps changing on schedule through an outage, which makes clock accuracy matter more, not less.

Pre-field checklist

The wider list of things to check before field units is in our dev kit to product checklist, and how to build an ESP32 meeting-room sign puts the schedule to use on a real sign. If you are still choosing a chip, see ESP32-S3 vs ESP32-C6 for e-paper.

FAQ

Can an ESP32 wake from deep sleep at a specific time?

Yes, but not directly. The ESP32's RTC timer only counts down a duration, so firmware reads the current time, works out how many seconds remain until the next target time, and passes that (in microseconds) to esp_sleep_enable_timer_wakeup() before calling esp_deep_sleep_start(). An external RTC with an alarm pin is the other option.

Does the ESP32 keep time in deep sleep?

Yes. Espressif's system time documentation says the RTC timer keeps time in sleep modes, while the high-resolution timer does not. By default the RTC runs from an internal RC oscillator whose frequency moves with temperature, so expect drift, and resync the clock whenever the device is online.

How accurate is the ESP32 deep sleep timer?

With the default internal RC oscillator, Espressif describes drift of several seconds per day as acceptable for suitable designs, and notes the frequency is affected by temperature. An external 32 kHz crystal is more stable and costs about 1 µA extra in deep sleep, per the same page. For scheduled wakes, resyncing every time the device connects matters more than the clock source.

What happens to the ESP32 clock after a power loss or brownout?

The time is lost and time() returns a value near zero. Firmware should detect this (our check is any epoch before late 2023) and avoid computing a wall-clock wake from it. Our signs retry in five minutes so the next sync can restore the clock, rather than sleeping for an hour on a guess.

How do I handle time zones and daylight saving on an ESP32?

Either store a POSIX TZ string on the device so it can apply daylight saving itself, or have the server send the current offset at every sync. A fixed offset entered once, which is what our device settings use today, needs updating when the clocks change.

Should I use ESPHome's deep_sleep until option?

For a home or prototype device built in ESPHome, yes: deep_sleep.enter with until and a time_id does the calculation for you on ESP32. For a product with its own firmware and cloud, you will end up writing the same calculation plus your own handling for a missing clock and for schedule changes from the server.