Main Menu

search

You are here

RTC - programming protocols

[last updated: 2024-08-06]
PCF8523 - program code
RTC - PCF8523
-----

  • Setting RTC time:
    In the setup function of your Arduino code
    there is a section to initialize the 8523.
      delay(2000); // wait for RTC to initialize
      if (! rtc.initialized() || rtc.lostPower()) {
            Serial.println("RTC is NOT initialized. You must set the time.");
            // this line will set the date & time to a hard-coded value
            rtc.adjust(DateTime(2024, 8, 5, 7, 5, 0)); // rtc.adjust(DateTime(yyyy, mo, day, hr, min, sec))
            // OTOH this line will set the date & time to the time of your PC as of the last time this sketch was compiled
            // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
            }

  • When an un-powered (and with no battery backup) 8523 is first turned on,
    it is not "initialized". That is,
    there is no date/time saved in it.
    The above code will run.
    In this code block, an rtc.adjust command is executed, and, depending on how you've got it programmed,
    it will set the 8523 date/time either to a hard-coded value (as in the code example above),
    or (by un-commenting the " // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));" line)
    it will set the 8523 to the date/time of your PC clock when your program was compiled (ie. downloaded to your Arduino).

  • If you have an 8523 that HAS been initialized, that is, it has a saved date/time in it,
    and it has had a battery backup installed,
    BUT it has the wrong time set,
    then here is my inelegant/convoluted solution to resetting the time:
    • Put this line in your setup function, but outside of the if (! rtc.initialized...) statement.
      that is, so that it will be executed on startup, regardless that there is already a date/time saved in the 8523.
            rtc.adjust(DateTime(2024, 8, 5, 7, 5, 0)); // rtc.adjust(DateTime(yyyy, mo, day, hr, min, sec))
    • compile and upload the program, which will install/set the time saved in the 8523 to your desired, correct value.
    • edit your program again, and now remove or comment out the rtc.adjust line.
      This will prevent your program from resetting the 8523 date/time every time you power up your Arduino.

.

eof