RTCLib.h DS3231 Clock Issue

For RTC breakouts, etc., use the Other Products from Adafruit forum

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

Hi,

I have an Arduino Uno WiFi Rev2 board with a DS3231 RTC module connected to the Arduino via I2C. My IDE is versions 2.0.3. I am using this Lib: https://github.com/adafruit/RTClib

I am using this to detect the day change and then run EQofTime and update some variables. For about day or so it all runs fine but randomly if the day is say 24th, randomly tiktok.day() will return 4 instead of 24. If the day is the 23rd it will randomly return 3 even though it is the 23rd.

I was watching tiktok.unixtime() as well and this is incorrect on the occasions the issue above occurs.

it works fine I would say 80% of the time. I feels like an issue with the RTClib, DS3231 Module, I2C but I an not good enought to work out how/why. Any help would be appreciated.

Code: Select all

int minChange = -1;
int hourChange = -1;
int dayChange = -1;
int yearChange = -1;
DateTime tiktok;
uint32_t unixNow = 0;
uint32_t unixTargetTime = 0;
int tiktokDay = 0;

void loop() 
{    
  tiktok = rtc.now(); 
  delay(5000);
  unixNow =  tiktok.unixtime();
  Serial.print("unixNow: ");Serial.println(unixNow);
  delay(5000);
  tiktokDay= tiktok.day();
  Serial.print("tiktok day(): ");Serial.print(tiktokDay);Serial.print(" dayChange: ");Serial.println(dayChange);
  delay(5000);
  
  if (tiktokDay != dayChange) {
        Serial.println("Day has changed (or first boot) from: " + String(dayChange) + " to " + String(tiktokDay) + " will now update.");        
        minChange  =  tiktok.minute();
        hourChange =  tiktok.hour();
        dayChange  =  tiktok.day();
        yearChange =  tiktok.year();
        Serial.print("Tickers Not Set. Setting now: ");
        Serial.print("Minute: ");Serial.print(minChange);Serial.print(" Hour: ");Serial.print(hourChange);Serial.print(" Day: ");Serial.print(dayChange);Serial.print(" Year: ");Serial.println(yearChange);
        EQofTime(unixNow);
        setSunriseTime24h(sunriseTrack);
        setSunsetTime24h(sunsetTrack);  
   }  
}

Output:

Code: Select all

18:56:28.504 -> unixNow: 1677264982
18:56:33.483 -> tiktok day(): 24 dayChange: 24
18:56:38.494 -> Sun is DOWN SLEEP
18:56:43.476 -> unixNow: 1677264997
18:56:48.492 -> tiktok day(): 24 dayChange: 24
18:56:53.480 -> Sun is DOWN SLEEP
18:56:58.496 -> unixNow: 1677265012
18:57:03.481 -> tiktok day(): 24 dayChange: 24
18:57:08.493 -> Sun is DOWN SLEEP
18:57:13.475 -> unixNow: 1044385027
18:57:18.489 -> tiktok day(): 4 dayChange: 24
18:57:23.471 -> Day has changed (or first boot) from: 24 to 4 will now update.
18:57:23.534 -> Tickers Not Set. Setting now: Minute: 57 Hour: 18 Day: 4 Year: 2003
18:57:23.598 -> UTC: 1044385027
18:57:23.630 -> Sunrise: 07:38
18:57:23.663 -> Transit: 12:18
18:57:23.663 -> Sunset:  16:58
18:57:23.694 -> Eq of time: -13.95 min
18:57:23.694 -> RA: 21.201h  Dec: -16.17��  R: 0.985836 AU
18:57:23.759 -> Az: 267.26��  El: -18.65��
18:57:28.711 -> unixNow: 1044385042
18:57:33.723 -> tiktok day(): 4 dayChange: 4
18:57:43.724 -> unixNow: 1044385057
18:57:48.710 -> tiktok day(): 4 dayChange: 4
18:57:58.717 -> unixNow: 1044385072
18:58:03.704 -> tiktok day(): 4 dayChange: 4
18:58:13.696 -> unixNow: 1677265087
18:58:18.712 -> tiktok day(): 24 dayChange: 4
18:58:23.696 -> Day has changed (or first boot) from: 4 to 24 will now update.
18:58:23.759 -> Tickers Not Set. Setting now: Minute: 58 Hour: 18 Day: 24 Year: 2023
18:58:23.824 -> UTC: 1677265087
18:58:23.855 -> Sunrise: 07:00
18:58:23.887 -> Transit: 12:17
18:58:23.887 -> Sunset:  17:35
18:58:23.919 -> Eq of time: -13.19 min
18:58:23.919 -> RA: 22.513h  Dec: -9.35��  R: 0.989668 AU
18:58:23.983 -> Az: 272.15��  El: -13.66��
18:58:24.016 -> Sun is DOWN SLEEP
18:58:28.972 -> unixNow: 1677265102
18:58:33.960 -> tiktok day(): 24 dayChange: 24
18:58:38.944 -> Sun is DOWN SLEEP
18:58:43.961 -> unixNow: 1677265117
18:58:48.942 -> tiktok day(): 24 dayChange: 24
18:58:53.962 -> Sun is DOWN SLEEP
18:58:58.948 -> unixNow: 1677265132
18:59:03.963 -> tiktok day(): 24 dayChange: 24
18:59:08.948 -> Sun is DOWN SLEEP
18:59:13.938 -> unixNow: 1677265147
18:59:18.954 -> tiktok day(): 24 dayChange: 24
18:59:23.938 -> Sun is DOWN SLEEP
18:59:28.953 -> unixNow: 1677265162
18:59:33.934 -> tiktok day(): 24 dayChange: 24
18:59:38.947 -> Sun is DOWN SLEEP
18:59:43.959 -> unixNow: 1677265177
18:59:48.944 -> tiktok day(): 24 dayChange: 24

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: RTCLib.h DS3231 Clock Issue

