arduino-libraries / RTCZero

RTC Library for SAMD21 based boards
http://arduino.cc/en/Reference/RTC
GNU Lesser General Public License v2.1
76 stars 79 forks source link

Time in the future (Arduino NANO 33 IoT) #88

Open mklemarczyk opened 2 years ago

mklemarczyk commented 2 years ago

Hello, I have very particular issue when using the RTCZero with my Arduino NANO 33 IoT. I am working with WiFi, BT and time at the same time to control light strength in setup of LED and movement sensor.

I found your library to exploit the internal RTC clock. I set it from NTP server using WiFi. After that I shut down the WiFi and move to BT to expose the values for diagnostic.

After many tries without your library, my time was decaying and getting more and more late. With your library I have totally opposite experience. The time still desynchronize, in that case into the future. At beginning the minutes, hours, days in the future from real time.

Do you have any idea what can cause such behavior? I set time once, and after I read it only.

mklemarczyk commented 2 years ago

Hello I made simple test, even without using BLE and WiFi the time shifts very much. How I can use it as alarm to wake me up in the morning if the time is so irrelevant after longer period of time ?

I expect that time will fluctuate but the difference in real time (my PC) and RTC time (Arduino NANO 33 IoT) will stay stable (not exceeding +/- 1 second).

For the experiment the Arduino was connected to the PC and no other I/Os were used. The time difference grown in 30 min to 15 seconds (0.8 % degradation).

Real time   ->  RTC date    RTC time    millis  micros
13:10:48.899    ->  24/06/22    16:00:03    3202    3202622
13:24:24.677    ->  24/06/22    16:13:46    819009  819010002
13:31:57.135    ->  24/06/22    16:21:22    1271482 1271482659
13:35:42.867    ->  24/06/22    16:25:10    1497168 1497168861
13:39:35.746    ->  24/06/22    16:29:05    1730078 1730079079
/*
  Simple RTC for Arduino Zero and MKR1000

  Demonstrates the use of the RTC library for the Arduino Zero and MKR1000

  This example code is in the public domain

  http://arduino.cc/en/Tutorial/SimpleRTC

  created by Arturo Guadalupi <a.guadalupi@arduino.cc>
  15 Jun 2015
  modified 
  18 Feb 2016
  modified by Andrea Richetta <a.richetta@arduino.cc>
  24 Aug 2016
*/

#include <RTCZero.h>

/* Create an rtc object */
RTCZero rtc;

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 0;
const byte hours = 16;

/* Change these values to set the current initial date */
const byte day = 24;
const byte month = 6;
const byte year = 22;

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

  rtc.begin(); // initialize RTC

  // Set the time
  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  // Set the date
  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);

  // you can use also
  //rtc.setTime(hours, minutes, seconds);
  //rtc.setDate(day, month, year);
}

void loop()
{
  // Print date...
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");

  // ...and time
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());

  // ... and ticks
  Serial.print(" ");
  Serial.print(millis());

  Serial.print(" ");
  Serial.print(micros());

  Serial.println();

  delay(1000);
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0"); // print a 0 before if the number is < than 10
  }
  Serial.print(number);
}

Used versions:

mklemarczyk commented 2 years ago

Test with new Arduino NANO 33 IoT. Distribution from RS components (https://uk.rs-online.com/web/p/arduino/1927585).

Real time   ->  RTC date    RTC time    millis  micros
13:50:30.343    ->  24/06/22    16:00:01    1131    1132026
13:52:21.152    ->  24/06/22    16:01:52    111942  111943175
13:52:51.171    ->  24/06/22    16:02:22    141967  141967440
13:56:29.714    ->  24/06/22    16:06:02    360519  360519603
13:59:05.125    ->  24/06/22    16:08:38    515950  515950645
14:02:14.567    ->  24/06/22    16:11:48    705385  705385588
14:07:20.975    ->  24/06/22    16:16:57    1011801 1011802306
14:10:03.552    ->  24/06/22    16:19:40    1174382 1174382463
14:12:35.775    ->  24/06/22    16:22:13    1326637 1326637858
14:13:08.921    ->  24/06/22    16:22:46    1359761 1359761607
14:13:09.959    ->  24/06/22    16:22:48    1360796 1360796419
14:13:10.961    ->  24/06/22    16:22:49    1361831 1361831234
14:13:12.013    ->  24/06/22    16:22:50    1362865 1362865680
14:13:13.025    ->  24/06/22    16:22:51    1363900 1363900352
14:13:14.116    ->  24/06/22    16:22:52    1364934 1364935099
14:13:15.099    ->  24/06/22    16:22:53    1365969 1365970108
14:18:19.771    ->  24/06/22    16:27:59    1670592 1670593184
14:22:23.251    ->  24/06/22    16:32:04    1914118 1914119025