RTC Script works once every 24hrs - Not hourly

Hi Everyone,

I’ve looked through the forum for similar issues and tried their fixes to my code but unfortunately it hasn’t worked. Rather than hijacking a thread I wanted to start my own fresh one.

I have an hobby Aquaponics garden being built. I’d like to automate it with Arduino.

I’m having issues with my rtc script. Works perfectly for one go/cycle when reset but then won’t continue on the hourly loop.

Here it is below. Any help is greatly appreciated. Once other sensors are added and working well I plan to share my build with other aquaponic members, in the spirit of open source sharing.

int ch = 4;// number of relays
int relay={2,3,4,5}; // Arduino pin numbers
int distance;

#include “RTClib.h”
#include <DS1302RTC.h>
#include <Time.h>
#include <TimeLib.h>

DS1302RTC RTC( 7, 8, 9 );// CE reset - I/O Data - Clock

void setup() {

Serial.begin(9600);// Serial monitor start
// set pins as output
time_t myTime;
myTime = RTC.get();

setTime(19, 29, 55, 14, 12, 2018);


pinMode(relay[0], OUTPUT);// connected to relay1
digitalWrite(relay[0], HIGH); // Turn the relay OFF

pinMode(relay[1], OUTPUT);// connected to relay2
digitalWrite(relay[1], HIGH); // Turn the relay OFF

pinMode(relay[2], OUTPUT);// connected to relay2
digitalWrite(relay[2], HIGH); // Turn the relay OFF

pinMode(relay[3], OUTPUT);// connected to relay2
digitalWrite(relay[3], HIGH); // Turn the relay OFF


void loop() {

tmElements_t tm;

if(tm.Minute == 30)
if(tm.Second == 00) {
digitalWrite(relay[2], LOW); Serial.println(“Tower On”);
if(tm.Minute == 30)
if(tm.Second == 10) {
digitalWrite(relay[2], HIGH); Serial.println(“Tower Off”);

if(tm.Minute == 30)
if(tm.Second == 00) {
digitalWrite(relay[3], LOW); Serial.println(“Bed On”);
if(tm.Minute == 35) {
digitalWrite(relay[3], HIGH); Serial.println(“Bed Off”);


}// loop

What are your Serial print statements showing? Do they show the exact time (minutes and seconds) required to trigger the various events? With that 5 second delay I can imagine a time walk-off since Arduino system clock-based time is asynchronous with the RTC.

Hmm I see what you mean. Would you recommend shorting it to 500ms?

I'm at a loss to see how I add an image to this post to show the Serial monitor results.

Use the code tags button </> and copy/paste the serial monitor contents into it. Use the A- button to remove any formatting, if you post and see it end up with a bunch of tags stuck in.

I went out and tested it. Changed the script to put the tower pump on the 10th second of every minute and set the delay to 500ms. Worked like a charm.

ONLY thing is now it's affected my Ultrasonic Sensor SR04T-2.0. The 5sec delay was to minimise false reading. Any quicker than 5 seconds makes the sump pump turn on and off rapidly.

The three fixes I can think of are

  1. Modify the script with an average of measurement over the course 5 seconds (not sure how to write that formula into the code).
  2. Single out the ultrasonic sensor and only delay it's reading by 5 seconds (not sure if you can set two or more delays in the one script)
  3. Build a Stilling Well out of a pvc pipe and install it in the tank (The Art of the Stilling Well | APG) to minimise false readings caused by the incoming water filling the tank.