Hopefully this is in the right spot on the forum.
I am using the DS1307 real tiny clock module, I see there is a number of posts about it having small inaccuracy ie 20 seconds a week, this however is not my problem.
My problem is that for every 1 second my RTC module seems to count too fast (6 seconds approx).
I am using the setTime sketch example and then the readTest sketch from the DS1307RTC library.
Here is an example output:
setting:
DS1307 configured Time=21:05:42, Date=Feb 5 2020
that works as expected however this is the weird part.
getting time:
Ok, Time = 21:07:26, Date (D/M/Y) = 5/2/2020
Ok, Time = 21:07:32, Date (D/M/Y) = 5/2/2020
Ok, Time = 21:07:38, Date (D/M/Y) = 5/2/2020
and so on.....
This code is updating every second so it is not that.
Until it ends up hours a head in minutes.
Any ideas?
This code is updating every second
What code would that be ?
For set time:
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
and getting time sketch:
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}
void loop() {
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}
What is the output if you turn on the timestamp in the Serial monitor ?
21:21:51.073 -> Ok, Time = 21:24:46, Date (D/M/Y) = 5/2/2020
21:21:52.084 -> Ok, Time = 21:24:51, Date (D/M/Y) = 5/2/2020
21:21:53.070 -> Ok, Time = 21:24:57, Date (D/M/Y) = 5/2/2020
21:21:54.082 -> Ok, Time = 21:25:03, Date (D/M/Y) = 5/2/2020
21:21:55.064 -> Ok, Time = 21:25:09, Date (D/M/Y) = 5/2/2020
21:21:56.079 -> Ok, Time = 21:25:15, Date (D/M/Y) = 5/2/2020
21:21:57.095 -> Ok, Time = 21:25:21, Date (D/M/Y) = 5/2/2020
Totally bizarre.
It's certainly not in the code as both the setting and reading work properly with my DS1307.
11:05:44.836 -> Ok, Time = 11:05:20, Date (D/M/Y) = 5/2/2020
11:05:45.852 -> Ok, Time = 11:05:21, Date (D/M/Y) = 5/2/2020
11:05:46.871 -> Ok, Time = 11:05:22, Date (D/M/Y) = 5/2/2020
11:05:47.848 -> Ok, Time = 11:05:23, Date (D/M/Y) = 5/2/2020
11:05:48.859 -> Ok, Time = 11:05:24, Date (D/M/Y) = 5/2/2020
11:05:49.868 -> Ok, Time = 11:05:25, Date (D/M/Y) = 5/2/2020
11:05:50.856 -> Ok, Time = 11:05:26, Date (D/M/Y) = 5/2/2020
I can't work out a stuck bit in the seconds register which would cause this defect.
I think there is a defect with the crystal oscillator or some sort of internal divider.
I would treat yourself to a DS3231.
Yeah it's totally strange when for 5 minutes there it remembered what a second was and worked.
How ever it has since forgotten again.
Thanks for the help I will go get a 3231 today