The sketch stops working after a few weeks

Hello all,

As I am totally a beginner, I wanted to apply a ready-made project from the internet to turn on/off one or more lights at specified times using a relay module. I used the code given below, which I found on Youtube (link) and modified it in an amateur way just by copying a few lines to control a second channel of the relay.
I am using a DS3231 RTC Module and a 4-channel relay module which are powered by the 5V output of the board.
The problem is it works as it should for a few weeks (unfortunately I don’t know exactly how long) and then stops working (the lights stay either on or off constantly) until I disconnect it from the power (5V USB charger) and upload the sketch again. The error used to occur also before I added the lines for the second relay channel.
After reading some posts about similar errors and some tips I think it might be a result of the memory getting full but I don’t know what I should change.
I would appreciate any help

#include <DS3231.h>

int Relay = 4;
int Relay2 = 2;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 7; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 00;
const int OffHour = 13; //SET TIME TO OFF RELAY
const int OffMin = 00;

const int OnHour2 = 6; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin2 = 00;
const int OffHour2 = 21; //SET TIME TO OFF RELAY
const int OffMin2 = 00;

void setup() {
  pinMode(Relay, OUTPUT);
  pinMode(Relay2, OUTPUT);
  digitalWrite(Relay, HIGH);
  digitalWrite(Relay2, HIGH);

void loop() {
  t = rtc.getTime();
  Serial.print(" hour(s), ");
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);
  if(t.hour == OnHour && t.min == OnMin){
    Serial.println("LIGHT ON");
    else if(t.hour == OffHour && t.min == OffMin){
      Serial.println("LIGHT OFF");

  if(t.hour == OnHour2 && t.min == OnMin2){
    Serial.println("LIGHT ON");
    else if(t.hour == OffHour2 && t.min == OffMin2){
      Serial.println("LIGHT OFF");

The original code I downloaded and the DS3231 library used in the code are attached

Specific_Time_Trigger_Relay.ino (847 Bytes)

DS3231.h (3.04 KB)

What kind of load are the relays switching?

Nothing in the code suggests a memory problem, so, as suggested above, power fluctuations are suspect.

Post a wiring diagram and identify all the components (links to product pages, etc.).

If the board has an on board LED, you might toggle it on/off in the loop so you can monitor the board and see if the code is still running in its loop when the lights fail to operate.

Thanks for the replies.
I first made the setup according to the scheme in the attachment. But I used a 4-channel relay which I bought from the following link:
Relay module

DS3231 RTC module

I should also mention that my board is a third party board equivalent to UNO R3:

IN1 of the relay is connected to digital pin 4 as in the scheme. It controls a 220V 10W led bulb.

Then I added a second lamp to the 2nd channel of the relay module, which is only a 5V USB lamp connected to a USB charger. The relay turns on/off the 5V output from the charger.

I will read about and try adding the led on/off into the loop. But I will have to wait until the error occurs again to see how it behaves.

How is your project being powered, and if it is being powered from an AC source, what protection do you have for a momentary power interruption?

The board itself is connected to a USB charger with multiple outputs which I also use for charging my phone. Could it be the problem? I don’t have any components for voltage regulation/protection.

Yes, the USB charger with multiple outlets could be the issue. When you plug or unplug the phone it could be generating transients on the DC powering the Arduino.

I suggest you try a stand alone USB charger to power the Arduino. They are inexpensive and readily available on the Internet, perhaps friends, etc.

You might also add a large capacitor to the arduino 5v rail to help survive a short power interruption.