Search code examples
esp32

How to improve ESP32 sleep time accuracy


When sending an esp32 device to sleep for approximately a day, it wakes up 3.34% earlier than expected. This amounts to approx 48 minutes.

Is this the expected accuracy of this device or can it be tuned to be more accurate?

The concrete device is an ESP32-CAM and it is running at 80MHz at approx 25°C room temperature.

Code to send the device to sleep:

    unsigned int time_to_sleep_sec = 86400;
    esp_sleep_enable_timer_wakeup(1ULL * unsigned int time_to_sleep_sec * 1000 * 1000);
    esp_deep_sleep_start();

Instead of 86400 seconds, the device woke up after approx 83465 seconds.


Solution

  • the default RTC clock source is only 5% accurate, so it's within spec. Check the info:

    The RTC timer has the following clock sources:

    Internal 150kHz RC oscillator (default): Features lowest deep sleep current consumption and no dependence on any external components. However, as frequency stability is affected by temperature fluctuations, time may drift in both Deep and Light sleep modes.

    External 32kHz crystal: Requires a 32kHz crystal to be connected to the 32K_XP and 32K_XN pins. Provides better frequency stability at the expense of slightly higher (by 1 uA) Deep sleep current consumption.

    External 32kHz oscillator at 32K_XN pin: Allows using 32kHz clock generated by an external circuit. The external clock signal must be connected to the 32K_XN pin. The amplitude should be less than 1.2 V for sine wave signal and less than 1 V for square wave signal. Common mode voltage should be in the range of 0.1 < Vcm < 0.5xVamp, where Vamp is signal amplitude. Additionally, a 1 nF capacitor must be placed between the 32K_XP pin and ground. In this case, the 32K_XP pin cannot be used as a GPIO pin.

    Internal 8.5MHz oscillator, divided by 256 (~33kHz): Provides better frequency stability than the internal 150kHz RC oscillator at the expense of higher (by 5 uA) deep sleep current consumption. It also does not require external components.

    So if you aren't able to add components, the 5uA 'expense' appears to be reasonable. Otherwise the best solution is to add an external 32kHz crystal.

    Or you wakeup the device during sleep to correct the time with help of the internet like SNTP.