Post by mikeysklar »

I see you have an issue open in the RTClib github repo. That is the right place to reproduce the problem with stock example code.

https://github.com/adafruit/RTClib/issues/276

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

I have it running at the moment to see if it happens again. Will let you know.

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

Just happened as i checked it after reading your comment. I used the sample code only changed the output part so it was easier to track. I put the code here and the output.

Code: Select all

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

RTC_DS3231 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.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));
}
int count = 0;
int day = -1;
void loop () {
    DateTime now = rtc.now();
    Serial.print("Day with DEC: ");Serial.print(now.day(), DEC);Serial.print("  Day no DEC:  ");Serial.print(now.day());Serial.print("   UnixTime: ");Serial.print(now.unixtime());Serial.print("   Count: ");Serial.println(count);
    if (now.day() != day) {   
      day = now.day();
      count = count + 1;
      Serial.print("day var: ");Serial.print(day);Serial.print(" now.day(): ");Serial.println(now.day());
    }
    delay(3000);
}
The output:

Code: Select all

18:03:46.005 -> Day with DEC: 6  Day no DEC:  6   UnixTime: 1044554624   Count: 13
18:03:48.998 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434627   Count: 13
18:03:49.094 -> day var: 26 now.day(): 26
18:03:52.055 -> Day with DEC: 6  Day no DEC:  6   UnixTime: 1044554630   Count: 14
18:03:52.119 -> day var: 6 now.day(): 6
18:03:55.077 -> Day with DEC: 6  Day no DEC:  6   UnixTime: 1044554633   Count: 15
18:03:58.071 -> Day with DEC: 6  Day no DEC:  6   UnixTime: 1044554636   Count: 15
18:04:01.063 -> Day with DEC: 6  Day no DEC:  6   UnixTime: 1044554639   Count: 15
18:04:04.086 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434642   Count: 15
18:04:04.149 -> day var: 26 now.day(): 26
18:04:07.112 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434645   Count: 16
18:04:10.104 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434648   Count: 16
18:04:13.095 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434651   Count: 16
18:04:16.119 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434654   Count: 16
18:04:19.113 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434657   Count: 16
18:04:22.136 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434660   Count: 16
18:04:25.128 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434663   Count: 16
18:04:28.120 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434666   Count: 16
18:04:31.141 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434669   Count: 16
18:04:34.131 -> Day with DEC: 26  Day no DEC:  26   UnixTime: 1677434672   Count: 16

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: RTCLib.h DS3231 Clock Issue

Post by mikeysklar »

Yep, you found a bug. Keep updating the github issue. Good find.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTCLib.h DS3231 Clock Issue

Post by adafruit_support_carter »

Please post a photo of your setup showing how everything is connected so the state of the hardware can be checked.

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

Hi,

The DS3231 module is this one:
https://www.amazon.co.uk/AZDelivery-DS3 ... QLRQH87QA3

I have it powered from the Arduino on the 3.3V line & GND. It did have a battery CR2032 on the RTC module and I have just removed it as I missed the part about removing a resistor if it is not rechargeable as recommended here:
https://lastminuteengineers.com/ds3231- ... t=cmp-true

I am rerunning it without the battery to see if that will make any difference.

The kit is unmoved on a work bench and I don't believe it has much noise around but I don't have a scope to fully confirm that.

Image attached.
Attachments
RTCModule.jpg
RTCModule.jpg (374.02 KiB) Viewed 1755 times

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTCLib.h DS3231 Clock Issue

Post by adafruit_support_carter »

How often does the issue show when running your example sketch from first post?

Not readily seeing the same behavior here:

Code: Select all

