I use this one:
adafruit's RTClibHere are a couple of simple sketches that I have used (not particularly efficient use of Arduino processing power, but maybe enough to get people started):
//
// DS1307SetTime.pde
//
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//
// RTClib from https://github.com/adafruit/RTClib
//
// davekwx
//
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup ()
{
Serial.begin(9600);
Wire.begin();
rtc.begin();
char inbuf[80];
char outbuf[80];
int i;
unsigned year;
unsigned month;
unsigned day;
unsigned minutes;
unsigned hours;
unsigned seconds;
Serial.print("Enter date/time: YYYYMMDDHHMMSS");
while(Serial.available() < 14)
;
Serial.println();
for(i = 0; i < 14; i++) {
inbuf[i] = Serial.read();
}
sscanf(inbuf, "%4d %2d %2d %2d %2d %2d ", &year, &month, &day, &hours, &minutes, &seconds);
sprintf(outbuf, "YYYY = %d, MM = %02d, DD = %02d, HH = %02d, MM = %02d, SS = %02d",
year, month, day, hours, minutes, seconds);
Serial.println(outbuf);
rtc.adjust(DateTime(year, month, day, hours, minutes, seconds));
}
uint8_t oldSeconds = 0;
void loop ()
{
DateTime now = rtc.now();
if (now.second() != oldSeconds) {
char buffer[100];
oldSeconds = now.second();
sprintf(buffer, "%s %s %2u %d %02d:%02d:%02d",
now.weekDay(), now.mon(), now. day(), now.year(), now.hour(), now.minute(), now.second());
Serial.println(buffer);
}
delay(200);
}
and
//
// DS1307ShowTime.pde
//
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
//
// RTClib from https://github.com/adafruit/RTClib
//
// davekw7x
//
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"
const int ledPin = 13;
RTC_DS1307 RTC;
// initialize the lcd with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup ()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Wire.begin();
RTC.begin();
lcd.begin(20,2);
if (! RTC.isrunning()) {
Serial.println("RTC was not running, you will have to run DS1307SetTime.");
Serial.println("For now, I'll set the RTC to the date and time the program was compiled.");
Serial.println("For better initialization, run Dave's DS1307SetTime sketch.");
Serial.println();
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
}
uint8_t old_seconds = 0;
void loop ()
{
DateTime now = RTC.now();
if (now.second() != old_seconds) {
char buffer[40];
old_seconds = now.second();
sprintf(buffer, "%s %s %2u %d %02d:%02d:%02d",
now.weekDay(), now.mon(), now.day(), now.year(), now.hour(), now.minute(), now.second());
Serial.println(buffer);
lcd.setCursor(0,0);
sprintf(buffer, "%s %s %2u %d ", now.weekDay(), now.mon(), now.day(), now.year());
lcd.print(buffer);
lcd.setCursor(0,1);
sprintf(buffer, " %02d:%02d:%02d ", now.hour(), now.minute(), now.second());
lcd.print(buffer);
}
delay(200);
}
Output from DS1307ShowTIme on the serial monitor (See Footnote.)
Fri Mar 11 2011 17:02:35
Fri Mar 11 2011 17:02:36
Fri Mar 11 2011 17:02:37
Fri Mar 11 2011 17:02:38
Fri Mar 11 2011 17:02:39
Fri Mar 11 2011 17:02:40
Fri Mar 11 2011 17:02:41
.
.
.
Regards,
Dave
Footnote:For a convenient display I added the following two functions to RTClib.cpp to make it easy to display month and day of week as short "strings."
const char *DateTime::weekDay() const
{
static const char *day [7] = {
"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"
};
return day[dayOfWeek() % 7];
}
// January = 1, February = 2, ...
const char *DateTime::mon() const
{
static const char *m[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
return m[month()-1];
}
And I added the ;public function declarations in RTClib.h
const char * weekDay() const; // String abbreviation
const char * mon() const;
The same type of things could be done with external functions, but I decided to make them part of the class.