Simple time RTC based relay stops after a while

I have a very simple RTC circuit based on ds1307 attached to ATMEGA328 .
I have bootloaded this chip with external crystal and have let it run by itself on a circuit.

I have a relay that switched on during day time and switched off at night..

All seems to work well for like 1-2 days then somehow the whole thing stops working until i power cycle it.
I have powered arduino using a voltage regulated 12V wallwart and i have a voltage regulator IC as well to bring the 12v down to 5 on the circuit and use the same wallwart to energize the relay.

I am sure the 12V regulator has its own IC/Caps, etc

And the power comes to this setup from my Inverter and during the grid failure which happen a lot of time lol.. there is always a very slight power blink or something. Caps should take care of it as it is very very short.
My computers do not stop running during this break .

So its either the RTC that stops giving data to the atmega OR the whole mega crashes and requires a reboot.

RTC time is correct because when i reset the arduino once in a while, it just works

Also I feel like an Atmega is an overkill for a silly task, I m going to soon use an attiny84 for this but didnt find any readymade RTC code that works with 84 yet.

Is arduino dependable to run for like months without any action from the user? because if i plan to use this system to be able to run some important stuff like street lamps, and want to know if this is good enough to run for years without power cycles,etc or there are any special things to note before doing a system that is

Sorry if i sound silly, I m not an expert in electronics more like a noob, Im a software person and do this all at night for fun.
I know the word arduino like exactly some 50 days ago and have no other background .

Here is the code, I don't know if there is a problem with code here.

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int relayPin = 8; // select the pin for the LED

void loop() {

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

printtime();
if(!isday())
{
Serial.write("Its night time");
digitalWrite(relayPin, LOW);
}
else
{
Serial.write("Day time");
digitalWrite(relayPin, HIGH);
}

delay(1000);
}

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}

boolean isday()
{
if((hour>4) && (hour<17))
{
return true;
}
return false;
}

void printtime()
{
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day_of_week:");
Serial.println(dayOfWeek, DEC);
}

It should run for years.
The only thing you have to keep in mind is a rollover of millis() and micros().

Please upload the whole sketch between the code tags.
Above the text input field are buttons. The '#'-button is for the code tags.

The I2C request to the DS1307 does not return an error.

A computer has capacitors in the power supply, perhaps enough for half a second or even more if the computer is not very busy.
A cheap wall wart could perhaps not handle a power failure. An inverter has usualy only small capacitors.

Some older or cheap capacitors have a high impedance, and don't do much.

How is your reset circuit ? With a 10k resistor ? You might need a lower value. I have had circuits that resets the microcontroller with a power glitch, because some kind of electric noise traveled to the reset pin.

What I am saying is: we need more information. If possible, your whole sketch, the schematics, and perhaps photos.

Hi Thanks for the reply.

Regarding the reset, Yes I used 10k resistor .

You said you had circuits that resets the microcontroller on power glitch , I think that would just restart the sketch ?

for me it seems to just hang.. can i try a lower value like 1k?

I will soon give you the whole sketch and photos .

same problem here to me... please, I suspect the same issue. When I switch off the relay, sometimes, it stops rtc oscillator, and serial monitor.
http://arduino.cc/forum/index.php/topic,161087.0.html

Hi!

I have a problem with one of my DS1307. The RTC will stop randomly. If you google you see a lot of people have the same problem.
I think the problem could be either a bad connection or faulty chip. Also check the battery.

Because I am to lazy to fix the hardware I have made a software fix :slight_smile:

        //Fix for bug
        //Sometimes oscillator is disabled
        //"Bit 7 of register 0 is the clock halt (CH) bit. 
        //When 0 the oscillator is enabled."
        //set_time sets bit 7 to 0 (starts oscillator again)
        get_time();
        if (secondsPrev==seconds)
        {
          //Sets bit 7 to 0
          seconds++;
          set_time();
        }
        else
        {
          secondsPrev=seconds;
        }