12:17:07.617 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500221 Count: 1
12:17:10.631 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500224 Count: 1
12:17:13.612 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500227 Count: 1
12:17:16.626 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500230 Count: 1
12:17:19.640 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500233 Count: 1
12:17:22.621 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500236 Count: 1
12:17:25.635 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500239 Count: 1
12:17:28.615 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500242 Count: 1
12:17:31.630 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500245 Count: 1
12:17:34.644 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500248 Count: 1
12:17:37.625 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500251 Count: 1
12:17:40.639 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500254 Count: 1
12:17:43.654 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500257 Count: 1
12:17:46.635 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500260 Count: 1
12:17:49.649 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500263 Count: 1
12:17:52.664 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500266 Count: 1
12:17:55.645 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500269 Count: 1
12:17:58.660 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500272 Count: 1
12:18:01.641 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500275 Count: 1
12:18:04.656 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500278 Count: 1
12:18:07.670 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500281 Count: 1
12:18:10.651 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500284 Count: 1
12:18:13.666 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500287 Count: 1
12:18:16.680 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500291 Count: 1
12:18:19.662 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500294 Count: 1
12:18:22.676 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500297 Count: 1
12:18:25.657 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500300 Count: 1
12:18:28.672 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500303 Count: 1
12:18:31.686 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500306 Count: 1
12:18:34.667 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500309 Count: 1
12:18:37.681 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500312 Count: 1
12:18:40.695 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500315 Count: 1
12:18:43.676 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500318 Count: 1
12:18:46.690 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500321 Count: 1
12:18:49.704 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500324 Count: 1
12:18:52.685 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500327 Count: 1
12:18:55.700 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500330 Count: 1
12:18:58.714 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500333 Count: 1
12:19:01.695 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500336 Count: 1
12:19:04.709 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500339 Count: 1
12:19:07.690 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500342 Count: 1
12:19:10.704 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500345 Count: 1
12:19:13.718 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500348 Count: 1
12:19:16.699 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500351 Count: 1
12:19:19.713 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500354 Count: 1
12:19:22.726 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500357 Count: 1
12:19:25.707 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500360 Count: 1
12:19:28.721 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500363 Count: 1
12:19:31.735 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500366 Count: 1
12:19:34.716 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500369 Count: 1
12:19:37.730 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500372 Count: 1
12:19:40.744 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500375 Count: 1
12:19:43.725 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500378 Count: 1
12:19:46.739 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500381 Count: 1
12:19:49.720 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500384 Count: 1
12:19:52.734 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500387 Count: 1
12:19:55.748 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500390 Count: 1
12:19:58.730 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500393 Count: 1
12:20:01.743 -> Day with DEC: 27 Day no DEC: 27 UnixTime: 1677500396 Count: 1

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

I usually leave it about 24h. It tends to happen towards the latter quarter time wise.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTCLib.h DS3231 Clock Issue

Post by adafruit_support_carter »

Can't recreate this issue locally. Tested with a DS3231 connected to an UNO running the sketch above. Left sketch running overnight. It never had an issue reading the RTC and reporting the date/time info.

Check your wiring. If you have a second DS3231 module, see if you can recreate the same issue with that one.

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

I had it running but the PC rebooted so have to start again. Will let you know late tomorrow.
The only change I have made is to remove the cell battery from the RTC module. I did have a non-rechargeable Cell battery in the unit when it should have been rechargeable.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: RTCLib.h DS3231 Clock Issue

Post by mikeysklar »

These DS3231 modules use a lithium coincell CR1220 that is not rechargeable.

https://www.adafruit.com/product/380

What had you been using for a battery?

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

It is a CR2032 3v battery. From what I have read here https://lastminuteengineers.com/ds3231- ... =cmp-true if you don't use a rechargeable battery you need to remove a resistor on the module. If you scroll down to the technical specifications header you will see the note.

To that note I have just run the unit for 24 hours again (without a battery installed) and have the same issue still with respect to the clock jumping back randomly in time due to that bit flip.

I will shorten the wires to the unit and try again just to minimize the chance of any noise/pickup but i am not sure this is the issue.

All I can do after that is order another unit and see if it has the same problem.

User avatar
IFoundABug
 
Posts: 8
Joined: Fri Feb 24, 2023 3:02 pm

Re: RTCLib.h DS3231 Clock Issue

Post by IFoundABug »

Hi. I have run the RTC module for 24h with the shorter wires (by half) and the issue no longer occurs. I will keep it running over the weekend and confirm Monday but the indications are that it was due to the wire length. The repeatability of the issue would suggest that it was not noise but perhaps an issue with the clock and signal edge detection.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: RTCLib.h DS3231 Clock Issue

Post by mikeysklar »

Thank you for the followup on this. Glad to hear rewiring with shorter wires seems to have resolved the issue.

Locked
Please be positive and constructive with your questions and comments.

Return to “Clock Kits (discontinued)”