Author Topic: DS3231 RTC - calibrate Aging register with GPS PPS  (Read 5702 times)

0 Members and 1 Guest are viewing this topic.

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
DS3231 RTC - calibrate Aging register with GPS PPS
« on: March 23, 2024, 03:53:12 am »
In case it might be of use to remote data logger types, here's my project to use an Arduino and a cheap GPS module to set  the DS3231's Aging register to the optimal value.  From the factory its value is zero, but the optimums on my three RTC are +5, -23 and -45.  The permitted range is -128 through 127.

https://github.com/gbhug5a/DS3231-Aging-GPS

The idea is to adjust Aging until the 1Hz squarewave output of the RTC is the same frequency as the 1Hz PPS (pulse per second) output of the GPS, and in effect calibrate the RTC's timekeeping to probably the most accurate time source we have.  Then on its own the RTC should stay within a few seconds a year.

The two signals are compared every five minutes to find whether the number of processor cycles between them has remained constant.  If not, Aging is adjusted so it will be.  So you get the answer within an hour or less instead of checking the time day after day.

There's a PDF in the Github repo that explains everything.    No libraries were harmed in writing this code.

 
The following users thanked this post: bingo600, floobydust, Jacon

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 151
  • Country: de
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #1 on: March 23, 2024, 10:59:05 am »
Didn't you made it a little bit too complicated?
Aging-reg of RTC offers 0.1 ppm resolution and jitter of any 1 PPS pulse is < 0.1 ppm. So you could capture one period of RTC and one period of GPS and compare both values. This would last only < 5 s.
Because of temperature, supply voltage and aging RTC will never be better than 0.1 ppm. You can't change this.

If GPS is constantly running you could use GPS-output directly.
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #2 on: March 23, 2024, 02:26:08 pm »
I didn't test how little time I could capture and still get good results.  When I actually print out the individual results, they vary a good bit.  And even the 5-minute results have some jitter.  Also, since the RTC is making temperature adjustments on its own, it just seemed best to let it run a while to let everything settle down.  But it's possible it could be done just as well a lot faster.
 

Offline BareMetalBard

  • Newbie
  • Posts: 6
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #3 on: March 24, 2024, 06:27:58 am »
Nice project. I'm trying to get into embedded software design and one of my first projects was making an LCD clock that grabbed time from a DS3231. I never actually looked if it was an SN or an M. It's whichever one comes with the Elegoo Arduino kit.

It's pretty cool that you can buy a GPS module for so cheap. I had no idea. I'm definitely going to buy it to play around with. Maybe I'll integrate it into my LCD clock project haha.
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #4 on: March 24, 2024, 09:39:05 pm »
Yes, it's amazing the sophisticated circuitry you can get for $13.  The only downside of GPS modules is that they can draw a good bit more current than may be appropriate for a battery-powered circuit.  I think they are usually a few milliamps or tens of milliamps, in contrast to the DS3231 RTC, which can run on a few microamps in a pinch.
 

Online PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1897
  • Country: au
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #5 on: March 24, 2024, 10:34:42 pm »
Didn't you made it a little bit too complicated?
Aging-reg of RTC offers 0.1 ppm resolution and jitter of any 1 PPS pulse is < 0.1 ppm. So you could capture one period of RTC and one period of GPS and compare both values. This would last only < 5 s.

That's true from a resolution viewpoint, with a high enough sampling clock, but RTCs have varying correction schemes that often run slower than 1pps.
I see the DS3231 has a 64 second update on sampling Temp Correct +Age. It also says the temp stability degrades as aging register moves away from 0.

The DS3231M is different again.
Others change the division ratios by pulse insert/swallow.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5236
  • Country: bj
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #6 on: March 25, 2024, 07:52:54 am »
The 1 pps pulse off a typical GPS module has got a pretty large jitter, like 20-60ns.
You have to average it, ie. discipline an OCXO with it..


Readers discretion is advised..
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 151
  • Country: de
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #7 on: March 25, 2024, 08:54:16 am »
The 1 pps pulse off a typical GPS module has got a pretty large jitter, like 20-60ns.
But aging-reg can not be tuned finer than 100 ns.

Quote
You have to average it, ie. discipline an OCXO with it..
There is no need to average something. XO of RTC isn't a high precision oscillator. Optimally tuned it will drift as soon as you open a door or a window.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 5236
  • Country: bj
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #8 on: March 25, 2024, 09:28:39 am »
Quote
You have to average it, ie. discipline an OCXO with it..
There is no need to average something. XO of RTC isn't a high precision oscillator. Optimally tuned it will drift as soon as you open a door or a window.
You have to average the 1pps off a typical GPS module, ie. discipline an OCXO with it..

The ds3231SN is pretty stable, one of mine runs since 9/2012 and made +190secs as of today (still the same cr2032 battery).. Perhaps it would be enough to read the 3231 once a month (for example) and simply adjust it based on the actual NMEA message..
« Last Edit: March 25, 2024, 09:44:06 am by iMo »
Readers discretion is advised..
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #9 on: March 25, 2024, 02:27:43 pm »
There is some short-term oscillation fuzziness in all three devices - the PPS, the RTC, and even the Nano.  I do the comparison every five minutes, and average the last 16 readings to get a number.  If I print out those 16 values, I see a fair amount of jitter, so I think averaging makes sense.  And if left to run for a couple hours, it tends to settle down and switch between two values.  It's never going to be exactly perfect, so one value will be a little fast, and the next a little slow, and you have to pick the one that looks best.

The point of this was to not depend on zero being the right Aging value, which is how it comes from the factory.  Zero will work pretty well on an SN, but Ms can be way off (my two Ms optimize at -23 and -45).  So why not get as close as you can.  The GPS just lets you find the best number pretty quickly.

