Syncronisation Teensy 4.0 Srtc with GPS Time

HSEY

Member
I've been trying to sync the SRTC with TimeGps (Arduino) for a long time
to obtain an accurate, buffered time for further use in the program.

Unfortunately I haven't had any success so far - can someone help me?
 
A simple pattern that works well is:
1. On boot, let `TimeLib` read from the Teensy RTC:
```cpp
time_t getTeensyTime() { return Teensy3Clock.get(); }
void setup() {
setSyncProvider(getTeensyTime);
setSyncInterval(300);
}

2. When GPS has a valid UTC date/time, update both TimeLib and the RTC:
void syncFromGps(time_t gpsUtc) {
setTime(gpsUtc);
if (abs((long)(gpsUtc - Teensy3Clock.get())) >= 1) {
Teensy3Clock.set(gpsUtc);
}
}

So the RTC gives you buffered/held time between GPS updates, and GPS corrects it whenever a valid fix is available.
Important:
- Keep the RTC in UTC, not local time.
- Apply timezone/DST only when displaying time.
- Do not write the RTC continuously in the loop, only when GPS time is valid.
If you want better than 1-second NMEA accuracy, add GPS PPS and sync on that edge.
 
Hello femme,

Unfortunately, I don't speak English very well and have to resort to Google - please excuse me!

Thank you for your contribution.

I had to make a few adjustments:

-time_t getTeensyTime() { return Teensy3Clock.get(); } I call in the program to keep the library in the original

-void syncFromGps(time_t gpsUtc) doesn't work with my Arduino IDE, so I call (time_t gpsUtc) in subprogram

So the program works perfectly.

I probably wouldn't have been able to do it without your help - thank you very much for that!
 
One potential thing to look out for:
GPS doesn't use UTC time, it uses GPS time. These are currently 18 seconds different, that may change in the future if they decide to add/remove a leap second to keep UTC in line with the earths rotation.

The GPS receiver in theory knows about this difference and corrects for it so you should normally get the correct UTC time from the GPS. However the GPS satellites only broadcast what the current correction is once every 12 minutes. This means that depending on the GPS receiver used and it's power up defaults you can sometimes get a sudden jump of a couple of seconds in the time output. This can happen up to 15 minutes after switching on.
Most receivers default to the value that was correct when their firmware was written and will store the last know value if they have battery backed up memory so the jump is typically only a couple of seconds if anything, it's not going to be the full 18 seconds.

Most of the time this doesn't matter but it is something to be aware of for applications where sub-second timing synchronization is important.
 
Thanks AndyA. Now I properly understand why I added the hack in my code to cater for an "unexplained" jump in the timestamp all those years ago.
 
Back
Top