RTC DS1307 does not keep time when unplugged

Hello,

I just received the SparkFun Real Time Clock Module but the clock stops when unplugged.

I checked the battery (reads 3V) and I'm using the sample code shipped with the RTC. No, I'm not resetting the time each time and I'm calling rtc.setTime only once :wink:

Any help would be greatly appreciated!
Thanks!

No, I'm not resetting the time each time and I'm calling rtc.setTime only once

The code you posted doesn't read the time correctly.

What do you mean? here’s the sample code:

void setup() 
{
  // Use the serial monitor to view time/date output
  Serial.begin(9600);
 
  rtc.begin(); // Call rtc.begin() to initialize the library
 
  // Now set the time...
  // rtc.setTime(16, 32, 7, 2, 31, 10, 16);  // Uncomment to manually set time
}

void loop() 
{
  static int8_t lastSecond = -1;
  
  // Call rtc.update() to update all rtc.seconds(), rtc.minutes(), etc. return functions.
  rtc.update();

  if (rtc.second() != lastSecond) // If the second has changed
  {
    Serial.print(String(rtc.hour()) + ":"); // Print hour
    if (rtc.minute() < 10)
      Serial.print('0'); // Print leading '0' for minute
    Serial.print(String(rtc.minute()) + ":"); // Print minute
    if (rtc.second() < 10)
      Serial.print('0'); // Print leading '0' for second
    Serial.print(String(rtc.second())); // Print second
    
    lastSecond = rtc.second(); // Update lastSecond value
  }
}

Did you confirm the battery polarity?

Also confirm that the battery voltage is present at the DS1307 chip . Sometimes the battery holders can be defective. In this image, the two pins at the top left of the chip are ground(at the far left) and Vbat(next to the two pins going to the crystal oscillator 32C3Y). With power off and a multimeter, confirm that the battery voltage is present at the chip.

Thanks for the tips!

I will have to check a little more but it seems that I have 3V voltage (when running on battery). I will let you know later today.

My suspicion is that the oscillator is dead. I tried to shoot an email to Adafruit customer support, just in case...

the clock stops when unplugged.

My suspicion is that the oscillator is dead.

An oscillator which runs on usb but not on battery is unusual. Here is an old thread covering several ds1307 chip and oscillator problems when running on battery voltage. http://forum.arduino.cc/index.php?topic=303406.0

I would not expect these kinds of problems with a Spark Fun Module, but there may be some information in the thread you find useful.

I tried to shoot an email to Adafruit customer support

Why? You said it is a Sparkfun module.

   Serial.print(String(rtc.hour()) + ":"); // Print hour

Why are you using String for such a trivial operation? rtc.hour() returns an integer which is then converted to a String. Then ":" is concatenated to the integer String and then the resulting String is printed.

   Serial.print(rtc.hour());
    Serial.print(":");

Print the integer returned by rtc.hour(). Print the semicolon. Done. This has the added advantage that, unlike using the String library, it doesn't run the risk of running out of memory.

Pete

Sorry, I meant I sent an email to Sparkfun's customer support.

It seems I have a 0.5v reading at the DS1307 chip (as measured in the picture below)

Thanks a lot for the C tip, I doubt it's related to my issue but it's good to know for the future... :slight_smile:

Update: After messing with the CH (Clock Halt) bit, I think I enabled the oscillator and it's working now!
Thanks @cattledog for the link! :slight_smile:

Pull out the little plastic tab that stops the battery discharging while the module is on the shelf at the shop.

lol :wink:
thanks! :slight_smile: