Page 1 of 1
ESP32 wakes too early from sleep.
Posted: Mon Sep 19, 2022 2:21 pm
by Grandpa-G
Here is the code I used. I want to wake in 12 hours. The actual time it woke up was 11:43. The value used for sleep time was 43200000000 but the acutal amount was 42180000000 (if I did all the math correctly). How do I get the 12 hours required?
Code: Select all
uint64_t toSleep = 43200000000;
esp_sleep_enable_timer_wakeup(toSleep);
esp_deep_sleep_start();
Re: ESP32 wakes too early from sleep.
Posted: Tue Sep 20, 2022 12:48 am
by ESP_Sprite
Does your ESP32 have a 32KHz crystal connected to it? If not, this is to be expected: without a 32KHz crystal the ESP32 uses its internal oscillator which can drift a fair bit.
Re: ESP32 wakes too early from sleep.
Posted: Tue Sep 20, 2022 12:56 am
by axellin
There are some bug in esp-idf regarding wakeup time, e.g.
https://github.com/espressif/esp-idf/issues/9455
Re: ESP32 wakes too early from sleep.
Posted: Tue Sep 20, 2022 1:44 am
by Grandpa-G
A. I find it hard to believe it drifts over 20 minutes in 12 hours.
B. How do I make it work to be at least within a few minutes max?
C. How do I find out what the crystal is?
Re: ESP32 wakes too early from sleep.
Posted: Wed Sep 21, 2022 2:45 am
by ESP_Sprite
The internal oscillator is rated at a precision of about 5%, which means a drift of +/-36 minutes over a 12 hour timeframe. That means 20 minutes is in-spec. You fix this by adding a 32KHz crystal to GPIO32/33; see section 2.4.2 of the
hardware design guidelines for more information. You may also be able to work around it in software: for instance, set your wake time 40 minutes earlier than when you want to wake up, then during that 'early wakeup' get the current time (e.g. from ntp) and set deep sleep for the small period to the actual wake-up. That trick should get you an accuracy of +/- 4 minutes.
Re: ESP32 wakes too early from sleep.
Posted: Wed Sep 21, 2022 3:18 am
by axellin
ESP_Sprite wrote: ↑Wed Sep 21, 2022 2:45 am
You may also be able to work around it in software: for instance, set your wake time 40 minutes earlier than when you want to wake up, then during that 'early wakeup' get the current time (e.g. from ntp) and set deep sleep for the small period to the actual wake-up. That trick should get you an accuracy of +/- 4 minutes.
This workaround takes too much power consumption.
Re: ESP32 wakes too early from sleep.
Posted: Wed Sep 21, 2022 3:20 am
by axellin
Grandpa-G wrote: ↑Tue Sep 20, 2022 1:44 am
A. I find it hard to believe it drifts over 20 minutes in 12 hours.
B. How do I make it work to be at least within a few minutes max?
Setting CONFIG_RTC_CLK_SRC_INT_8MD256=y can improve this.
But there is another wakeup time bug as I mentioned above.
Re: ESP32 wakes too early from sleep.
Posted: Wed Sep 21, 2022 11:07 am
by Grandpa-G
axellin - This is an area I never have been in. Would you mind showing a line or two of code I would put in my Arduino sketch to effect this change. I would really appreciate your help.