RTC clock not work on Arduino (water irrigation system.)

I working with a water irrigation system.

The problem is the clock, the clock will not automatically sets up from pc

  // RTC initialization
  RTC.begin();
  if (!RTC.isrunning()){
    // date and time adjust as the PC computer date and time
    RTC.adjust(DateTime(__DATE__, __TIME__));

This not work on arduino. Think code is from other systeam

The code:

/*
#####################################################################################
#  	File:               Arduino_Watering_sustem.ino                                             
#       Processor:          Arduino UNO, MEGA ou Teensy++ 2.0      
#  	Language:	    Wiring / C /Processing /Fritzing / Arduino IDE          
#						
#	Objectives:         Watering System - Irrigation
#										  
#	Behavior:	    When the soil is dry,
#                           
#
#			
#   Author:                 Marcelo Moraes 
#   Date:                   12/10/12	
#   place:                  Brazil, Sorocaba City	
#					
#####################################################################################
 
  This project contains public domain code.
  The modification is allowed without notice.
  
 */

// libraries definition
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"



// frequency musical notes
#define NOTE_C6  1047
#define NOTE_C3  131
#define NOTE_G3  196

// pins definition
int levelSensorPin = 0;
int moistureSensorPin = 1;
int audioPin = 2;
int soggyLEDPin = 3;
int moistsoilLEDPin = 4;
int drysoilLEDPin = 5;
int pumpLEDPin = 6;
int pumpPin = 7;

// variables
int levelSensorValue;       // stores the level sensor values
int moistureSensorValue;    // stores the moisture sensor values
int j = 0;

// system messages
const char *string_table[] =
{   
  "     Welcome! =)",
  "   Tank LOW level",
  "      Dry soil",
  "     Moist soil",
  "     Soggy soil",
  "The water pump is on",
  "   ArduinoByMyself",
  "   Watering System",
  "    Please wait!"
};

// objects definition
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27,20,4);


void setup(){
  // serial initialization
  Serial.begin(9600);
  
  // LCD initialization
  lcd.init();          
  lcd.backlight();     // with Backlight
  lcd.clear();         // clearscreen
  
  // Wire initialization
  Wire.begin();
  
  // RTC initialization
  RTC.begin();
  if (!RTC.isrunning()){
    // date and time adjust as the PC computer date and time
    RTC.adjust(DateTime(__DATE__, __TIME__)); 
  }
  
  // Arduino pins initalization
  pinMode(audioPin, OUTPUT);
  pinMode(soggyLEDPin, OUTPUT);
  pinMode(moistsoilLEDPin,OUTPUT);
  pinMode(drysoilLEDPin,OUTPUT);
  pinMode(pumpLEDPin,OUTPUT);
  pinMode(pumpPin,OUTPUT);
  
  // LCD initial messages
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(string_table[6]); 
  lcd.setCursor(0,1);
  lcd.print(string_table[7]); 
  lcd.setCursor(0,3);
  lcd.print(string_table[0]); 
  // initialization delay
  delay(5000); 
}


