DS1307 giving wrong date/time

Hi,

I'm a beginner and I see I'm not the only one to have understanding problems with RTC clock...

I have a RTC module DS1307 (MR005-001.2) on my Arduino Uno.
I use the Wire.h / Time.h / DS1307RTC.h libraries.

The date and time returned is completely wrong ("17:18:9 2 4 2036") ... What do I do wrong ??

Here is my partial code for setup() :

setSyncProvider(RTC.get);
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");

timeStatus() returns TimeSet when time is set and is synced and that's what i get...

Here is my partial code for loop() :

Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.print(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println();

Just as in the example ... and the date/time returned is "17:18:9 2 4 2036"

And when I run another script (opening an example file like "TimeRTC") the date/time is wrong too.

Thanks...!

Run the SetTime example in the DS1307RTC library.

Done... Date/Time is still wrong : 17:18:44 10 8 2037

Show us all of the code, because odds are you are not reading the Time Keeper register correctly.

I tried several scripts given with the Time Library without changing a line :

  • TimeRTC
  • Time RTCLog
  • TimeRTCSet

And this is my code :

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

int Ah = 16;
int Am = 6;
int Th = 17;
int Tm = 3;
// RVB veille
int Rs = 0;
int Gs = 0;
int Bs = 150;
// RVB alarme
int Ra = 150;
int Ga = 0;
int Ba = 0;
// RVB Transition
int Rt = 0;
int Gt = 255;
int Bt = 0;
// RVB courant
int Rc = 0;
int Gc = 0;
int Bc = 0;

int Actif = 1; // ACTIF ou INACTIF (réveil programmé ou non)
int Encours = 0; // Alarme en cours de sonnerie

// ON ou OFF du bouton
int State = 1;
int Prev = 0;
int Stop = 0;
int Transition = 0;

void setup(){
  Serial.begin(9600); 
  time_t t = now();
  setSyncProvider(RTC.get);
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");
  //if (Actif==1){setTime(16,05,50,31,12,14);} else{setTime(0,0,0,0,0,0);}
  pinMode(6, OUTPUT);
  pinMode(7, INPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  digitalWrite(6,HIGH);
  digitalWrite(11,HIGH);
}

void loop(){  
  int On = digitalRead(7); 
  if (Prev==1 && On==0){
    if (State==1){
       if (Encours==1){
         Encours=0;
         Stop=1;
       }
      State = 0;
    } else{State = 1;}
  }
  if (Th<=hour() && Tm<=minute()){Transition = 1;}
  if (Ah==hour() && Am==minute() && Stop==0){
    Encours = 1;
    State = 1;
  }
  if(Encours == 1){
    if (Gc<Ga){Gc++;}
    if (Gc>Ga){Gc--;}
    if (Bc<Ba){Bc++;}
    if (Bc>Ba){Bc--;}
    if (Rc<Ra){Rc++;}
    if (Rc>Ra){Rc--;}
  } else{
    if (Transition==1){
      if (Gc<Gt){Gc++;}
      if (Gc>Gt){Gc--;}
      if (Bc<Bt){Bc++;}
      if (Bc>Bt){Bc--;}
      if (Rc<Rt){Rc++;}
      if (Rc>Rt){Rc--;}
    } 
    else{
      if (Gc<Gs){Gc++;}
      if (Gc>Gs){Gc--;}
      if (Bc<Bs){Bc++;}
      if (Bc>Bs){Bc--;}
      if (Rc<Rs){Rc++;}
      if (Rc>Rs){Rc--;}
    }
    
  }
  if (State == 0){
    Gc = 0;
    Bc = 0;
    Rc = 0;
  }  
  analogWrite(8,Gc);
  analogWrite(9,Bc);
  analogWrite(10,Rc);
  //Serial.println(Prev);
  Prev = On;
  // digital clock display of the time
  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.print(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println();
  delay(50);
}

My RTC module is connected this way :

  • VCC -> 5V
  • SDA -> A4
  • SCL -> A5
  • OUT -> none
  • GND -> ground

The library example SetTime sets the RTC time to the compile time on your computer. Perhaps the time on the computer is not correct. Here is a sketch to set the RTC to a unix epoch time stamp.You can get the time stamp from this website http://www.epochconverter.com/

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

void setup() {
  
  Serial.begin(9600);
 
  Serial.println("Setting Unix time on RTC");
  Serial.println("1388534400  Jan 1, 2014 00:00:00");
  Serial.println();
  
  time_t unixTimeStamp = 1388534400; //Jan 1, 2014 00:00:00
  RTC.set(unixTimeStamp);
  
  
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
    Serial.println("Unix Time");
    Serial.println(makeTime(tm));
    Serial.println(RTC.get()); //from RTC
    
    
  
    
  } 
  
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

I've used the RTC library with cheap eBay RTC's with good results. The library is available from:

I would try one of the examples from the library and see if it works with your board.

Thanks for your replies.
I tried RTClib and the "softrtc" example file worked !
I don't know why the DS1307RTC library and other example files in RTClib didn't work, but it's ok now.

That is odd.... I've got 2x1307s, one loose and one on a breakout, both worked fine with the DS1307RTC library, but as the man said, All's Well That Ends Well.