Use of RTC with XTAL and deepsleep

  • I have a few questions regarding use of the RTC (real time clock).

    1. Is there a difference in accuracy and power consumption when using the RTC with XTAL_32KHZ vs. INTERNAL_RC
    2. Will the RTC need to be reinitalized after a deepsleep to make it use XTAL_32KHZ or INTERNAL_RC
    3. Will deep sleep have any affect on the accuracy of the RTC?

  • @robert-hh
    I have the same impression. I had a 15 minutes deep sleep cycle running the night and the time was ~4 minutes off in the morning.

  • @t0000899 In my test I also had the impression that the external XTAL is not used. I connected one, made it working. At least I believe so. Getting a crystal to oscillate is not easy. It is not just soldering it to the pins. But the time lapse compared to an external RTC clock was still the same. Even injecting a 32kHz signal with an external generator did not change the figure. I have to investigate further.

  • Hi @jmarcelino ,
    Is it still true that one would need a special firmware to use external RTC crystal in machine.deepsleep?

    If so, could it be possible to select a firmware with a 32768 Hz external cystal enabled in "Advanced Settings" in Firmware Update Tool?
    Or update deep sleep documentation to remind that external crystal is not used to count machine.deepsleep time?

  • Hi,

    I'm trying to setup the RTC to use the external XTAL while in deep-sleep. I create an RTC and set its source to XTAL_32KHZ right before entreing the deep-sleep mode. Everything seems to work fine, but accuracy is still a mess (2 seconds diff over a 10-min sleep time), exactly the same as when using the internal RC oscillator.

    It looks like the ESP32 is still using the internal clock, because if I remove the crystal, it does the same.

    My test code:

    import machine
    import time
    rtc = machine.RTC()

    Any thoughts on this? What am I missing?


  • Hi @jardar

    You will get better accuracy - including timing deepsleep wake, especially if you're having long sleep times - using an external crystal at the expense of slightly higher power consumption. I don't however have quantitative data on this to share and it'll also vary depending on the crystal used.

    On the normal firmware you'll need to tell RTC to enable XTAL_32KHZ every time, because the pycom-esp-idf is compiled with


    so this reverts to the internal clock every deepsleep cycle.

    If you're using an external crystal I'd recommend compiling a custom firmware with the external crystal enabled: Component config->ESP32-specific->"Timers used for gettimeofday" and "RTC clock source"

Pycom on Twitter