
Timezone: Europe/London
In the previous article I mentioned the RTC is not particularly accurate during deepsleep. The specification does mention this, quoting 5% accuracy, however it is convenient to know the expected gain or loss over a set time period.
In tests, I have found the RTC gains between 33 to 38 seconds an hour. It seems to be different for each module, but it certainly is not insignificant.
In this article I will explain a way to test your individual RTC and maintain a better level of accuracy.
I have tested 4 different ESP32 feather huzzar units over a number of days. I found the average gain was fairly consistent for each unit, though it differed from unit to unit.
Running cycles of 8 deepsleeps, each of 1 hour duration, the first cycle was always about 10 seconds longer.
I suspect this is to do with the internal temperature of the chip. It is documented
that the crystal oscillator running the clock is affected by temperature. After emerging from deepsleep, the temperature will be lower, and in my tests, after the first sleep it was never awake for more than a few seconds.
Subsequent cycles were fairly consistent within 5 seconds of each other.

If your project requires very accurate timing and very low power consumption, then this can be a problem.
For example, in my project I need several ESPs to take measurements at exactly the same time once a day. Because they run on batteries, I need them to be in deepsleep hibernation for most of that time.
I can (and do) get the correct time from GPS as soon as they wake, and then wait until the measurement target time to take the measurements. However, if they wake up 20 minutes early, thats 18 minutes on full power I would like to avoid.
Luckily, the RTC gains time. If it were the other way around they would wake after the measurement time unless I compensated.
Knowing how your individual RTC performs is useful for this reason
Below is the code for a testing program to check your own ESP
Once you know the time gained during deepsleep for your particular ESP board, it is easy to adjust your code to allow for this in your programmes to maintain greater accuracy and maximum power saving.
If you uncomment the line logdata.write('Expect true epoch on wake to be ' + str((epoch + snooze + 1)-offset) + '\n') and set the variable offset to your expected gain for the test period, you can fine tune your results.
Don't expect accuracy within 2 or 3 seconds, as there factors I have either assumed or not taken into account here. Though I have allowed for the time it takes to connect to the internet and set the time and 1 second for waking from hibernation, I have not allowed for the time taken to interpret and run the code. Also, seconds appear as a rounded figure, so each reading could be 0.9 seconds out.
IDE editors for programming ESP boards.
Micropython editors
Micropython functionality and libraries.
Micropython.org
Find the uPython firmware for your board.
uPython firmware for development boards.
ESP32 uPython modules and function examples.
Quick reference for the ESP32
Scanning and connecting to multiple SSIDs
Using an SD card reader breakout board
RTC Tuning
OTA Updating running software without interuption
Future articles
Communication between ESPs
Using an Adafruit GPS module
Hassle free web communication with urequests.py.