Skip to content

ESP32 clock runs fast #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dajtxx opened this issue Feb 8, 2023 · 3 comments
Closed

ESP32 clock runs fast #23

dajtxx opened this issue Feb 8, 2023 · 3 comments
Labels
bug Something isn't working firmware Issues and enhancements related to firmware development.

Comments

@dajtxx
Copy link
Contributor

dajtxx commented Feb 8, 2023

The ESP32s clock is very inaccurate (eg 10 seconds in 40 minutes in one test) due to the default 150 kHz source. There is an option to use an internal 8.5 MHz source which requires recompiling the Arduino/ESP32 core. This is meant to be more accurate so worth a try.

The simplest fix we can put in is to connect to the internet and do a time sync using NTP or the mobile network every hour to keep this drift in check.

More complicated fixes are to either include a 32 kHz crystal on the board which takes 2 ESP32 pins, or to include a separate RTC and crystal which will need to be communicated with via I2C or SPI.

@dajtxx dajtxx added bug Something isn't working firmware Issues and enhancements related to firmware development. labels Feb 8, 2023
@HarveyBates
Copy link
Member

@dajtxx
The board has an onboard 32 kHz crystal already.
It connected to pins 8 and 9 on the schematic (https://github.com/DPIclimate/enviro-node/blob/master/electronics/motherboard/enviro-node/schematics/microcontroller.pdf).

@dajtxx
Copy link
Contributor Author

dajtxx commented Feb 12, 2023

So it does! Well done :)

Now to recompile the ESP32/Arduino core. Reference:

espressif/esp32-arduino-lib-builder#102

@dajtxx
Copy link
Contributor Author

dajtxx commented Mar 29, 2023

See commit f27495d

The code to set the RTC source was copied from the Arduino core and modified to only use the external crystal. Given it's only feeding the RTC is should be safe to call from setup().

A clock skew adjustment parameter has also been added to the config class, and some code added to where the time is taken from the modem to try and keep the RTC in line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working firmware Issues and enhancements related to firmware development.
Projects
None yet
Development

No branches or pull requests

2 participants