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) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
dayOfWeek = (byte) (Serial.read() - 48);
dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
Wire.beginTransmission(clockAddress);
Wire.write(byte(0x00));
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
// Reset the register pointer
Wire.beginTransmission(clockAddress);
Wire.write(byte(0x00));
Wire.endTransmission();

Wire.requestFrom(clockAddress, 7);

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

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

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

}

void setup() {
Wire.begin();
Serial.begin(57600);
}

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

// 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.
Wire.beginTransmission(clockAddress);

// Set the register pointer to be just past the date/time registers.
Wire.write(byte(0x08));
for (int i = 1; i <= 27; i++) {
Wire.write(byte(0xff));
delay(100);
}
Wire.endTransmission();
getDateDs1307();
Serial.println(": RTC1307 Initialized Memory");
}
else if (command == 50) { //If command = “2” RTC1307 Memory Dump
getDateDs1307();
Serial.println(": RTC 1307 Dump Begin");
Wire.beginTransmission(clockAddress);
Wire.write(byte(0x00));
Wire.endTransmission();
Wire.requestFrom(clockAddress, 64);
for (int i = 1; i <= 64; i++) {
test = Wire.read();
Serial.print(i);
Serial.print(":");
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
delay(100);
}

Have you tried RTClib.h? https://github.com/adafruit/RTClib

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"

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(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);
}

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 https://github.com/adafruit/RTClib/downloads, 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