ESP32 wakes too early from sleep.

Grandpa-G
Posts: 4
Joined: Mon Sep 19, 2022 2:16 pm

ESP32 wakes too early from sleep.

Postby Grandpa-G » Mon Sep 19, 2022 2:21 pm

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();

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 wakes too early from sleep.

Postby ESP_Sprite » Tue Sep 20, 2022 12:48 am

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.

axellin
Posts: 199
Joined: Mon Sep 17, 2018 9:09 am

Re: ESP32 wakes too early from sleep.

Postby axellin » Tue Sep 20, 2022 12:56 am

There are some bug in esp-idf regarding wakeup time, e.g. https://github.com/espressif/esp-idf/issues/9455

Grandpa-G
Posts: 4
Joined: Mon Sep 19, 2022 2:16 pm

Re: ESP32 wakes too early from sleep.

Postby Grandpa-G » 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?
C. How do I find out what the crystal is?

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 wakes too early from sleep.

Postby ESP_Sprite » Wed Sep 21, 2022 2:45 am

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.

axellin
Posts: 199
Joined: Mon Sep 17, 2018 9:09 am

Re: ESP32 wakes too early from sleep.

Postby axellin » Wed Sep 21, 2022 3:18 am

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.

axellin
Posts: 199
Joined: Mon Sep 17, 2018 9:09 am

Re: ESP32 wakes too early from sleep.

Postby axellin » Wed Sep 21, 2022 3:20 am

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.

Grandpa-G
Posts: 4
Joined: Mon Sep 19, 2022 2:16 pm

Re: ESP32 wakes too early from sleep.

Postby Grandpa-G » Wed Sep 21, 2022 11:07 am

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.

Who is online

Users browsing this forum: No registered users and 68 guests