0

I tried to use the RTC DS1307 in my Arduino project, every time I ran my code I get this error: RTC is not running. The code should light up a LED every minutes:10sc and turn off every minutes:20s.

This is the code that I wrote:

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

RTC_DS1307 rtc;

void setup () 
{
  Serial.begin(57600); 
   pinMode(0, OUTPUT);  
 #ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
   if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
  }
  if (! rtc.isrunning()){
    Serial.println("RTC is not running");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }        
}
void loop () {
    DateTime now = rtc.now();
    lcd.setCursor(0, 2);
    Serial.println(now.month(),DEC);
    lcd.print(now.year(), DEC);
    lcd.print('/');
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.day(), DEC);
    lcd.print(' '); 
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);

    if (now.second() == 10 )
    { 
      digitalWrite (0, HIGH); 
      Serial.println("high");
    }
    else if (now.second() == 20 )  
    { 
      digitalWrite (0, LOW); 
      Serial.println("low");
    }
     delay(1000); 

}

I used the RTClib.h lib from Arduino site. Any idea please.

BNT
  • 936
  • 10
  • 27
Walid Sassi
  • 185
  • 2
  • 16

1 Answers1

0

I have just had the same sort of problem with a program that had been working fine - using the same sort of code. I think the battery connection was lost, probably briefly.

I found that it definitely needs initialisation code , e.g. ...

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

to be sent after battery failure of removal. Went back to working after that.

Siphalor
  • 712
  • 5
  • 16