// 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);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
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(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);
}
I get the following output:
2094/5/29 0:18:62
since midnight 1/1/1970 = 3925930742s = 45439d
2099/5/29 0:18:62
since midnight 1/1/1970 = 4083697142s = 47265d
2094/5/29 0:18:62
since midnight 1/1/1970 = 3925930742s = 45439d
2099/5/29 0:18:62
since midnight 1/1/1970 = 4083697142s = 47265d
2094/5/29 0:18:62
since midnight 1/1/1970 = 3925930742s = 45439d
It just oscillates between those. What could be the problem? I'm using Ubuntu 12.04 and the Arduino Uno.
Thanks for your help.
I have the GND pin going to the GND on the Arduino.
The VCC pin is connected to the positive lead wire from 4 AA batteries.
I also connected the ground wire of the 4 AA batteries to a GND pin on the Arduino (is that correct)?
The SDA wire is going to pin A4.
The SCL wire is going to pin A5.
The VCC pin is connected to the positive lead wire from 4 AA batteries.
4 AA batteries do not have a defined voltage. Alkaline batteries may have between 4.5V and 6V, rechargeable batteries are probably in the range of 3.6V to 4.8V. DS1307 are quite picky about power supply. If the voltage falls below about 4.7V they just don't communicate with the controller anymore. So provide stabilized power to the chip and your problem probably disappears.
The VCC pin is connected to the positive lead wire from 4 AA batteries.
4 AA batteries do not have a defined voltage. Alkaline batteries may have between 4.5V and 6V, rechargeable batteries are probably in the range of 3.6V to 4.8V. DS1307 are quite picky about power supply. If the voltage falls below about 4.7V they just don't communicate with the controller anymore. So provide stabilized power to the chip and your problem probably disappears.
I hooked it up to the 5V pin of the Arduino and now I'm getting this:
2099/10/26 0:0:63
since midnight 1/1/1970 = 4096656063s = 47415d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
2010/26/6 0:63:81
since midnight 1/1/1970 = 1304384661s = 15097d
Also, running the TimeRTCSet sketch from the Time.h library:
/*
* TimeRTCSet.pde
* example code illustrating Time library with Real Time Clock.
*
* RTC clock is set in response to serial port time message
* A Processing example sketch to set the time is included in the download
* On Linux, you can use "date +T%s > /dev/ttyACM0" (UTC time zone)
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
void setup() {
Serial.begin(9600);
while (!Serial) ; // Needed for Leonardo only
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
void loop()
{
if (Serial.available()) {
time_t t = processSyncMessage();
if (t != 0) {
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if(Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime;
if( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}
EDIT: Last question, I set the time using the setTime function from the Time library:
void setTime(int hr,int min,int sec,int dy, int mnth, int yr)
The method sets the time just fine, but after the Arduino loses power, powering it up again starts the clock from the setTime method.
I tried commenting it out, but of course it just resets itself to 1970 unix time.
How do I get the RTC to continuously update and retain the time after being disconnected?
How do I get the RTC to continuously update and retain the time after being disconnected?
Does your RTC have a battery for that? Is the battery a fresh one?
There's a battery installed. I'm not sure how fresh it is, but it came with the RTC and I just bought it a couple of weeks ago, so I would think it's good.