I've been using x2 RTC DS1307 (tinyRTC) for data logging on and off and so far they have been great. However, today after the Arduino IDE crashing, I am having a problem with both RTC's not maintaining the correct time after my Arduino has been unplugged from the USB socket.
If I set the date and time and leave it connected to the serial monitor all seems ok, but when I unplug the Arduino, leave it a few minutes then plug it back in, open the serial monitor, all values remain as they were when setup previously. So for example, if I unplug the module for 5 minutes, the RTC is 5 minutes behind.
I've tried rebooting the computer (Mac), reseting the board, deleting the preference.txt file, but all remains the same. As I say this is happening with x2 of these modules, which were purchased at different times and have both been working fine in other projects.
Here is my code, which has not changed since the start:
#include "Wire.h" // Include the Wire library for I2C comms
#define I2Caddress 0x68 // This is the I2C address of the module
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; // Variables to store our data
void setup() {
Wire.begin(); // Start the I2C
Serial.begin(9600); // Start the Serial to show results in Serial Monitor
Wire.beginTransmission(I2Caddress); // Open I2C line in write mode
Wire.write((byte)0x00); // Set the register pointer to (0x00)
Wire.write(decToBcd(00)); // second
Wire.write(decToBcd(04)); // minute
Wire.write(decToBcd(13)); // hour
Wire.write(decToBcd(01)); // day of week, with Sunday being 1
Wire.write(decToBcd(26)); // day of month
Wire.write(decToBcd(05)); // month
Wire.write(decToBcd(13)); // year
Wire.endTransmission(); // End write mode
// The above will set the clock to 10:48 on Tue 12th March 2013
// Uncomment the above code to set the time and date
}
void loop() {
Wire.beginTransmission(I2Caddress); // Open I2C line in write mode
Wire.write((byte)0x00); // Set to the first address
Wire.endTransmission(); // End write mode
Wire.requestFrom(I2Caddress, 7); // Now request 7 Bytes from the module
// Store the 7 Bytes of data in our variables.
second = bcdToDec(Wire.read());
minute = bcdToDec(Wire.read());
hour = bcdToDec(Wire.read());
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
// Send our results to the Serial Monitor
Serial.print("Hour : ");
Serial.println(hour, DEC);
Serial.print("Minute : ");
Serial.println(minute, DEC);
Serial.print("Second : ");
Serial.println(second, DEC);
Serial.print("Day of week : ");
Serial.println(dayOfWeek, DEC);
Serial.print("Day of month : ");
Serial.println(dayOfMonth, DEC);
Serial.print("Month : ");
Serial.println(month, DEC);
Serial.print("Year : ");
Serial.println(year, DEC);
Serial.println();
delay(3000); // 3 second pause
}
// Convert decimal to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to decimal
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}