RTC Issue Losing Time

We are trying to automatically control a Chicken Coop Door, and using an Arduino Uno and RTC to do so. I have two codes. I upload Step_One first, then upload the code for the actual chicken coop. We are testing the setup by changing the times the actuator should open and closed, and testing this only works when connected to the computer. If we have the Arduino running from our external battery, it appears the RTC is resetting the time, but unsure on this. Below are the two codes I am running.

/*----------------------------------------------------------------------*
 * SetRTCtoUTC
 *
 * Adapted from WorldClock example from the Timezone library examples.
 *
 *----------------------------------------------------------------------*/

#include <DS1307RTC.h>
#include <Wire.h>
#include <Time.h>        //http://www.arduino.cc/playground/Code/Time
#include <TimeLib.h>
#include <Timezone.h>    //https://github.com/JChristensen/Timezone

#pragma GCC diagnostic ignored "-Wwrite-strings"

TimeChangeRule usEDT = {"EDT", First, Sun, Nov, 2, -240};  //Eastern Daylight Time = UTC - 4 hours
TimeChangeRule usEST = {"EST", Third, Sun, Nov, 2, -300};   //Eastern Standard Time = UTC - 5 hours
Timezone usET(usEDT, usEST);

tmElements_t tm;

TimeChangeRule *tcr;        // pointer to the time change rule, use to get the TZ abbrev
time_t utc;

void setup(void)
{
    Serial.begin(9600);
    setTime(usET.toUTC(compileTime()));
    utc = now();
    RTC.set(utc);
    
}

void loop(void)
{
    Serial.println();
    utc = now();
    time_t local = usET.toLocal(utc, &tcr);
    Serial.print("Is DST in effect: ");
    Serial.println(usET.locIsDST(local));
    
    printTime(utc, "UTC", " Universal Coordinated Time");
    printTime(usET.toLocal(utc, &tcr), tcr -> abbrev, " New York");
    delay(10000);
}

//Function to return the compile date and time as a time_t value
time_t compileTime(void)
{
#define FUDGE 25        //fudge factor to allow for compile time (seconds, YMMV)

    char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
    char chMon[3], *m;
    int d, y;
    tmElements_t tm;
    time_t t;

    strncpy(chMon, compDate, 3);
    chMon[3] = '\0';
    m = strstr(months, chMon);
    tm.Month = ((m - months) / 3 + 1);

    tm.Day = atoi(compDate + 4);
    tm.Year = atoi(compDate + 7) - 1970;
    tm.Hour = atoi(compTime);
    tm.Minute = atoi(compTime + 3);
    tm.Second = atoi(compTime + 6);
    t = makeTime(tm);
    return t + FUDGE;        //add fudge factor to allow for compile time
}

//Function to print time with time zone
void printTime(time_t t, char *tz, char *loc)
{
    sPrintI00(hour(t));
    sPrintDigits(minute(t));
    sPrintDigits(second(t));
    Serial.print(' ');
    Serial.print(dayShortStr(weekday(t)));
    Serial.print(' ');
    sPrintI00(day(t));
    Serial.print(' ');
    Serial.print(monthShortStr(month(t)));
    Serial.print(' ');
    Serial.print(year(t));
    Serial.print(' ');
    Serial.print(tz);
    Serial.print(' ');
    Serial.print(loc);
    Serial.println();
}

//Print an integer in "00" format (with leading zero).
//Input value assumed to be between 0 and 99.
void sPrintI00(int val)
{
    if (val < 10) Serial.print('0');
    Serial.print(val, DEC);
    return;
}

//Print an integer in ":00" format (with leading zero).
//Input value assumed to be between 0 and 99.
void sPrintDigits(int val)
{
    Serial.print(':');
    if(val < 10) Serial.print('0');
    Serial.print(val, DEC);
}

Edit, second code is too many characters, therefore uploaded it as an attachment instead.

ChickenDoorEasternTime_Zone.ino (14.1 KB)

From your selection of pins I guess you're using a Micro or Leonardo for this. If I guess additionally that you still use the DEBUG option, this code is compiled in:

    // Start the serial port
    Serial.begin(9600);
    while (!Serial)
      ;
    delay(200);

This will wait until the serial port on the PC is opened and not continue before it did.

if you are using a DS1307, that is the problem. replace it with a DS3231, which works fine with the DS1307 library,

for a few dollars more, add a GPS and make a GPS disciplined clock: the GPS sets the time, the RTC fills in the occasional gap if the GPS loses lock

if you are using a DS1307, that is the problem. replace it with a DS3231, which works fine with the DS1307 library,

