Page 1 of 1

The esp32 timer drifted over half an hour in the course of 10 hours (in deep sleep). Is this normal?

Posted: Sat Jul 31, 2021 1:18 pm
by cal237
My esp32 is supposed to wake up at a certain time. It calculates the number of microseconds for esp_sleep_enable_timer_wakeup() by getting the current time from NTP and calculating the difference between now and the desired wakeup time. I turned in on around 10:30pm and set it to wake up at 8:30am. It woke up at 7:55am. I knew the RTC timer clock was not very accurate, but I was not expecting it to drift that far. Is this normal?

Re: The esp32 timer drifted over half an hour in the course of 10 hours (in deep sleep). Is this normal?

Posted: Mon Aug 02, 2021 7:25 pm
by fabianoriccardi
If you rely on the internal oscillator (a simple RC), it should have a relative error of up to 10% (if I recall well). Maybe ESP32 can be slightly better, but in general RC oscillator has a significant drift. So if it deep sleeps few hours, it can drift *minutes*.

Re: The esp32 timer drifted over half an hour in the course of 10 hours (in deep sleep). Is this normal?

Posted: Wed Aug 04, 2021 4:31 am
by zmlee79
In my tests on some devices, after entering the deep-sleep mode, the frequency of RTC_CAL_8MD256 is reduced by about 6%. By calibrating the error between the sleep mode and the actual time, before entering the sleep mode

const double clockRate = 1.06; // Each device needs to be calibrated independently
REG_WRITE(RTC_SLOW_CLK_CAL_REG, (uint32_t)(0.5 + rtc_clk_cal(RTC_CAL_8MD256, CONFIG_ESP32_RTC_CLK_CAL_CYCLES) * clockRate));

A clock accuracy of about 50ppm can be obtained. I recalibrated the RTC clock by waking up for about 40 milliseconds every about 1 minute in the deep-sleep mode. In the test of my two devices, I got an accuracy of 10ppm, which is very close to the accuracy of a 40M crystal oscillator. The error of one device after running for 3 days is 300 milliseconds, and the error of another 8 hours of testing is 200 milliseconds

Re: The esp32 timer drifted over half an hour in the course of 10 hours (in deep sleep). Is this normal?

Posted: Wed Aug 11, 2021 11:26 am
by Neil.Macmullen
If you need to avoid clock drift then you really need to use an external 32K crystal in your circuit. There are some dedicated APIs for this....

Code: Select all

rtc_clk_32k_enable(true);
rtc_clk_slow_freq_set(RTC_SLOW_FREQ_32K_XTAL);