Having trouble with RTC clock

Hi all, I am super new to Arduino UNO and I have been playing around with the code for the clock. I am trying to have it read the real time from the computer reference but it tends to change to various times when I come back to work on it again. It sometimes will work and sometimes it doesn't.
This is my code:



RTC_PCF8523 rtc;


char daysOfTheWeek[7][12] = {
  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};


// timing
#define LOG_INTERVAL 1000
#define SYNC_INTERVAL 1000
uint32_t syncTime = 0;


#define ECHO_TO_SERIAL 1

void setup(void) {


  Serial.begin(9600);
  Wire.begin();


#ifndef ESP8266
  while (!Serial);
#endif


  // RTC setup
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }


  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }




  DateTime now = rtc.now();


  // ---------- THERMISTOR TEMP ----------
  int analogValue = analogRead(thermistorPin);


  float R = R_fixed * (1023.0 / analogValue - 1.0);
  float invT = (1.0 / T25) + (1.0 / B) * log(R / R25);
  float T = 1.0 / invT;
  float Tc = T - 273.15;


  // ---------- SENSOR + VOLTAGE ----------
  sensorValue = analogRead(analogInPin);
  readVolt = sensorValue * (5.0 / 1023.0);


  // ---------- DATETIME STRING ----------
  String dateTime = String(now.year()) + "/" +
                    String(now.month()) + "/" +
                    String(now.day()) + " " +
                    String(now.hour()) + ":" +
                    String(now.minute()) + ":" +
                    String(now.second());


  // ---------- CSV ROW ----------
  String dataString = dateTime + "," +
                      String(Tc) + "," +
                      String(sensorValue) + "," +
                      String(readVolt);


  // ---------- WRITE TO SD ----------
  logfile.println(dataString);
  logfile.flush();


  // ---------- SERIAL DEBUG ----------
  Serial.println(dataString);

"" This is all my code related to time. Thank you.

Once you give your RTC the real time (either system time correction: compile time [see post #3] (rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));), or your local-time, hard-coded, you need to comment-out (or remove) that line from your sketch, THEN re-upload the sketch.

Your sketch is incomplete...

Try this example from the library you are using... PCF8523/Examples/PCF8523_RAW/PCF8523_RAW.ino at master · SpellFoundry/PCF8523 · GitHub

The RTC does not continuously read the time from the computer.
This line:

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

sets the RTC to the compile time of the sketch, not the current computer time when the Arduino starts.

Also, in your code it only runs if:

rtc.lostPower()

is true.

So after the RTC has been set once, it should keep time by itself using its backup battery. If the time changes randomly later, I would first check the RTC backup battery and the RTC wiring.
For testing, you can temporarily set the RTC every upload by using:

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

without the lostPower() condition.

But remove that again afterwards, otherwise every reset will set the RTC back to the time when the sketch was compiled.

And definitely have a look at @xfpd's examples!

This worked thank you!

Please mark the post that solved your problem as the solution.