DS3231 not updating time

Hello,

I have a program which I am using to count down days left until an event. When I upload the program, the time get set correctly, but is never updated.

I tried using one of the examples afterwards, to read the time back. But the time just stays the same and is not updated.
I am using this DS3231:

Code I am reading the time from the ds3231 with:

/* Timestamp functions using a DS1307 RTC connected via I2C and Wire lib
**
** Useful for file name
**		` SD.open(time.timestamp()+".log", FILE_WRITE) `
**
**
** Created: 2015-06-01 by AxelTB
** Last Edit:
*/

#include "RTClib.h"

RTC_DS1307 rtc;

void setup () {
  Serial.begin(9600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

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

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

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

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_FULL:\t")+time.timestamp(DateTime::TIMESTAMP_FULL));

 //Date Only
 Serial.println(String("DateTime::TIMESTAMP_DATE:\t")+time.timestamp(DateTime::TIMESTAMP_DATE));

 //Full Timestamp
 Serial.println(String("DateTime::TIMESTAMP_TIME:\t")+time.timestamp(DateTime::TIMESTAMP_TIME));

 Serial.println("\n");

 //Delay 5s
 delay(5000);
}

And this is the RTC part in my code in my program:

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 rtc;

void setup () {

  if (! rtc.begin()) {
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

The result is that the time is standing still, like here.
Does anyone have any suggestion what this might be? Am I using the correct library?

image

Could not post it with more than one picture since I am a new user:

I am using this DS3231:
https://www.ebay.com/itm/143955868597?hash=item2184703bb5:g:SagAAOSwtmVgMoai&frcectupt=true

And wiried it like this:

Hi,
to fix this problem, after setting your RTC, comment out this line and load the sketch again.

Hi, thanks for the suggestion. I tried commenting out this line in both codes with no luck. The time does still not updating like is is supposed to do.

I also tried the ds3231 example, but no luck there either with the line commented out.

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"

RTC_DS3231 rtc;

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

void setup () {
  Serial.begin(9600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // This line sets the RTC with an explicit date & time, for example to set
  // January 21, 2014 at 3am you would call:
  // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

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(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");

    // calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));

    Serial.print(" now + 7d + 12h + 30m + 6s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();

    Serial.print("Temperature: ");
    Serial.print(rtc.getTemperature());
    Serial.println(" C");

    Serial.println();
    delay(3000);
}

Probably unrelated to your problem, and just an observation, but I wouldn't power your RTC module from the UNO Vin pin. Use the 5V pin instead.

Then why are you doing:

EDIT: Ah, I see you changed the code in post #4.

EDIT2: Does your code in #4 print out:

"RTC lost power, let's set the time!"

If it does, then you need to call the rtc.adjust() function to set the time AND start the clock running.

1 Like

hi @bjonness welcome to the fora.

If you have the module RTC in the picture, please google

 diode resistor ds3231

and poke around. You must address the issues caused by the badly designed battery charging circuit it uses, which is not meant for the CR2032 or whatever the battery is it usually comes with.

Old known problem, easy solutions.

a7

1 Like

Okei, I tried again and I get that print out:

RTC lost power, let's set the time!
2022/3/3 (Thursday) 18:43:53
 since midnight 1/1/1970 = 1646333033s = 19054d
 now + 7d + 12h + 30m + 6s: 2022/3/11 7:13:59
Temperature: 0.00 C

2022/3/3 (Thursday) 18:43:53
 since midnight 1/1/1970 = 1646333033s = 19054d
 now + 7d + 12h + 30m + 6s: 2022/3/11 7:13:59
Temperature: 0.00 C

2022/3/3 (Thursday) 18:43:53
 since midnight 1/1/1970 = 1646333033s = 19054d
 now + 7d + 12h + 30m + 6s: 2022/3/11 7:13:59
Temperature: 0.00 C

2022/3/3 (Thursday) 18:43:53
 since midnight 1/1/1970 = 1646333033s = 19054d
 now + 7d + 12h + 30m + 6s: 2022/3/11 7:13:59
Temperature: 0.00 C

2022/3/3 (Thursday) 18:43:53
 since midnight 1/1/1970 = 1646333033s = 19054d
 now + 7d + 12h + 30m + 6s: 2022/3/11 7:13:59
Temperature: 0.00 C

2022/3/3 (Thursday) 18:43:53
 since midnight 1/1/1970 = 1646333033s = 19054d
 now + 7d + 12h + 30m + 6s: 2022/3/11 7:13:59
Temperature: 0.00 C

If I reload the program I get the same message with the RTC lost power again every time. (I have not disconnected my in between)

The DS3231 is lighting up red, I also changed it to a 5v port on the arduino.

Thanks, I will take a look at this, but is this not only related to battery charging, and not my problem with the clock not counting?

Yes, sry, I should have made that clear.

a7

It's printing out "RTC lost power, let's set the time!" because the lostPower() function is returning a '1'. It's getting this from Bit 7 of the DS3231 control register - which enables/disables the RTC oscillator.

Try:

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

The adjust() function sets the RTC oscillator going and should only be called once, as on further resets of the Arduino, the RTC oscillator should be still running.

If the date/time updates correctly when you run the code in #4 with the modification, then reset the Arduino using the reset button. It should not report that the RTC lost power and should correctly show the date/time updating.

If you cycle the power to the Arduino, it should not report that the RTC lost power. If it does, then that suggests that there is something wrong with the RTC battery backup circuitry/coin cell.

1 Like

I would in such case examine soldering quality of DS3231 chip. smartphone camera can help.

Tried your code and it sets the time correctly to 2014, but when I reset the arduino or re-upload the code I get the report that the RTC has lost power again.
If I disconnect the RTC, I get "RTC not found", so the system has contact with the RTC, but it is not updating the time for some reason.

Could not see anything wrong, might it be the ds3231 itself which is causing my trouble?

of course. but i would first desolder all chips and check underside and PCB. carefully soldering back and when not working - recycling

1 Like

Ok, so once the time/date is set, before you reset it or upload more code, does the time increment, or does the same time get printed on the serial monitor repeatedly?

As an aside, how are you powering the UNO? Is it via the USB connector or via the separate DC in connector? Have you powered it using the DC in conndctor?

1 Like

The same time gets printed on the serial monitor repeatedly.

I am powering the arduino uno through the DC connector with 12V DC. Have tried only trough the USB port also.
I just read this:

Vin can also be used as a voltage output, copying the voltage supplied via the barrel adaptor or USB.

Maybe I have fried my DS3231 with powering it from the VIN port on the Arduino? I am powering it from the 5v port now (led diode still works).

If you wired it up as per your diagram so the RTC is powered from the Vin header pin, then you've put 12V into the RTC board. I think it's safe to say that the RTC board is now toast.

1 Like

Yeah, I just realised that.. I have a adafruit ds3231 also, but I need to solder on the pins on that one. Going to try that tomorrow.
Is this how it should be wired?

GND -> GND
VCC-> 5V
SDA -> A4
SCL -> A5
Is this correct?

Well, you live and you learn!

Looks good.

1 Like

yes. and here are same pins

No. Bad advice. The Aref pin is an input. You can not use it to supply 5v. Data sheet is very clear.
https://forum.arduino.cc/t/use-aref-as-digital-output/338249

1 Like