Data logger DS1307 time problem

Hi all, i have been having a problem with time keeping on my rtc. The time can be set to the module and works fine but if the Arduino loses power the time changes pack to what it was when the sketch was made. I know this is what the line RTC.begin(DateTime(DATE, TIME)); does but if i remove that line the time gets messed up. Can any one help me solve this problem?

/*Temperature and light data logger.
 *using i2c rtc, dallas one wire temperature sensor, sd shield
 */
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include "RTClib.h" 
#include <SdFat.h>
#include <SdFatUtil.h>
#include <ctype.h>

RTC_Millis RTC;
int ONE_WIRE_BUS = 2;  //one wire bus
int photocell = A2;  //photocell pin
int photocellReading = 0;  //photocell storage variable


OneWire oneWire(ONE_WIRE_BUS);  //Setup oneWire
DallasTemperature sensors(&oneWire);  //OneWire reference for Dallas Temperature sensor

//Variables used by the Sd Library
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

char name[] = "LOG.csv";     //Array for file name

void setup(void) {
  RTC.begin(DateTime(__DATE__, __TIME__));  //rtc start and update time
  sensors.begin();  //one wire sensor start
  
  pinMode(10, OUTPUT);       //Pin 10 set as out put for sd to work
  card.init();               //Initialize the SD card
  volume.init(card);         //Initialize a volume on card
  root.openRoot(volume);     //Open the root
}

void loop(void){
  DateTime now = RTC.now();
  sensors.requestTemperatures();
  photocellReading = analogRead(photocell);
  
  file.open(root, name, O_CREAT | O_APPEND | O_WRITE);
  file.print(now.unixtime());
  file.print(",");  
  file.print(now.month(), DEC);
  file.print("/");
  file.print(now.day(), DEC);
  file.print("/");
  file.print(now.year(), DEC);
  file.print(" ");
  file.print(now.hour(), DEC);
  file.print(":");
  file.print(now.minute(), DEC);
  file.print(":");
  file.print(now.second(), DEC);  
  file.print(",");
  file.print(sensors.getTempCByIndex(0));
  file.print(", ");    
  file.print(photocellReading);  
  file.println();
  file.close();
  
  delay(60000);
}

I know this is what the line RTC.begin(DateTime(DATE, TIME)); does but if i remove that line the time gets messed up.

Please describe what "the time gets messed up" means. You only need to set the time once. After that, the RTC keeps time. If it doesn't you have either a hardware problem or a problem with the code that fetches the time from the RTC. Hard to tell which is the case from your description.

What i mean is the time goes from being correct:(2011/5/31 18:43:21) to this (2106/2/6 6:29:26) when that line is removed and re uploaded.

You need the RTC.begin() but you only need to set the time once

    RTC.begin();
    ////// Uncomment the next line and upload the sketch to set the time,
    ////// Then comment it out again and re-upload the sketch so the clock is not set back each time the Arduino resets
    // RTC.adjust(DateTime(__DATE__, __TIME__)); // Set the clock to the build time

If you want to get fancy you can put a flag in EEPROM to say that the clock has been set and only set it if the flag is clear… ‘clear’ being whatever the initial erased state of EEPROM is. Then you can re-upload the sketch to set the clock and not have it reset the clock on reset.

johnwasser: You need the RTC.begin() but you only need to set the time once

    RTC.begin();
    ////// Uncomment the next line and upload the sketch to set the time,
    ////// Then comment it out again and re-upload the sketch so the clock is not set back each time the Arduino resets
    // RTC.adjust(DateTime(__DATE__, __TIME__)); // Set the clock to the build time

If you want to get fancy you can put a flag in EEPROM to say that the clock has been set and only set it if the flag is clear... 'clear' being whatever the initial erased state of EEPROM is. Then you can re-upload the sketch to set the clock and not have it reset the clock on reset.

i tried this but i get a error compiling: no matching function for call to 'RTC_Millis::begin()'

Is there any chance you have a non-working backup battery?

RTC_Millis doesn't use the DS1307. It just remembers the date and time you feed in and adds the current Millis() counter to get a rough clock.

If you want to use the DS1307 you have to change your RTC variable to type RTC_DS1307. Then it will have the separate begin() and adjust().

Thanks i got it working now, i reset the battery and tried the new code again and it worked.

WEMOS D1 MINI DS1307 RTC MODULE COIN BATTERY DRAIN
Hope this helps some people out there. There’s dodgy boards out there which need a line on the PCB to be cut.
To see if you have a dodgy board. Simply take out and test the voltage in the coin battery. Mine went from 3v to 2v in just a few mins then dead shortly after. If you already have one in there… It’ll be dead.
A coin battery should last for years. Do at your own risk:
Follow the trace Between:
DS1307 VCCBATT (+) ------» 3vBATT this is ok and needed - do not cut here!
From
3vBATT(+) -----» cut here
You can see the trace next to where D6 D7 is printed on the board on the battery side of the board
I just scratched it off. Works fine now.

Picture Attached