Everytime I unplug the power source of the arduino board, the DS1307 RTC breakout board keeps on resetting. It shows the time when was upload. I've change the back up battery but still it resets. Please help. THanks
Let's see your program and a wiring diagram.
UKHeliBob:
Let's see your program and a wiring diagram.
// Date and time functions using just software, based on millis() & timer
#include <Wire.h>
#include "RTClib.h"
RTC_Millis RTC;
void setup () {
Serial.begin(57600);
// following line sets the RTC to the date & time this sketch was compiled
RTC.begin(DateTime(DATE, TIME));
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" seconds since 1970: ");
Serial.println(now.unixtime());
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
I just followed that wiring diagram
wiring
// following line sets the RTC to the date & time this sketch was compiled
RTC.begin(DateTime(__DATE__, __TIME__));
This line needs to be commented out of the sketch after the RTC is set. You keep resetting the RTC to the compile time every time it is executed.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
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__));
}
}
void loop () {
DateTime now = RTC.now();
Serial.print("Current time in RTC: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print("Now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
gonnie08:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
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));
}
}
void loop () {
DateTime now = RTC.now();
Serial.print("Current time in RTC: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print("Now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
this work. But how will put the real time? How do I adjust? Thanks.
But how will put the real time? How do I adjust? Thanks.
When you want to set the time, you run the program without the comments like you were doing.
The RTC should not need adjustment. If you find it does, then you can run the setting program whenever you think you have drifted too much. You can switch to a DS3231 from the DS1307 if you need better accuracy.
cattledog:
// following line sets the RTC to the date & time this sketch was compiled
RTC.begin(DateTime(DATE, TIME));
This line needs to be commented out of the sketch after the RTC is set. You keep resetting the RTC to the compile time every time it is executed.
cattledog:
// following line sets the RTC to the date & time this sketch was compiled
RTC.begin(DateTime(DATE, TIME));
This line needs to be commented out of the sketch after the RTC is set. You keep resetting the RTC to the compile time every time it is executed.
It also work. But It don't give the real time and date if i Comment RTC.begin(DateTime(DATE, TIME));
Use @gonnie08's code which I believe is is the ds1307 example in the RTClib.h examples folder. Run it once with the RTC.adjust without the comments to set the time.
RTC.adjust(DateTime(__DATE__, __TIME__));
Then put the comments back on
//RTC.adjust(DateTime(__DATE__, __TIME__));
Make the following mods:
#define SETCLOCKONCE // Add this line
void setup () {
Serial.begin(57600);
#ifdef SETCLOCKONCE // Add this line
RTC.begin(DateTime(__DATE__, __TIME__));
#endif // Add this line
}
Now run the clock program to initialize the RTC. After you run the program one time, comment out the line:
//#define SETCLOCKONCE // Comment out this line
Recompile, and upload the code to the board. Your clock should now keep the correct time even if power is removed. If it doesn't check the battery.
@acosidm If you have the DS1307 RTC you should not be using
RTC_Millis RTC;
Instead you should have
RTC_DS1307 RTC;
Refer to the two library examples softrtc and ds1307 and see the differences in how they work.