LinkIt ONE RTC Example Code

Standard

When I was trying out all the different parts of the LinkIt, I wondered if there was an RTC onboard. Loovee told me there was, but that the example was not complete at this point in time.

I took this situation upon myself to make a complete example for anyone else out there who wants to use the LinkIt’s onboard RTC. In this discovery of things, I found that the RTC can be set back to 1/1/2004 if the battery and USB power supply are both disconnected. I made a loop in my code that will turn on the GPS to resync the GMT automatically in the background, the only thing that needs to happen is for the GPS to turn on. This is why in my more complex programs, I didn’t see the resetting behavior.

Here is a screenshot of what it looks like when the GPS has finally synced the time:

LDateTime working demo

And here is the code that runs the whole thing:

#include <LDateTime.h>
#include <LGPS.h>

datetimeInfo t;
unsigned int rtc;
int gps_on=0; //This is used to check if the GPS needs to be turned off

void setup() {
 Serial.begin(115200);
 }

void loop() {
 
 LDateTime.getTime(&t);
 LDateTime.getRtc(&rtc);
 
 Serial.print("Current GMT: ");
 Serial.print(t.mon);
 Serial.print("/");
 Serial.print(t.day);
 Serial.print("/");
 Serial.print(t.year);
 Serial.print(" ");
 Serial.print(t.hour);
 Serial.print(":");
 Serial.print(t.min);
 Serial.print(":");
 Serial.print(t.sec);
 
 Serial.print(" Seconds since 1/1/1970 GMT: ");
 Serial.println(rtc);
 
 //Turning on the GPS syncs up the RTC with the GPS time.
 //If the battery is pulled, the RTC goes back a couple years.

 //Turning on the GPS syncs up the RTC with the GPS time.
 //If the GPS is needed, t.year will be 2004. This will start this loop.
 if ((gps_on != 1) && (t.year < 2010))
 {
 Serial.println("Using GPS to sync GMT. Please wait...");
 LGPS.powerOn();
 gps_on = 1;
 }

 //If the GPS has synced the RTC, the year will be 2015 or greater.
 if ((gps_on == 1) && (t.year >= 2015))
 {
 LGPS.powerOff();
 Serial.println("Synced! Turning off GPS. Please wait...");
 gps_on = 0;
 }
 
 //Wait one second before getting new time.
 delay(1000);
 }

4 thoughts on “LinkIt ONE RTC Example Code

  1. Gilbs

    Hi, I tried your example but it seems my LinkIt One’s RTC won’t sync with the GPS even if its getting the right readings :(, do you have any insights on this?

    Like

  2. I haven’t tried it this yet but when the code cross posted from your personal blog to the Seeed Studio website it suffered badly. The libraries that are called did not post so there are two #includes at the top of the sketch not including anything.

    Like

Leave a reply to David Connolly Cancel reply