time come earlier after 4 days rtc DS1302

hi guys

i wonder if you help me

i used nano arduino, rtc DS1302, relay. So, after 4 day the time come different by 1 minutes earlier than the real time i mean the relay shuold be on the same time every day, could you help me ?

the code i used :

#include <DS1302.h>

DS1302 rtc(A0, A1, A2);

Time t;

const int Sun = SUNDAY ;

const int Mon = MONDAY ;

const int Tue = TUESDAY ;

const int Wed = WEDNESDAY;

const int Thu = THURSDAY ;

const int Sat = SATURDAY ;

const int Fri = FRIDAY ;

const int OnHour1 = 13 ;

const int OnMin1 = 20 ;

const int OffHour1 = 13 ; 

const int OffMin1 = 33; 

const int Relay1 = 2;

void setup()



rtc.setDOW(TUESDAY);// Set Day-of-Week to FRIDAY
rtc.setTime(17, 41, 00);  // Set the time to 12:00:00 (24hr format)
rtc.setDate(31, 12, 2019); // Set the date to August 6th, 2010

void loop()


t = rtc.getTime();

Serial.print(" ");

Serial.print(" -- ");


delay (1000);

pinMode(Relay1, OUTPUT);

digitalWrite(Relay1, HIGH);

if( (t.dow == Sat || t.dow == Mon || t.dow == Thu ) && t.hour == OnHour1 && t.min >= OnMin1 && t.min < OffMin1 ){
Serial.println("LIGHT ON_1");
else if((t.dow == Sat || t.dow == Mon || t.dow == Thu ) && t.hour == OffHour1 && t.min == OffMin1){
Serial.println("LIGHT OFF_1");


where is my mistake please ?

warm regards

I seem to remember that a DS1302 is a poor choice for accurate time keeping and that a DS3231 would be better.

The best solution would be to get the time from the internet and sync the RTC when network connection is available.

1 minute off in 4 days is an error of about 170ppm, which is way of the specs if you followed the guidelines about crystals by maxim and kept the temperature constant.

As the DS1302 is not temperature compensated (the DS3231 is), a temperature change is a probable source of your problem. Another one is a bad shielding from the relay magnetic field. The good news is that your code is most probably not responsible for the problem.
If you need more accuracy, build your board according the guidelines of maxim, choose an appropriate crystal and keep the temperature constant at 25°C. Or buy a better RTC.

NEXT TIME, put your code in .
You’ll get more friends.
Read the HOW-TO POST thread at the top of each section.

NEXT TIME, put your code in .

No, do it now!

You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post them) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can as you now see, be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.