DS3231 RTC - calibrate Aging register with GPS PPS - Page 1
Forum section: electronics / projects

author: Peabody
author_rank: Super Contributor
timestamp: March 23, 2024, 03:53:12 am
content: 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-GPSThe 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.

author: mino-fm
author_rank: Regular Contributor
timestamp: March 23, 2024, 10:59:05 am
content: 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.

author: Peabody
author_rank: Super Contributor
timestamp: March 23, 2024, 02:26:08 pm
content: 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.

author: BareMetalBard
author_rank: Newbie
timestamp: March 24, 2024, 06:27:58 am
content: 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.

author: Peabody
author_rank: Super Contributor
timestamp: March 24, 2024, 09:39:05 pm
content: 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.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: March 24, 2024, 10:34:42 pm
content: Quote from: mino-fm on March 23, 2024, 10:59:05 amThat'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 DS3231Mis different again.Others change the division ratios by pulse insert/swallow.

author: iMo
author_rank: Super Contributor
timestamp: March 25, 2024, 07:52:54 am
content: 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..

author: mino-fm
author_rank: Regular Contributor
timestamp: March 25, 2024, 08:54:16 am
content: Quote from: iMo on March 25, 2024, 07:52:54 amBut aging-reg can not be tuned finer than 100 ns.QuoteThere 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.

author: iMo
author_rank: Super Contributor
timestamp: March 25, 2024, 09:28:39 am
content: Quote from: mino-fm on March 25, 2024, 08:54:16 amYou 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..

author: Peabody
author_rank: Super Contributor
timestamp: March 25, 2024, 02:27:43 pm
content: 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.

author: tanveerriaz
author_rank: Contributor
timestamp: March 25, 2024, 06:45:22 pm
content: more infohttp://radiopench.blog96.fc2.com/blog-entry-943.htmlhttp://radiopench.blog96.fc2.com/blog-entry-929.htmlhttp://radiopench.blog96.fc2.com/blog-entry-946.html

author: PCB.Wiz
author_rank: Super Contributor
timestamp: March 25, 2024, 06:59:47 pm
content: Quote from: Peabody on March 25, 2024, 02:27:43 pmI 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.

author: mino-fm
author_rank: Regular Contributor
timestamp: March 26, 2024, 09:42:57 am
content: Quote from: PCB.Wiz on March 25, 2024, 06:59:47 pmOh, 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.

author: Peabody
author_rank: Super Contributor
timestamp: March 26, 2024, 01:57:40 pm
content: 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.

author: Peabody
author_rank: Super Contributor
timestamp: March 30, 2024, 05:13:26 pm
content: 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.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: March 31, 2024, 04:44:40 am
content: Quote from: Peabody on March 30, 2024, 05:13:26 pmGoogle finds thisArduino clock frequency accuracyhttps://jorisvr.nl/article/arduino-frequency#:~:text=Every%20Arduino%20has%20an%20on,8%20MHz%20or%2016%20MHz.GivesStandard deviation Xtal = 43.75ppb Ceramic resonator 2.75ppmTempco Xtal = 60.62ppb / °C Ceramic resonator 8.69ppm / °Cso 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.

author: Peabody
author_rank: Super Contributor
timestamp: March 31, 2024, 02:46:19 pm
content: 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.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: April 01, 2024, 03:46:48 am
content: Quote from: Peabody on March 31, 2024, 02:46:19 pmBy 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.

author: Peabody
author_rank: Super Contributor
timestamp: April 01, 2024, 04:45:49 am
content: 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.