But I think it's true that termperature and supply voltage can affect timekeeping significantly.  The DS3231 has a built-in temperature adjustment, but voltage is another thing.  In running the Aging code, the closer you can get to the temperature and supply voltage conditions it will experience in the field, the better the answer will be.
« Last Edit: March 25, 2024, 02:29:27 pm by Peabody »
 


Online PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1897
  • Country: au
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #11 on: March 25, 2024, 06:59:47 pm »
There is some short-term oscillation fuzziness in all three devices - the PPS, the RTC, and even the Nano.
I think the nano has a 16 MHz ceramic resonator, so ideally you need to overlap readings as much as possible, to remove resonator effects.
Averaging will certainly help.
 

Offline mino-fm

  • Regular Contributor
  • *
  • Posts: 151
  • Country: de
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #12 on: March 26, 2024, 09:42:57 am »
I think the nano has a 16 MHz ceramic resonator, so ideally you need to overlap readings as much as possible, to remove resonator effects.
Averaging will certainly help.

Oh, I thought current Arduino NANO boards would use a true XO. Ceramic resonator is not bad but awful! It drifts quite fast.
Averaging would work if signals are noisy. But it can't help if frequencies drifting fast and a lot.

The first time I used Arduino UNO R3 for time measurements, I had to remove ceramic resonator and used XTAL2 output of U3 (ATmega16U2) to drive XTAL1 input of ATmega328 instead. That's the only way to get reliable and stable results.
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #13 on: March 26, 2024, 01:57:40 pm »
I'd guess a genuine Arduino would use a crystal, but my Nanos are cheap clones, and they probably have resonators if resonators are cheaper.  I'm not sure I can tell by looking, but there are no capacitors.
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #14 on: March 30, 2024, 05:13:26 pm »
It seems that Timer1 can be externally clocked at something less than F_CPU/2.5.  So if I wanted to take the resonator jitter/variablility out, I could feed a 4MHz crystal oscillator into Timer1.  That would be a lot cheaper and easier than trying to find a Nano with a crystal.

It would be interesting to see if the crystal makes any difference.
 

Online PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1897
  • Country: au
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #15 on: March 31, 2024, 04:44:40 am »
It seems that Timer1 can be externally clocked at something less than F_CPU/2.5.  So if I wanted to take the resonator jitter/variablility out, I could feed a 4MHz crystal oscillator into Timer1.  That would be a lot cheaper and easier than trying to find a Nano with a crystal.

It would be interesting to see if the crystal makes any difference.

Google finds this
Arduino clock frequency accuracy https://jorisvr.nl/article/arduino-frequency#:~:text=Every%20Arduino%20has%20an%20on,8%20MHz%20or%2016%20MHz.

Gives
Standard deviation  Xtal = 43.75ppb           Ceramic resonator 2.75ppm
Tempco                  Xtal  = 60.62ppb  / °C  Ceramic resonator 8.69ppm / °C

so if you are chasing comparative measurements in the 0.1ppb ballpark, a XTAL is looking ok, but  the ceramic resonator noise/drift/jitter will be significant.
If you can synchronize the capture windows so they overlap 'most of the time' you can cancel out some of the local clock variations.


« Last Edit: March 31, 2024, 05:00:29 am by PCB.Wiz »
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #16 on: March 31, 2024, 02:46:19 pm »
Thanks very much for that link.  The method I used was to start timer1 from zero when the RTC 1Hz output goes low, and capture the timer count when the PPS goes high, which is about 1/2 second later.  But then I compare the result to what I got five minutes ago.  I've found that things are relatively stable after everything has been running for half an hour when the temperature of all the parts has settled down.   I expect it to shift back and forth between two Aging settings because it's never going to be exactly right, so the closest I can get will either be a little slow or a little fast.  But even after an hour, it may wander by one Aging tick more than that temporarily.

I haven't noticed that my SN example, which has a crystal, wanders any less than the two M examples, which use MEMS.  So maybe the Nano is the primary source of the wandering.  Anyway, I think I will order a 4 MHz crystal and see if that makes any difference.  Or, actually, I have a couple of ESP8266 modules.  I need to check if they might have crystals.

It turns out I have a 4 MHz crystal in my junque box.  But no matching little caps. Not sure what I can do with that.  Maybe I can rig up some kind of Colpitts thing.
 

Online PCB.Wiz

  • Super Contributor
  • ***
  • Posts: 1897
  • Country: au
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #17 on: April 01, 2024, 03:46:48 am »
Thanks very much for that link.  The method I used was to start timer1 from zero when the RTC 1Hz output goes low, and capture the timer count when the PPS goes high, which is about 1/2 second later.  But then I compare the result to what I got five minutes ago. 
By doing that, you have a 'gain' of appx 300, so a 1us per second or 1ppm phase change, measures as 300us after 5 minutes.
If you want to resolve 0.1ppm, that's now 30us in 500ms or about 60ppm in 500ms, and a ceramic resonator might be able to do that.
 

Offline PeabodyTopic starter

  • Super Contributor
  • ***
  • Posts: 2156
  • Country: us
Re: DS3231 RTC - calibrate Aging register with GPS PPS
« Reply #18 on: April 01, 2024, 04:45:49 am »
I don't quite understand your numbers, but over 5 minutes, its 264 timer clocks per Aging step for the SN, and 576 for the M.  Those are the divisors  I use to get to the correct Aging value.  The timer has no prescaler or divisor, so it's running at 16MHz. So that's like 16.5us for the SN, and roughly double that for the M.

The Nano resonator seems to work well enough after some settling time, but I'd like to see if using a crystal works noticeably better or faster.
 
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf