Why my RTC module is not keeping time?

Hello,
I got this RTC module called DS3231 and I don't know why is not keeping the time.

After I unplug and plug the board again, is counting time up from the moment I uploaded the code.

I tried with another module (because I bought a pack of 3), I tried with a rechargable battery LIR2032, and it's the same. The module is connected to pin A4 and A5 (i2c on Arduino Nano) and powered on the 3.3v pin.

What can I do? :pensive:

This is my code:

#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup () {
  Serial.begin(9600);
  lcd.init();
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
  DateTime now = rtc.now();
  
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print(now.hour());
  lcd.print(":");
  lcd.print(now.minute());
  lcd.print(":");
  lcd.print(now.second());
}
1 Like

Remove/comment this line:

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

1 Like

This line of code will reset your RTC back to the compile time of your sketch every time you run the code. Remove it and your RTC should then maintain time.

1 Like

OMG it was so simple, thanks both of you! :pray:

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