DS3231Wrong date and time (and they're stuck too)

Hello,
first post here.

I'm testing a DS3231 with my Arduino Uno board.

I'm using this simple code

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

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("Test the connections");
    while(true);
    }

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

void loop() {
  
  DateTime now = rtc.now();

  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(pad(now.month()));
  Serial.print("/");
  Serial.print(pad(now.day()));
  Serial.print(" ");

  Serial.print(pad(now.hour()));
  Serial.print(":");
  Serial.print(pad(now.minute()));
  Serial.print(":");
  Serial.print(pad(now.second()));
  Serial.println(); 

  delay(1000);
  
  }

char *res = malloc(5);

String pad(int n) {
  sprintf(res, "%02d", n);
  return String(res);
  }

The library is the RTClib from Adafruit version 2.0.2.
What I want is the date and the time in the YYYY/MM/DD hh:mm:ss format (the "pad" is to have the months, the days, the hours, the minutes and the seconds in a two-digit format, so 01, 02, ... ).

When I run the code, the date and the time are totally wrong and they even go backwards until they stabilise to a value, like in the picture attached:

Screenshot 2022-02-26 at 10.40.41

The wiring is correct, I'm using a breadboard (used different places on the breadboard).

I tried to remove the battery and put it back, but nothing changes.
I tried without the battery, but nothing changes.

I installed the Time library by Michael Margolis (version 1.6.1) and used the SyncArduinoClock Processing to set a time on the DS3231and while the processing worked, once I'm back on the Arduino and my sketch, I got the weird date and time values again.

Any suggestions?

Take out the calls to your pad() function. Is the date and time correct now and does it change as expected ?

Where in the sketch do you actually set the time ?

Your sketch is pretty dumb, but I can't see a problem with it.

Great, you sound 100% confident about that and we don't need to worry about it at all.

This Processing code can set the time on the RTC without using an Arduino? How does that work, do you connect RTC to i²c bus on the PC motherboard?

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

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("Test the connections");
    while(true);
    }

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

void loop() {
  
  DateTime now = rtc.now();
  char buffer[30];

  sprintf(buffer, "%04d/%02d/%02d %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second());
  Serial.println(buffer); 

  delay(1000);
  
  }

Nothing changed.

Screenshot 2022-02-26 at 11.38.24

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

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("Verificare le connessioni");
    while(true);
    }

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

void loop() {
  
  DateTime now = rtc.now();

  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" ");

  Serial.print(now.hour(),DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.println(); 

  delay(1000);
  
  }

I meant I used this Processing Time/SyncArduinoClock.pde at master · PaulStoffregen/Time · GitHub

Then I tried my sketch again but nothing changed, still wrong date and time.

Sorry, I used the Arduino. After I used the Processing code (which worked), I loaded my sketch again but I got the same wrong date and time.

Put this in setup() to set the time on the RTC

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

I note that you are no longer using the F() macro in your newly posted sketch

Tried both using the F() macro and not using it to see if anything changed.

Ok, it works now.

Got a multimeter and both the battery that came with the sensor and the one I used in my testing read around 3.7 V.

Got a new battery (read about 3.3 V) and at first the serial monitor was blank.
I loaded @PaulRB's sketch (reply #5) and it worked, but the time was 3 minutes behind.
Then I loaded my sketch and it also worked, although the time was still 3 minutes behind.

So I loaded this sketch on my Arduino and run this Processing code to set the time on the RTC from the time on my computer.
And it worked:

Screenshot 2022-02-27 at 10.18.40

I turned off the Arduino, waited a bit and then turned it on again and it was still happily working.

I guess the wrong voltage of both batteries screwed up the read in my tests? Is that possible?

Anyway, thanks @PaulRB and @UKHeliBob for your time and your help.

The sketch I posted was written to give the same results as your sketch, but, as you see, it is far shorter and simpler.

The 3.7V batteries are probably rechargeable lithium-ion and the 3.3V is probably a non-rechargeable lithium battery. Either should work fine, so I don't understand why the 3.7V batteries did not work.

Did you try the 3.7V batteries again, now that your sketch is working?

Also, please do not post screen captures of serial monitor. Instead, copy the text from serial monitor and paste that into your post between code tags.

Doesn't work. And now, once I put back the new battery, the time is 2 seconds behind.

The batteries are not rechargeable (the sensor came with a CR2025 3V, I used two CR2032 3V).

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