DS1307 not updating time

Hello guys,
i'm trying to build a quick and light sketch to get time from a DS1307 RTC.
I am currently able to write date/time to the device, but time is not "updated" into the RTC. Obviously, the CH bit is not set. I check that buy querying all register independently. But still i'm not able to get an updated time.
Can someone point me where the issue is ?

Here is the code

#include <Wire.h>

struct mytime {
    byte hour;
    byte min ;
    byte sec;
    byte day;
    byte month;
    byte year;
};

struct mytime this_time;

int DS_addr = 0x68;
void setup() {
  Wire.begin();
  //Wire.setClock(100000);
  Serial.begin(38400);
  Wire.beginTransmission(DS_addr); 
  Wire.write(0);          
  Wire.write(0b00000000); //set Clock Halt bit to 0 (enable clock) sec
  Wire.write(0b00111001); // min
  Wire.write(0b00100000);// hour
  Wire.write(0b00100001);// day 21
  Wire.write(0b00001001);// month 0:9
  Wire.write(0b00100001);// year 2:1
  
  Wire.endTransmission();
}

void loop() {
  getTime(this_time);
  Serial.print("hour ");
  Serial.println(this_time.hour);

  Serial.print("min ");
  Serial.println(this_time.min);

  Serial.print("sec ");
  Serial.println(this_time.sec);
  Serial.println("");
  delay(1000);

}


void getTime(struct mytime _time ){
    Serial.print("write compilation");
    Serial.print(__DATE__);
    Serial.println(__TIME__);
    Wire.beginTransmission(DS_addr);
    Wire.write(0);
    Wire.endTransmission();
    Wire.requestFrom(DS_addr,6);
    Serial.print("testsec: ");
     Serial.println(Wire.read());
    //this_time.sec =Wire.read();
    this_time.min = bcdToDec(Wire.read());
    this_time.hour = bcdToDec(Wire.read());
    this_time.day = bcdToDec(Wire.read());
    this_time.month = bcdToDec(Wire.read());
    this_time.year = bcdToDec(Wire.read());
}

byte bcdToDec(byte val)
{
  return( (val/16*10) + (val%16) );

Return serial debug

write compilationSep 21 202120:40:49
testsec: 0
hour 20
min 8
sec 0

write compilationSep 21 202120:40:49
testsec: 0
hour 20
min 8
sec 0

write compilationSep 21 202120:40:49
testsec: 0
hour 20
min 8
sec 0

I can not confirm your issue/ When I run your sketch I see the seconds happily ticking away.

12:50:38.262 -> write compilationSep 21 202112:46:06
12:50:38.262 -> testsec: 0
12:50:38.262 -> hour 20
12:50:38.262 -> min 39
12:50:38.262 -> sec 0
12:50:38.262 -> 
12:50:39.246 -> write compilationSep 21 202112:46:06
12:50:39.246 -> testsec: 1
12:50:39.246 -> hour 20
12:50:39.246 -> min 39
12:50:39.246 -> sec 0
12:50:39.246 -> 
12:50:40.277 -> write compilationSep 21 202112:46:06
12:50:40.277 -> testsec: 2
12:50:40.277 -> hour 20
12:50:40.277 -> min 39
12:50:40.277 -> sec 0
12:50:40.277 -> 
12:50:41.262 -> write compilationSep 21 202112:46:06
12:50:41.262 -> testsec: 3
12:50:41.262 -> hour 20
12:50:41.262 -> min 39
12:50:41.262 -> sec 0
12:50:41.262 -> 
12:50:42.293 -> write compilationSep 21 202112:46:06
12:50:42.293 -> testsec: 4
12:50:42.293 -> hour 20
12:50:42.293 -> min 39
12:50:42.293 -> sec 0
12:50:42.293 -> 
12:50:43.277 -> write compilationSep 21 202112:46:06
12:50:43.277 -> testsec: 5

If the clock halt bit were not set to 0, you would not be reading 0 for test seconds but rather 128, so I don't that that the oscillator not running due to clock halt bit is your issue.

Try running a basic library example for the ds1307.

You might try this

bildr ยป Do You Have The Time? DS1307 RT Clock + Arduino

1. You may try the following sketch:

#include "RTClib.h" 
byte prSec = 0;

RTC_DS1307 rtc;  //RTC_DS3231 rtc; //user data type RTC_DS3231    variable rtc or object
DateTime nowDT;
//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup ()
{
  Serial.begin(115200);
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  //rtc.adjust(DateTime(2021, 12, 31, 11, 59, 57));//set date-time manualy:yr,mo,dy,hr,mn,sec
}

void loop ()
{
  
  showTime();
  timeDelay();//1-sec interval of time display; this time delay comes form DS3231 itself
}

//===================================================
void showTime()
{
  nowDT = rtc.now();  //nowDT hold Date and Time
  //---------------------------
  byte myHour = nowDT.hour();   //myHour holds Hour of time of daya
  Serial.print(myHour); //12:58:57     12:4:56
  Serial.print(':');
  //-------------------------------------------------
  byte myMin = nowDT.minute(); //to show leading zero of minute
  if (myMin < 10)
  {
    Serial.print('0');
  }
  Serial.print(nowDT.minute()); Serial.print(':');
  //--------------------------------------------------
  byte mySec = nowDT.second();
  if (mySec < 10)
  {
    Serial.print('0');
  }
  Serial.println(mySec);//(nowDT.second(), DEC);
}
//---------------------------------------------
void timeDelay()
{
  prSec = bcdSecond();   //current second of RTC
  while (bcdSecond()== prSec)// != 1 )
  {
    ;
  }
  prSec = bcdSecond(); //delay(1000);
}

byte bcdSecond()
{
  nowDT = rtc.now();
  if (nowDT.second() == 0 )
  {
    return 0;
  }
  else
  {
    return nowDT.second();
  }
}

2. You may also try the following sketch.

#include <Wire.h>
#define dsAddress 0x68

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  Wire.beginTransmission(dsAddress);  //24 Hours Clock Set
  Wire.write(0x00);       //address of Second Register is put into Address Counter of RTC
  Wire.write(0x12);       //data for Sec Register-12
  Wire.write(0x58);       //data for Min Register (58)
  Wire.write(0x23);       //data for Hours Register (23): 24 Hours Time: 23:47:12
  Wire.endTransmission();
}

void loop()
{
  showTime();
  delay(1000);   //just for the Serial Monitor
}

void showTime()
{
  Wire.beginTransmission(dsAddress);
  Wire.write(0x00);       //address of Second Register is put into Address Counter of RTC
  Wire.endTransmission();

  Wire.requestFrom(dsAddress, 3);
  byte bcdSeconds = Wire.read();
  bcdSeconds = bcdSeconds & 0b01111111;
  byte bcdMinutes = Wire.read();
  byte bcdHours = Wire.read();

  Serial.print((bcdHours>>4)& 0b00000011); Serial.print(bcdHours&0b00001111);Serial.print(':');
  Serial.print(bcdMinutes>>4); Serial.print(bcdMinutes & 0b00001111);Serial.print(':');
  Serial.print(bcdSeconds>>4);Serial.println(bcdSeconds & 0b00001111);
}

I try to run the sketch with RTClib.h library. It is the same.
Time is not ticking up...
So i think my DS1307 is dead.
Thanks for your help guys.

If you have DS3231 RTC Module, then try to operate that.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.