better rtc accuracy

  • All my RTCs gain about 0.25s/minute. mentions RTC.XTAL_32KHz, is it as simple as soldering a 32kHz rock across 2 points somewhere on the lopy4 PCB? If so where?

    If not what extra is involved in fitting a crystal to improve the timing?

  • @Gusti-Made-Arya-Wijaya As far as I recall, I used an indirect method with a sine wave generator, and oscilloscope and a HV probe with a known low capacity. So I built a RC-Circuit, with the known R and C being the addition of the probe C_probe and the board C_board. Then I could estimate via the relation amplitude at C over Frequency the value of C_board.

  • @robert-hh how to measure capacitance in board?

  • @kjm Itäs getting kind of complicated, but it is just the usual search for all the nasty little rough edges of the ESP32 and it's IDF. It seems to be full of features and full of bugs as well.
    Using an external clock vs. the builtin has different properties:

    • External: Devices like the DS1307 or DS3231 are precise, properly tested and reliable. And the clock with an own battery is truly independent from all hiccups that the ESP32, it's firmware and it's power supply may cause. The DS3231 may also be used as power fail detector and for wake-up. In addition, both could be used as 32kHz clock source or 1 Hz Interrupt source. Of the two chips, the DS3231 is truly the better one, with integrated temperature controlled crystal and a guaranteed 2ppm precision.

    • Internal: Simple (yes!) integration in the hardware and existing integration into the firmware. It can be used for a precise deepsleep period. But to add it to an existing board like the Lopy is surely a little bit of work. Usually a crystal would be directly integrated.

    Pin wise the two options are similar. Both require 2 pins. But the external I2C clock can share the pins with other I2C devices.

    Since it does not use test time, I continued the testing. A 15 hour deepsleep on the crystal ended just exactly at the expected second. The crystal frequency at that test was shown as 32769.01 Hz. This is way better than one could expect.
    On the down side, I noticed that a call to machine.reset() seem to affect some rtc clock registers. I did not expect that. Hard reset should surely affect that. At least deepsleep wake-up keeps the rtc values.

    Note: I test the rtc with deepsleep, because when power is applied, the clock source seems to be the main crystal. And - good for testing - the RC-Oscillator of my test device is off by 1 second per minute. So 10 Minutes test time is by far sufficient to tell, which clock is running.

  • @robert-hh Gets complicated doesn't it? I might try an I2C like the DS1307, about time I had a go at I2C.

  • @kjm Just another note on this after having a test run for a few days and several cycles. There is an issue with the startup time of the oscillator. According to my tests, it takes some time to get a stable amplitude. From reset, it is up to 600-1000ms second, from warm boot, it is 200-400ms. The code for starting RTC waits 80ms for the oscillator so start. Then it seems to fall back to using the RC-oscillator internally, even if the crystal clock is then running. The picture below shows the startup phase after reset. Yellow is a GPIO pulse around the call to init RTC, the green trace is the signal al P19. It is a mix of the 32kHz crystal and about 100MHz picked up from the local radio station. So the signal is not 0 left to the RTC start.
    A software workaround is to call
    rtc = machine.RTC(source=machine.RTC.XTAL_32KHZ)
    a second time after at least a second after the first time. Also, my hardware set-up may still be not the best.


  • @kjm So I repeated my tests again, and this time it worked by adding a 32768 Hz crystal. I used a crystal with a CL of 12.5 pF, such that it can deal with larger capacitances on the board. Schematic below. For a CL of 12.5 pF, the effective capacity of C1 and C2 should be 25pF (CL * 2). I tested 22 pF at the circuit, which pretty close. So the board and my set-up added about 12 pF stray capacitance.

    The frequency I could measure at the crystal was 32768.017 Hz, and an 1 hour deepsleep ended exactly after that period.


  • @kjm The Pins are P19 and P20. But just fitting a crystal there will most likely not work. You have to take into account the capacitors require for the crystal. For that, you have to know the stray capacity of the board & wiring. To make it right, the crystal and its capacitors would have to be connected directly to the ESP32 and no further wiring.
    What you could do is attaching an external 32kHz crystal based clock oscillator to p19. They provide the clock signal as stable CMOS output. Costs at Mouser about 1-2 €.

    That's the hardware side. I tried to implement that, both with an external crystal and with an external clock source. It did not work. I had the impression that it did not switch the clock source from the esp32 RC oscillator to the external clock. I did not dig into that further.

Log in to reply

Pycom on Twitter