Please explain, why the chosen chip is the problem. The DS1307 only makes problems if the “5V” voltage on the Arduino is too low compared to the backup battery voltage.

I am using a Diymore DS3231, so that should'nt be the issue. My thought is every time the Arduino power cycles, it resets to the compile time. I see on other RTC sketches you comment out a section and reupload to keep this from happening. But I am unable to figure out what portion I would comment out.

We are using an Arduino Uno. Could you clarify on the portion about the Serial Port?

pylon:
From your selection of pins I guess you're using a Micro or Leonardo for this. If I guess additionally that you still use the DEBUG option, this code is compiled in:

    // Start the serial port

Serial.begin(9600);
   while (!Serial)
     ;
   delay(200);




This will wait until the serial port on the PC is opened and not continue before it did.

My thought is every time the Arduino power cycles, it resets to the compile time. I see on other RTC sketches you comment out a section and reupload to keep this from happening. But I am unable to figure out what portion I would comment out.

I don't think so, given you uploaded the first sketch (inline in post) just once to set the RTC time and the uploaded the second sketch (attached to post). That sketch does not set the time again.

May be a stupid question but I ask anyway: Does your RTC have a backup battery? Does that battery provide more than 3V?

Yes we have an operational battery in the clock.

pylon:
I don't think so, given you uploaded the first sketch (inline in post) just once to set the RTC time and the uploaded the second sketch (attached to post). That sketch does not set the time again.

May be a stupid question but I ask anyway: Does your RTC have a backup battery? Does that battery provide more than 3V?

This is our setup incase yall need that.

What voltage is that “Battery Backup”?

We have a 12 Volt Battery powering the actuator through the relay board, and then the backup for the arduino is a 9V battery connected through the DC Power Jack. Once the USB is unplugged to test to see if the actuator will move at the set time, it does not. But once that time has passed, as soon as i replug in the USB, the actuator then moves.

We have a 12 Volt Battery powering the actuator through the relay board, and then the backup for the arduino is a 9V battery connected through the DC Power Jack.

What tyype of 9V battery? The small block type 9V batteries tend to fall in voltage very fast. Why don't you power the Arduino from the 12V as the relays?

If the voltage on the DC power jack falls below about 7V the internal voltage drops to low to read the RTC. Check the voltage between the 5V and GND pins on the Arduino while running on battery. This must be at least 5V.

We initially tried a DC power jack soldered to the positive and negative wires coming off our 12 Volt battery, but found out I guess we overloaded the Arduino and blew the voltage regulator. We had to get a new arduino. How would you suggest I wire this up? I believe you are right that the 9V is dropping in voltage though, so if you are able to guide me on how I should wire up the DC power jack to the 12 volt I think we should be good to go.

pylon:
What tyype of 9V battery? The small block type 9V batteries tend to fall in voltage very fast. Why don’t you power the Arduino from the 12V as the relays?

If the voltage on the DC power jack falls below about 7V the internal voltage drops to low to read the RTC. Check the voltage between the 5V and GND pins on the Arduino while running on battery. This must be at least 5V.

Yes, connecting 12V to the Arduino will often cause the built in regulator to overheat, especially if you are trying to use the Arduino to power other modules or several LEDs.

Use a separate step down switching regulator from the 12V supply, to provide either 7 to 9V to the Arduino barrel jack (RAW input), or 5V to the Vcc pin.

Pololu has a great selection.

Would this do the trick?

If so which Amps?

jremington:
Yes, connecting 12V to the Arduino will often cause the built in regulator to overheat, especially if you are trying to use the Arduino to power other modules or several LEDs.

Use a separate step down switching regulator from the 12V supply, to provide either 7 to 9V to the Arduino barrel jack (RAW input), or 5V to the Vcc pin.

Pololu has a great selection.

Would this do the trick?

It can but it is a complete overkill. Such a DC/DC converter will do the job and is quite a bit cheaper.

I would regulate it to 5V and then power the Arduino by the 5V pin as this DC/DC converter is much more efficient than the onboard regulator of the Arduino.

So some big issues. We installed the regulator and everything was working perfectly for 24 hours, and now everything is acting up. The actuator is moving at random times, the RTC seems to be fried, and no longer shows up in the serial monitor showing the time, and the regulator is flickering power on and off. I am attempting to upload a video but have difficulty. Here is a picture. Any Ideas?

Terrible wiring.

So would you say that is the issue? Or could the issue be the battery isn’t powerful enough to power the actuator and our arduino and relay board? We will redo all wiring if that is the case, and any advise would be appreciated