TinyRTC drift

Hello,

i've read a lot in this forum and found a lot of guidance for my projects. Now I started a new project - a time-based switching for 433MHz Power-Switches. I'm using the TinyRTC for the timing of my software. Unfortunately I recognized a time drift of about 2,5h in 24h runtime. The time is running to slow - so in real world it is 6pm and on my arduino it is 4pm....... How can I fix this? Has this someting to do with the Delay Command used in my Code?

Here is my Code

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// 2010-02-04 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

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

RTC_DS1307 RTC;
RCSwitch mySwitch = RCSwitch();

/*****************************************************************************
 Anzahl der Schaltzeiten
*****************************************************************************/
const int intSwitchTimesCount = 7;
char SystemCode[6] = "10101"; 

/*****************************************************************************
 Array mit Schaltzeiten
 { [Tag der Woche], [Stunde], [Minute], [Steckdose], [On (1), Off (0)] }
*****************************************************************************/
typedef struct
 {
     int Day;
     int Hour;
     int Minute;
     char SwitchAddress[6];
     int SwitchAction;
 }  switchTimes;

switchTimes mySwitchTimes[intSwitchTimesCount] = {
      {0,17,1,"00010",1},
      {0,18,55,"00010",1},
      {0,22,1,"00010",0},
      {0,17,1,"01000",1},
      {0,18,55,"01000",1},
      {0,22,1,"01000",0}
    };


/*****************************************************************************
 setup()
*****************************************************************************/
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
    
    // Transmitter is connected to Arduino Pin #10  
    mySwitch.enableTransmit(10);
    
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__)); 
}

/*****************************************************************************
 loop()
*****************************************************************************/

void loop () {
    DateTime now = RTC.now();
    
    Serial.print("Now: ");
    Serial.print(now.hour());
    Serial.print(":");
    Serial.print(now.minute());
    Serial.println();
    
    //Durch Schaltzeiten Array loopen
    for (int x=0; x<intSwitchTimesCount; x++) 
    {

      
      Serial.print("Tag: ");
      Serial.print(mySwitchTimes[x].Day, DEC);
      Serial.print(" ");
      Serial.print(mySwitchTimes[x].Hour, DEC);    
      Serial.print(":");
      Serial.print(mySwitchTimes[x].Minute, DEC);    
      Serial.print(" Address: ");
      Serial.print(mySwitchTimes[x].SwitchAddress);
      Serial.print(" Action: ");
      Serial.print(mySwitchTimes[x].SwitchAction, DEC);
      Serial.println("");
      
      // Check if a Switch Action exists for current day
      // Switch Day 0 = every day
      if (mySwitchTimes[x].Day == now.dayOfWeek() || mySwitchTimes[x].Day == 0)
      {
        if (mySwitchTimes[x].Hour == now.hour() && mySwitchTimes[x].Minute == now.minute())
        {
          // Switch Action -> SwitchOn
          if (mySwitchTimes[x].SwitchAction == 1)
          {
              Serial.println("On");
              mySwitch.switchOn(SystemCode, mySwitchTimes[x].SwitchAddress);      
          }
          
          // Switch Action -> SwitchOff
          if (mySwitchTimes[x].SwitchAction == 0)
          {
            Serial.println("Off");
            mySwitch.switchOff(SystemCode, mySwitchTimes[x].SwitchAddress); 
          }
        }
      }
      
      Serial.println("--------------------------");
    }
    
    
    Serial.println();
    delay(60000);
    
}

Assuming this is not just a fixed offset because you didn't set the time on the Rtc ever.

Then the Rtc is probably defective

Normally the Rtc is much much more acurate.

You can do minor calibration e.g. See

However 2.5 hours per day sounds like a serious fault with the crystal

2.5 hours per day

That's not a RTC :astonished:

I just did some further tests and it seems that the drift only occurs if the arduino is powered with 5V via the power jack.
If arduino uno is powered with 5v on the USB Port (even with the same Power-Supply) the drift did not occur within the last hours....

can anybody tell my why?

Hi, you should not power the Arduino via the power jack if using 5V. You need at least 6.5V to power via the jack, otherwise the Arduino and other components will get significantly less than 5V.

If your 5V supply is regulated/switch-mode, you can connect directly to the 5V connector on the Arduino, bypassing the regulator (which is what the jack connects to).

Paul

But would that account for the RTC drift?

seems so - after additional 8 hours being powered with the usb-port connected to a regulated 5V supply no time drift is occuring...
I will test tomorrow with a higher voltage on the power jack.

The Tiny RTC module uses a DS1307 RTC. It counts but can't be read, when powered by either battery or low power.

I am not sure that would lead to drift unless you have a battery problem in the RTC. (It should keep time on the battery and be accurate when read at full power).

There are a number of potential problems in using the DS1307 with a substandard power supply, which would be the case if the voltage input to the Arduino barrel jack is lower than acceptable for the voltage regulator.

There are comments in the data sheet relevant to this.

First, the DS1307 crystal oscillator is susceptible to power supply noise, which would be greatly increased if the Arduino voltage regulator is not operating properly.

Second, the device switches between the backup battery and the power supply, which would take place at about 3.75 V, and could be quite frequent with a substandard power supply. As noted in the data sheet:

When VCC falls below 1.25 x VBAT, the device terminates an access in progress and resets the device address counter. Inputs to the device will not be recognized at this time to prevent erroneous data from being written to the device from an out-of-tolerance system. When VCC falls below VBAT, the device switches into a low-current battery-backup mode. Upon power-up, the device switches from battery to VCC when VCC is greater than VBAT +0.2V and recognizes inputs when VCC is greater than 1.25 x VBAT.

All of this is consistent with satisfactory operation on USB power.