I'm revisiting an annoying problem I've had for a while, having my DS3231 RTC keep the correct time during power off. Plus each time I compile I need the RTC (fresh new or existing pre-set) to be set to compile time, even if it is already running. Most of the examples I have use something similar to:
if (! RTC.isrunning())
{
Serial.println("RTC is NOT running!");
following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
My full code is as follows:
// Daily Alarm Test using DS3231 RTC
//
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#include <TimeAlarms.h>
LiquidCrystal_I2C lcd(0x27,20,4);
RTC_DS1307 RTC;
time_t syncProvider()
{ return RTC.now().unixtime(); }
void setup () {
Wire.begin();
RTC.begin();
setSyncProvider(syncProvider);
//if (! RTC.isrunning())
//{
//Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__)); //Set RTC to compile time
// }
Alarm.alarmRepeat(8,0,0, Event1); // Daily event time 24 Hr
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
pinMode(7, OUTPUT); // Daily test output to controller zone
digitalWrite(7, HIGH); //Turn on Q3 Transistor
}
void loop () {
clockDisplay();
Alarm.delay(1000);
}
void clockDisplay(){
DateTime now = RTC.now();
char buf[20]; // -------------------- RTC Time --------------------
sprintf(buf, "%02d:%02d:%02d %02d/%02d/%4d", now.hour(), now.minute(), now.second(), now.month(), now.day(), now.year());
lcd.setCursor(0,0);
lcd.print(buf);
char buf1[20]; // ------------------ Arduino Time -----------------
sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%4d", hour(), minute(), second(), month(), day(), year());
lcd.setCursor(0,1);
lcd.print(buf1);
}
void Event1(){ // *********** Daily Alarm Output ****************
digitalWrite(7, LOW); //alarm test output
delay(4000);
digitalWrite(7, HIGH); }
Could it be that the DS3231 doesn't set correctly using the DS1307 commands, etc.?
These are new RTC units, the battery voltages are 3.29-3.3V