2

I am trying to make an LED clock using an DS3231 to keep track of time, but for some reason, it's not keeping time when the Arduino is turned off.

I tried to disconnect the clock from a power source for 5 minutes, and when I powered it back on, the clock picks up the time it was displaying before powering off. I thought the battery I was using at first ran out, and so I replaced it with a new one, but the issue persists. Then I read that it could be that I am using the non-rechargeable one, and to fix this, I removed some resistors and diodes to turn off the charging function, but that also didn't fix the issue. At this point, I'm not sure if this is more on hardware or software issue that the clock is not keeping count of time when Arduino is turned off. It could be that the module I bought was the cheap knockoff, which doesn't function like it should, so I ordered a better one.

I'll the provide the snippet of codes I used:

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

RTC_3231 rtc;

int hoursNow;
int minutesNow;
int secondsNow;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//  rtc.adjust(DateTime(2023, 6, 31, 14, 21, 0));
}

void loop() {
  DateTime now = rtc.now();
  hoursNow = now.hour();
  minutesNow = now.minute();
  secondsNow = now.second();
  Serial.print(hoursNow);
  Serial.print(":");
  Serial.print(minutesNow);
  Serial.print(":");
  Serial.print(secondsNow);
  delay(1000);
}

Maybe I'm missing something in the code that is causing it not to do what I want it to do, but I've looked online, and most were pretty similar to the code I have.

3 Answers 3

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

You have this line in setup that sets the date and time to the date and time that the code was compiled. That's fine to set it once, but that time never changes. It's the time that it was when you compiled the code. So every time it runs, you are setting the time back to this. The RTC keeps the time running while the Arduino is off, but as soon as it's turned on your code sets it back to what it was before.

If you remove that line, then things will work the way you want. You might need to load a sketch once with a line like this to set the time, run it once, then load a new sketch without that line. Or include a routine in your code to allow a user to set the time.

2
  • How would that work? Should I use a conditional statement? I've tried commenting out that particular line just as you suggested, and sure enough, the rtc is keeping real time. However, without that line, it won't update the time to match my current time. Commented Jul 21, 2023 at 18:58
  • I said it in my answer. Load a sketch that allows you to set the time. Run it ONCE. Now the time is set. Then load a NEW sketch that doesn't set the time and the RTC will still have the right time that was set by the sketch before.
    – Delta_G
    Commented Jul 21, 2023 at 20:21
0

You might consider using the EEPROM to manage the execution of your time-set command based on a boolean flag. Initially, write a false (or 0) to a specific address in the EEPROM to signify that the command has yet to be executed. Your program can then check this flag at startup. If the flag is false, it triggers the time-set command and subsequently updates the flag to true (or 1) in the EEPROM. This ensures that the command only runs when necessary.

#include <EEPROM.h>

// Assume the flag is stored at address 0
const int flagAddress = 0;

void setup() {
  EEPROM.begin(512); // Adjust size as needed
  bool timeSetFlag;
  EEPROM.get(flagAddress, timeSetFlag); // Read the flag

  if (!timeSetFlag) {
    // Time-set command goes here
    timeSetFlag = true; // Update flag
    EEPROM.put(flagAddress, timeSetFlag);
    EEPROM.commit(); // Save changes to EEPROM
  }
}

void loop() {
  // Main code
}
1
  • You probably never use the DS3231 before, __DATE__ and __TIME__ is the date time information when a sketch is compiled, try any Arduino sketch to print it out and see yourself. Saving to EEPROM does not address the issue.
    – hcheung
    Commented Apr 6 at 0:22
0

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

I know this is an old question, however the answers so far doesn't correctly address the issue that the OP facing.

The __DATE__, __TIME__ is the time when the compiler compiled your code, if you compiled the code yesterday, and you never change the code and never compiled the code again, the two constants will contain the date and time of yesterday when the code was compiled. It is not a real time date time that you thought it was.

What you need to do is to following these steps.

  1. Take out the back-up battery;
  2. Only use __DATE__ and __TIME__ once when you first time powering up the module, with the following code:
if (! rtc.isrunning()) {
    Serial.println("Setup time based on compiled date and time");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
  1. Plug in the module and immediately upload the new sketch, the date and time should be setup the sketch complied date time, if you do it quickly, the RTC will likely setup to the time 10-15s earlier (i.e. when code was compiled).
  2. insert the battery while the module is powered by VCC.

Next time when you plug-in the module, the if block won't be executed to reset the time as long as the back-up battery is able to power the module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.