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.
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.
// 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);
}
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
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.
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
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.
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.
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.
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.