void loop(){
  
  
  // RTC parameters definition
  DateTime myRTC = RTC.now();
  int H = myRTC.hour();
  int M = myRTC.minute();
  int S = myRTC.second();
  
  // call Clock Function
  //RightHour();
  
  // reads the sensors
  levelSensorValue = analogRead(levelSensorPin);
  moistureSensorValue = analogRead(moistureSensorPin);
  
  // if low water level: plays the low level alarm
  if(levelSensorValue > 600){
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[1]); 
    // plays the alarm sound
    for(int i=0;i<2;i++){
      tone(audioPin, NOTE_G3, 200);
      delay(200);
      tone(audioPin, NOTE_C3, 200);
      delay(200);
      noTone(audioPin);
    }
  }
  
  // check the moisture range
  if(moistureSensorValue >= 700){
    // in case of dry soil:
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[2]);
    // lights up the correct LED
    digitalWrite(drysoilLEDPin,HIGH);
    digitalWrite(moistsoilLEDPin,LOW);
    digitalWrite(soggyLEDPin,LOW);
    // plays the alarm sound
    tone(audioPin, NOTE_C6, 100);
    delay(250);
    noTone(audioPin);
  }
  if((moistureSensorValue < 700) && (moistureSensorValue >= 300)){
    // in case of moist soil:
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[3]);
    // lights up the correct LED
    digitalWrite(drysoilLEDPin,LOW);
    digitalWrite(moistsoilLEDPin,HIGH);
    digitalWrite(soggyLEDPin,LOW);
    delay(250);
  }
  if(moistureSensorValue < 300){
    // in case of soggy soil:
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[4]); 
    // lights up the correct LED
    digitalWrite(drysoilLEDPin,LOW);
    digitalWrite(moistsoilLEDPin,LOW);
    digitalWrite(soggyLEDPin,HIGH);
    delay(100);
  }
  
  // if the soil is dry and if it is the right time: turn on the pump for 1 minute
  if((H == 16) && (M == 50) && (S == 00)){
    while(moistureSensorValue >= 700){
      // system messages
      lcd.clear();
      RightHour();
      lcd.setCursor(0,1);
      lcd.print(string_table[8]);
      lcd.setCursor(0,3);
      lcd.print(string_table[5]);
      // turn the pump on
      digitalWrite(pumpPin,HIGH);
      digitalWrite(pumpLEDPin,HIGH);
      delay(10000);
      // if the soil is not moist so far
      // reads the moisture sensor once more
      moistureSensorValue = analogRead(moistureSensorPin);
    }
    // turn the pump off
    digitalWrite(pumpPin,LOW);
    digitalWrite(pumpLEDPin,LOW);
  }
  
}

// Real Time Clock Function
void RightHour()
{
  DateTime Now = RTC.now();
  String clock_date = "  Date: ";
  String clock_hour = "   Time: ";
  
  int _day = Now.day();
  int _month = Now.month();
  int _year = Now.year();
  
  clock_date += fixZero(_day);
  clock_date += "/";
  clock_date += fixZero(_month);
  clock_date += "/";
  clock_date += _year;
  
  int _hour = Now.hour();
  int _minute = Now.minute();
  int _second = Now.second();
  
  clock_hour += fixZero(_hour);
  clock_hour += ":";
  clock_hour += fixZero(_minute);
  clock_hour += ":";
  clock_hour += fixZero(_second);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(clock_date);
  lcd.setCursor(0, 1);
  lcd.print(clock_hour);
  
  delay(500);
}


String fixZero(int i)
{
  String ret;
  if (i < 10) ret += "0";
  ret += i;
  return ret;
}

DATE and Time give you the date and time that the sketch was compiled, not the current time. The only way it will work to set your RTC is if you compile and then immediately upload while it is still that time. Then, if you restart your board it is going to set it to that time again, so if you use this method to set the RTC you'll need to then take those lines out and recompile and upload again.

The RTC should have a battery to keep time when power is off to the board. Make a separate sketch just to set the time and then upload your sketch that uses it.

Delta-G has the correct solution. You can test what might happen with code like:

  RTC.begin();

//  if (!RTC.isrunning()){           // Force an adjust() call

    // date and time adjust as the PC computer date and time
    RTC.adjust(DateTime(__DATE__, __TIME__)); 

//  }

to force an adjust() call once. Then stop the code, restore the original (commented-out) code, and run it? This will at least let you see if your RTC is running.

Which RTC chip?

I run this code fist.

and so my code. and the time chang. so it work :slight_smile:

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }

  Serial.begin(9600);
  while (!Serial) ; // wait for Arduino Serial Monitor
  delay(200);
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(__TIME__);
    Serial.print(", Date=");
    Serial.println(__DATE__);
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print(__TIME__);
    Serial.print("\", Date=\"");
    Serial.print(__DATE__);
    Serial.println("\"");
  }
}

void loop() {
}

bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}

bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

dear
I had this problem and I could solve it in this way
pull off the battery and wait 10 sec and then upload the code while the battery is still off the RTC
in this way the date and time would set according to the pc, while the power is connected pull back the battery and then reset the system you would see it will work properly.

also I just made some modification to the original code
the original one has three important issues
first and second ar:
the time is not written properly and system would not act properly out of the written time which is 17:30:00, I mean if the time goes 2 second ahead the system would not act until tomorrow which is deadly for all plants so I just made modification to check the moisture and acting in the period of two separated times, once in the morning between 8 to 10 and again between 17 to 19.
third is:
when the tank is empty and soil is dry there is an alarm for the tank but if in the middle of watering, tank goes empty the pump would not stop and it can cause destruction effect on the pump which is working without water, I just change it, so whenever the tank goes empty the pump would stop and start to sending alarm

also I changed pin 7 to 8 which is relay output because of busy position of outputs.
the code is here:

// libraries definition
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"



// frequency musical notes
#define NOTE_C6  1047
#define NOTE_C3  131
#define NOTE_G3  196
#define NOTE_DS3  156

// pins definition
int levelSensorPin = 0;
int moistureSensorPin = 1;
int audioPin = 2;
int soggyLEDPin = 3;
int moistsoilLEDPin = 4;
int drysoilLEDPin = 5;
int pumpLEDPin = 6;
int pumpPin = 8;

// variables
int levelSensorValue;       // stores the level sensor values
int moistureSensorValue;    // stores the moisture sensor values
int j = 0;

  // system messages
  const char *string_table[] =
  {  
    //0 
    "     Welcome! =)",
    //1
    "   Tank LOW level",
    //2
    "      Dry soil",
    //3
    "     Moist soil",
    //4
    "     Soggy soil",
    //5
    "The water pump is on",
    //6
    "  ArduinoByMyself",
    //7
    "  Watering System",
    //8
    "    Please wait!"
  };

// objects definition
RTC_DS1307 RTC;
LiquidCrystal_I2C lcd(0x27,20,4);


void setup(){
  // serial initialization
  Serial.begin(9600);
  
  // LCD initialization
  lcd.init();          
  lcd.backlight();     // with Backlight
  lcd.clear();         // clearscreen
  
  // Wire initialization
  Wire.begin();
  
  // RTC initialization
  RTC.begin();
  if (!RTC.isrunning()){
    // date and time adjust as the PC computer date and time
    RTC.adjust(DateTime(__DATE__, __TIME__)); 
  }
  
  // Arduino pins initalization
  pinMode(audioPin, OUTPUT);
  pinMode(soggyLEDPin, OUTPUT);
  pinMode(moistsoilLEDPin,OUTPUT);
  pinMode(drysoilLEDPin,OUTPUT);
  pinMode(pumpLEDPin,OUTPUT);
  pinMode(pumpPin,OUTPUT);
  
  // LCD initial messages
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(string_table[6]); 
  lcd.setCursor(0,1);
  lcd.print(string_table[7]); 
  lcd.setCursor(0,3);
  lcd.print(string_table[0]); 
  // initialization delay
  delay(5000); 
}


