DS1307 not working on battery

I tried to make digital clock which just display the time.
I used: Arduino Uno R3, DS1307 (rtc module), ssd1306 (OLED Display), 3V coin cell battery
Libraries: Wire.h, RTClib.h, Adafruit_SH1106.h

  1. I made real time clock successfully but problem is that rtc module (ds1307) is not keeping track of time. Whenever I shutdown Arduino it starts from beginning from where time was configured during compilation/uploading. I checked voltage of coin cell battery & it was perfect.

  2. Another thing I noticed is that there is difference of 8-10 seconds between actual time & time displayed by rtc.

so how can I solve this two problems?

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

#define OLED_RESET 4
RTC_DS1307 rtc;
Adafruit_SH1106 display(OLED_RESET);

void setup() {
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  Wire.begin();
  rtc.begin();
  //display.display();
  //delay(2000);
  //display.clearDisplay();
 
  // code to set the time on the RTC module.
  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
}

void loop() {
  display.clearDisplay();
  DateTime now = rtc.now();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  
  // Print the date and time in IST.
  //Printing Date
  //Day
  if(now.day()<10)
  {
    display.print("0");
  }
  display.print(now.day(), DEC);
  display.print('/');
  
  //month
  if(now.month()<10)
  {
    display.print("0");
  }
  display.print(now.month(), DEC);
  display.print('/');
  
  //year
  display.print(now.year(), DEC);
  display.println();
  if(now.hour()<10)
  {
    display.print("0");
  }
  display.print(now.hour(), DEC);
  display.print(':');
  if(now.minute()<10)
  {
    display.print("0");
  }
  display.print(now.minute(), DEC);
  display.print(':');
  if(now.second()<10)
  {
    display.print("0");
  }
  display.print(now.second(), DEC);
  // display.display();
  display.println();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  // display.println();
  // display.println();
  display.println("made by Yash Gandhi");
  //display.print(s);
  display.display();
  // delay(1000);
}

comment the line
rtc.adjust(DateTime(F(DATE),F(TIME)));
in your setup and check again. i beleive that this line took date and time of system on compilation time and made it part of code. it was supposed to be used only for first time.

ok I will comment it but it would not solve the problems that I mentioned

It will :slight_smile:

You are unconditionally setting RTC DatetTime to the compilation date and time on every powerup. Does it not ring any bell to you ?

32,768 kHz crystal placed?

Are you running a UNO R3 with 3V?
I suggest you post a schematic of your project.

@Waheed_Shah & @Delta_G from your P.O.V, I need to 1st upload a code that sets the time on RTC which will contain line "rtc.adjuct......" & after that I need to upload code of my project. Did I understand in right way?

@buckfast_beekeeper How can I check kHz of crystal in my ds1307?

@ruilviana I am not powering Arduino Uno R3 with 3V. I am using my laptop to turn on Arduino. what do you mean by schematic of my project? I don't know much. I am a beginner.

What are you using? Only the DS1307 (like this) or the DS1307 on a break out board (like this).

In first case you must have an extern crystal. To test that, you need a oscilloscope or frequentiecounter.

A drawing how you connect the DS1307 and you're arduino.


Consider uploaded animated image as my sketch.
I am using DS1307 shown in this image: DS1307 RTC clock module Set and Read Time using Arduino

you'll find difference between RTC Module shown in image & shared URL but situation is that I wanted to give you animated schema & it was the only RTC that was available on that website but you can consider the connections in actual way.

How can you set the real time? There are no buttons and routines to do that. There isn't NTP connection. There isn't GPS connection. The DS1307 can't know what the actual time is. The only thing he know is the compile time. Between compile time and real time there is a difference of x seconds, minutes or days.

The screen gives a time? Than all that you have programmed is doing what it must doing.

@yash_7200 I am almost certain that this explains the problem you are having.

The DS1307 is like the clock on your microwave oven. In order for it to show the correct time, first you need to set the correct time.
What this line of code does, is

  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));

it sets the time on the RTC to the time that the code was compiled. Every time this line of code runs, it will set your RTC to the same time, the time the code was compiled.

I did some Googling and found this: Standard Predefined Macros (The C Preprocessor)
So, it looks like you can take this line

rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));

and just put in the date and time yourself, like this:

rtc.adjust(DateTime(F("Nov 29 2023"),F("05:49:00")));

and set the time that way. That way, you can set it to any date and time you want, not just whatever date and time the code was compiled.

@Waheed_Shah & @Delta_G I followed your guidance.

  1. 1st I uploaded code to just setup time then
  2. I uploaded code of my project (after commenting time adjusting line)

Now, rtc is able to keep a track of time but now there is again issue of accuracy.
Now I noticed that accuracy is decreased. There is a difference of 20-21 seconds between actual time & time being display on OLED.

so how do I resolve this?

Please read what I wrote in Post #18, above.
If you put in the date and time yourself, you can set it to whatever time you want. If your clock is 20 seconds behind, then set it to a time that is 20 seconds ahead, and see if that helps.

Like odometer say. But it isn't the accurate time. It is the time started at the time you initiate. Do you set

rtc.adjust(DateTime(F("Dec 25 2025"),F("05:49:00")));

It will give you the time started at Christmas 2025.

You must make something with buttons to set the correct hour and date. At least 2 buttons, 1 for set/select and 1 to increase. Then push set to select the setting procedure, push the increase button to set the correct hour, push set (save the hour and select minutes) , push increase until the minutes are correct, push set (save the minutes and select seconds), .....

Other way is using an extern time. GPS, NTP, DCF (radiaclock) or similarly.

Or setting the time with serial connection from PC (IDE serial monitor, Putty, ...)

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