RTC works only once with RTClib

I bought IN1307N and interfaced to Arduino Uno.

  1. Initially I initialized the chip with rtc.adjust(DateTime(__DATE__, __TIME__));. I uploaded image to Arduino immediately after compiling. I got the correct time printed out via UART.

  2. Then I commented out the datetime initialization and uploaded the new image. Still got the correct prints.

  3. Powered off Arduino but IN1307N was powered via coin cell battery. When I reconnected Arduino, got the correct prints.

  4. Finally, powered off Arduino. Removed the battery. I assume RTC chip should now lose its data. Repeated step 1 but now I don’t get correct prints. In fact, RTC chip doesn’t seem to respond. The print I get is simply:

RTC is NOT running!
2000/0/0 0:0:80
2000/0/0 0:0:80
2000/0/0 0:0:80

What’s going on here? Code is shown below. Library I am using is RTClib.

// https://github.com/jcw/rtclib

#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 rtc;

void printDateTime(DateTime inDt)
{
  Serial.print(inDt.year(), DEC);
  Serial.print('/');
  Serial.print(inDt.month(), DEC);
  Serial.print('/');
  Serial.print(inDt.day(), DEC);
  Serial.print(' ');
  Serial.print(inDt.hour(), DEC);
  Serial.print(':');
  Serial.print(inDt.minute(), DEC);
  Serial.print(':');
  Serial.print(inDt.second(), DEC);  
}

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

  delay(100);
  if (!rtc.isrunning()) {
    Serial.println(F("RTC is NOT running!"));
  }
  else {
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }
}

void loop () {
  DateTime currDt = rtc.now();
  printDateTime(currDt);
  Serial.println();
  delay(3000);
}

Removed the battery.

Pardon the stupid question, but did you put the battery back in the RTC ?

Yes, I reconnected the battery. I forgot to mention that in my first post.

I looked at the datasheets and found the following:

  • DS1307: Bit 7 of Register 0 is the clock halt (CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. On first application of power to the device the time and date registers are typically reset to 01/01/00 01 00:00:00 (MM/DD/YY DOW HH:MM:SS). The CH bit in the seconds register will be set to a 1. The clock can be halted whenever the timekeeping functions are not required, which minimizes current (I BATDR ).
  • IN1307: Bit 7 of Register 0 is the Clock Halt (CH) bit. When this bit is set to a one, the oscillator is disabled. When cleared to a zero, the oscillator is enabled.

IN1307 is not explicit about what it does to the CH bit when I suddenly pull out the battery power and put it back. So made this code change:

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

  delay(100);
  while (!rtc.isrunning()) {
    Serial.println(F("RTC is NOT running. Resetting CH bit ..."));
    rtc.writeByteInRam(0, 0);
    delay(100);
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }
}

Now it works as expected. But this code does not work when I replace IN1307 with DS1307. DS1307 simply says, "RTC is NOT running. Resetting CH bit ..."