void loop(){
  
  
  // RTC parameters definition
  DateTime myRTC = RTC.now();
  int H = myRTC.hour();
  int M = myRTC.minute();
  int S = myRTC.second();
  
  // call Clock Function
  RightHour();
  
  // reads the sensors
  levelSensorValue = analogRead(levelSensorPin);
  moistureSensorValue = analogRead(moistureSensorPin);
  
  // if low water level: plays the low level alarm
  if(levelSensorValue > 600){
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[1]); 
    // plays the alarm sound
    for(int i=0;i<2;i++){
      tone(audioPin, NOTE_G3, 200);
      delay(200);
      tone(audioPin, NOTE_C3, 200);
      delay(200);
      tone(audioPin, NOTE_DS3, 200);
      delay(1000);
      noTone(audioPin);
    }
  }
  
  // check the moisture range
  if(moistureSensorValue >= 700){
    // in case of dry soil:
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[2]);
    // lights up the correct LED
    digitalWrite(drysoilLEDPin,HIGH);
    digitalWrite(moistsoilLEDPin,LOW);
    digitalWrite(soggyLEDPin,LOW);
    // plays the alarm sound
    tone(audioPin, NOTE_C6, 100);
    delay(1000);
    noTone(audioPin);
  }
  if((moistureSensorValue < 700) && (moistureSensorValue >= 300)){
    // in case of moist soil:
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[3]);
    // lights up the correct LED
    digitalWrite(drysoilLEDPin,LOW);
    digitalWrite(moistsoilLEDPin,HIGH);
    digitalWrite(soggyLEDPin,LOW);
    delay(1000);
  }
  if(moistureSensorValue < 300){
    // in case of soggy soil:
    // system messages
    lcd.clear();
    RightHour();
    lcd.setCursor(0,3);
    lcd.print(string_table[4]); 
    // lights up the correct LED
    digitalWrite(drysoilLEDPin,LOW);
    digitalWrite(moistsoilLEDPin,LOW);
    digitalWrite(soggyLEDPin,HIGH);
    delay(1000);
  }
  
  // between time 8:00:00 to 9:59:59 (morning check)
  if((H >= 8) && (H <= 9))
  {
    while(moistureSensorValue >= 700 && levelSensorValue <= 300){
      // system messages
      lcd.clear();
      RightHour();
      lcd.setCursor(0,2);
      lcd.print(string_table[8]);
      lcd.setCursor(0,3);
      lcd.print(string_table[5]);
      // turn the pump on
      digitalWrite(pumpPin,HIGH);
      digitalWrite(pumpLEDPin,HIGH);
      delay(5000);
      // if the soil is not moist so far
      // check sensors again
      moistureSensorValue = analogRead(moistureSensorPin);
      levelSensorValue = analogRead(levelSensorPin);
    }
    // turn the pump off
    digitalWrite(pumpPin,LOW);
    digitalWrite(pumpLEDPin,LOW);
  }
  
  // between time 17:00:00 to 18:59:59 (afternoon check)
  if((H >= 17) && (H <= 18))
  {
    while(moistureSensorValue >= 700 && levelSensorValue <= 300){
      // system messages
      lcd.clear();
      RightHour();
      lcd.setCursor(0,2);
      lcd.print(string_table[8]);
      lcd.setCursor(0,3);
      lcd.print(string_table[5]);
      // turn the pump on
      digitalWrite(pumpPin,HIGH);
      digitalWrite(pumpLEDPin,HIGH);
      delay(5000);
      // if the soil is not moist so far
      // check sensors again
      moistureSensorValue = analogRead(moistureSensorPin);
      levelSensorValue = analogRead(levelSensorPin);
    }
    // turn the pump off
    digitalWrite(pumpPin,LOW);
    digitalWrite(pumpLEDPin,LOW);
  }
  }

// Real Time Clock Function
void RightHour()
{
  DateTime Now = RTC.now();
  String clock_date = "  Date: ";
  String clock_hour = "   Time: ";
  
  int _day = Now.day();
  int _month = Now.month();
  int _year = Now.year();
  
  clock_date += fixZero(_day);
  clock_date += "/";
  clock_date += fixZero(_month);
  clock_date += "/";
  clock_date += _year;
  
  int _hour = Now.hour();
  int _minute = Now.minute();
  int _second = Now.second();
  
  clock_hour += fixZero(_hour);
  clock_hour += ":";
  clock_hour += fixZero(_minute);
  clock_hour += ":";
  clock_hour += fixZero(_second);
  
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(clock_date);
  lcd.setCursor(0, 1);
  lcd.print(clock_hour);
  
  delay(500);
}


String fixZero(int i)
{
  String ret;
  if (i < 10) ret += "0";
  ret += i;
  return ret;
}