Real Time Clock

I have tried various sketches (those in the library and Sparkfun) but can not get my real time clock to hold the time.

When I send the T********** code (Epoch number) via the serial port everything works fine..... for just under 5 minutes then the year leaps about 40 into the future.

I have used the TimeRTCset as it is. I have also added a sparkfun library in. Both do the same.

I am using DS1307RTC time card, although the files here only seem to be word pad.

Is there something obvious I am missing? Thanks

Post the script that is giving that problem, and a link to which rtc you are using.

This is the sparkfun one -

#include <Wire.h>

int clockAddress = 0x68; // This is the I2C address
int command = 0; // This is the command char, in ascii form, sent from the serial port
long previousMillis = 0; // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
return ( (val/10*16) + (val%10) );

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
return ( (val/16*10) + (val%16) );

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you’re passing in valid numbers,
// Probably need to put in checks for valid numbers.
void setDateDs1307()
// Use of (byte) type casting and ascii math to achieve result.
second = (byte) (( - 48) * 10 + ( - 48));
minute = (byte) (( - 48) *10 + ( - 48));
hour = (byte) (( - 48) *10 + ( - 48));
dayOfWeek = (byte) ( - 48);
dayOfMonth = (byte) (( - 48) *10 + ( - 48));
month = (byte) (( - 48) *10 + ( - 48));
year= (byte) (( - 48) *10 + ( - 48));
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
// Reset the register pointer

Wire.requestFrom(clockAddress, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec( & 0x7f);
minute = bcdToDec(;

// Need to change this if 12 hour am/pm
hour = bcdToDec( & 0x3f);
dayOfWeek = bcdToDec(;
dayOfMonth = bcdToDec(;
month = bcdToDec(;
year = bcdToDec(;

Serial.print(hour, DEC);
Serial.print(minute, DEC);
Serial.print(second, DEC);
Serial.print(" “);
Serial.print(month, DEC);
Serial.print(dayOfMonth, DEC);
Serial.print(year, DEC);


void setup() {

void loop() {
if (Serial.available()) { // Look for char in serial que and process if found
command =;
if (command == 84) { //If command = “T” Set Date
Serial.println(" ");
else if (command == 81) { //If command = “Q” RTC1307 Memory Functions
if (Serial.available()) {
command =;

// If command = “1” RTC1307 Initialize Memory - All Data will be set to 255 (0xff).
// Therefore 255 or 0 will be an invalid value.
if (command == 49) {

// 255 will be the init value and 0 will be cosidered an error that
// occurs when the RTC is in Battery mode.

// Set the register pointer to be just past the date/time registers.
for (int i = 1; i <= 27; i++) {
Serial.println(": RTC1307 Initialized Memory");
else if (command == 50) { //If command = “2” RTC1307 Memory Dump
Serial.println(": RTC 1307 Dump Begin");
Wire.requestFrom(clockAddress, 64);
for (int i = 1; i <= 64; i++) {
test =;
Serial.println(test, DEC);
Serial.println(" RTC1307 Dump end");
Serial.print("Command: ");
Serial.println(command); // Echo command CHAR in ascii that was sent

command = 0; // reset command

Have you tried RTClib.h?

Thanks for the response

When I try to compile I get -

core.a(main.cpp.o): In function main': C:\Documents and Settings\My Documents\Downloads\arduino-1.0-windows\arduino-1.0\hardware\arduino\cores\arduino/main.cpp:11: undefined reference tosetup' C:\Documents and Settings\My Documents\Downloads\arduino-1.0-windows\arduino-1.0\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop'

With which sketch? The supplied example is:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"


void setup () {

  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 =;
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(, DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    // 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(future.month(), DEC);
    Serial.print(, DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(future.minute(), DEC);
    Serial.print(future.second(), DEC);

Based on that sketch I get -

sketch_feb13e.cpp:4:20: error: RTClib.h: No such file or directory sketch_feb13e:5: error: 'RTC_DS1307' does not name a type sketch_feb13e.cpp: In function 'void setup()': sketch_feb13e:10: error: 'RTC' was not declared in this scope sketch_feb13e:15: error: 'DateTime' was not declared in this scope sketch_feb13e.cpp: In function 'void loop()': sketch_feb13e:20: error: 'DateTime' was not declared in this scope sketch_feb13e:20: error: expected ;' before 'now' sketch_feb13e:22: error: 'now' was not declared in this scope sketch_feb13e:42: error: expected;' before 'future' sketch_feb13e:45: error: 'future' was not declared in this scope

You need to download and install the library. Go to, and click on the 'Download as zip' button. Unzip the downloaded file into the libraries directory inside your sketchbook directory, then rename the resulting directory as RTClib. You'll need to restart the IDE so that it can scan for new libraries.

Sorry just keep getting same error. I have added the folder into my sketchbook and restarted it.

Then you haven't got it in the right place. What is the full path to the RTClib directory?

On my Mac, it is /Users/davidwood/Documents/Arduino/Libraries/RTClib, where /Users/davidwood/Documents/Arduino is the sketchbook directory. Downloaded libraries then go into /Users/davidwood/Documents/Arduino/Libraries.

Ok, files sorted out now and it has uploaded ok. I then type in T1329148043 through the COM port.

This brings up the correct time. I then save close the com port, open it again ant the time has gone from 2012 to 2036

I did hope that it would save the time.

The sketch I posted above doesn't take the time from serial. Does that work ok?

I am not sure - I have been looking at the serial com port. How else can I tell?

It prints the time to the serial port every three seconds, so you should see the time increasing properly. If you reset the arduino, it should start printing the correct time again.

Rest the arduino and uploaded the sketch again.

COM is updating every 3 seconds but with jumbled code now rather than date.

What would cause this?

Probably baud rate on your serial monitor. It's set at 57600 in the sketch.

Yes, the baud rate required increase.

It now seems the RTC is not on, maybe that is why it did not work after five minutes after the Com command

I'm a bit stuck then. Do you have a battery properly installed in the clock card?

I think I will get another battery just to be on the safe side. Thanks for your help