Need help to understand DS1307RTC.h library and set time

Hello!

I`m new to this and try to understand how to set my RTC, DS3231. I use the library DS1307RTC.h in order to write the RTC time, together with GPS that give me GPS UTC time, in a SD card datalogger. So, if GPS time is not available, RTC time will be available. It works just fine. However, I would like to be able to set the RTC time, by GPS time (if valid) or simply, press a key on serial input (sets the RTC clock after laptop system time.

This is the library: https://github.com/PaulStoffregen/DS1307RTC

What I do not understand, it the (RTC.write(tm) part.

If try to use (RTC.write(day,month,year) as integers or try my strings from tinygps++:

  String Tid=String(gps.time.hour())+(":")+(gps.time.minute())+(":")+(gps.time.second());
  String Dato=String(gps.date.day())+("/")+(gps.date.month())+("/")+(gps.date.year());

The RTC it not set. I dont understand what "tm" contains in order to write to RTC. I try to use RTC.set, but no luck.

This is what the library owner write:


RTC.set(t);

Sets the date & time, using a 32 bit "time_t" number. Returns true for success, or false if any error occurs.

RTC.read(tm);

Read the current date & time as TimeElements variable. See the Time library for TimeElements details. Returns true on success, or false if the time could not be read.
https://www.pjrc.com/teensy/td_libs_DS1307RTC.html


Now, I need to compile and load the time set example, evry time I want to set the RTC time.

Anyone that can help me out ? :slight_smile:

This is the DS1307RTC.h code for set the RTC. I need to compile and load:

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

Please post your sketch, not the library example sketch.

OK, my code is simple, just change tm to: (RTC.write(1,1,1,1,1,1))

As I would like to set the RTC to 1:1:1 and date 1/1/1 (example) and get this error:

C:\Users\alfa\Documents\Arduino\libraries\DS1307RTC/DS1307RTC.h:20:17: note: candidate expects 1 argument, 6 provided

exit status 1
no matching function for call to 'DS1307RTC::write(int, int, int, int, int, int)'

So what do "tm" realy contain? a 32 bit string of some sort?

typedef struct  { 
  uint8_t Second; 
  uint8_t Minute; 
  uint8_t Hour; 
  uint8_t Wday;   // day of week, sunday is day 1
  uint8_t Day;
  uint8_t Month; 
  uint8_t Year;   // offset from 1970; 
} 	tmElements_t, TimeElements, *tmElementsPtr_t;
1 Like

Be sure to let the GPS run for a while, 30mins+ maybe so that it is actually outputting UTC.

At startup and first fix the time put out by a GPS can be a few seconds out.

You might want to investigate how to poll the GPS to see if the seconds value is correct and inline with UTC.

1 Like

Yes, I use layered time sources. So when I take time from GPS or NTP, I run a sanity check against the RTC before adjusting it. I see whether it's at least within a few seconds...

Thank you! So it seems that the liberary use seven integers to set the time?

So, since my goal is to set the RTC with provided integers, i wrote this:

   if (RTC.write(1,1,1,1,1,1,1)) {
      config = true;

But I got this error message:

C:\Users\alfa\Documents\Arduino\libraries\DS1307RTC/DS1307RTC.h:20:17: note: candidate expects 1 argument, 7 provided

exit status 1
no matching function for call to 'DS1307RTC::write(int, int, int, int, int, int, int)'

So my question is, is it possible to set this RTC with provided library, in a simple way using integers? Provided by simply enter seven number like i did above or get them from f.ex a string of GPS data like this:

  String Time=String(gps.time.hour())+(":")+(gps.time.minute())+(":")+(gps.time.second());
  String Date=String(gps.date.day())+("/")+(gps.date.month())+("/")+(gps.date.year());

My programming knowledge is limited so I can not see this clearly.

Possibly, you haven't read the time library documentation enough. But, on a general point that you may not be aware, you need to pass your values in a struct, like the one I posted. So Google C structs as well.

The time library example sketches show you how to use those structs.

1 Like