DS1307 using GMT

I'm trying to implement a data logger with Date and Time needed using a DS1307. I want to use the PC time as my time source. So far it all works except the hours is 5 hours off.

Greenwich Mean Time (GMT) it looks like its following. My windows PC is correct.

Any ideas?

These are very poor choice for an RTC.

Use the DS3231 (~$2.00 with battery), most of the ones used here are ± a few seconds per 6 months.

ok thanks - I used the one the kit had. I actually have the DS3231 ordered already from Amazon. Arrives tomorrow.

Amazon sells them; eBay is less expensive.

Post your actual code.

(remember click < code > and post code)

How did you set the time on the DS1307? If you have it set to match your computer time, then it may be how you are reading the log file.

I used the example program.

#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;
}

Can you set the RTC manually? Your problem is that the time on your PC is altered by the time zone you are in. Knowing this, you can calculate what UTC is: manually set your clock once and as long as the backup battery lives, the RTC will maintain that time.

If you know the system clock is a set number of hours different from the compile time, you can modify the example sketch to adjust for this time. See the line I've added to set the clock 5 hours later than the compile time.


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
    breakTime(makeTime(tm) + 60 * 60 * 5, tm); //add 5 hours to compile time
    if (RTC.write(tm)) {
      config = true;
    }
  }

try this code.

In this line set you GMT diference. " int timeGMT = 5;"

#include <RTClib.h>
#include <Wire.h>
RTC_DS3231 rtc;
char t[32];
int timeGMT = 5;
int time_Mod = 60 * 60 * timeGMT;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  DateTime now = DateTime(F(__DATE__), F(__TIME__));
  DateTime GMT = now.unixtime() - time_Mod;
  rtc.adjust(DateTime(GMT.unixtime()));
}
void loop()
{
  DateTime now = rtc.now();
  sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
  Serial.print(F("Date/Time: "));
  Serial.println(t);
  delay(1000);
}

worked - thanks much...