RTC DS1302 - Where is my TIME?!

Hi there,

I’m trying to test my new Arduino UNO with DS1302 Module (the green-brown module), but I got some strange readings from the Serial monitor.

I got double DATE reading without TIME.

Used RTC Library can be found here.

Used RTC Module:

#include <DS1302.h>

DS1302 RTC(2, 3, 4);

char RTCBuff[34];

void setup() {
 pinMode(13, OUTPUT);
 digitalWrite(13, LOW);
 RTC.halt(false);
 RTC.writeProtect(false);
 Serial.begin(9600);
 
 //RTC.setDOW(6);            // Set Day-of-Week format SATURDAY or 6
 //RTC.setTime(22, 44, 0);   // Set the time format 00:00:00
 //RTC.setDate(4, 7, 2015);  // Set the date format dd.mm.yyyy
 
}

void loop() {
  // Show Day, Date, Time
 sprintf(RTCBuff, "%s, %s, %s", RTC.getDOWStr(), RTC.getDateStr(), RTC.getTimeStr());
 Serial.println(RTCBuff);
 memset(RTCBuff, 0, sizeof(RTCBuff)); // Clear RTCBuff Buffer
 delay(1000);
 Serial.flush();
}

Can someone help me out, what is wrong with my code?!

Thanks!

beic:
with DS1302 Module (the green-brown module)

You add the color like it means something.

Maybe a link to the module you’re using and the library you’re using would be helpful.

beic:

 delay(1000);

Serial.flush();

Unrelated, but there’s absolutely no reason to call flush() after a 1 second delay. The transmit buffer is definitely cleared by that point.

Edited my Topic, now all things are there…

Yes, it would help if we could see the library you are using for the DS1302.

Anyway, I suspect that the problem lies here:

sprintf(RTCBuff, "%s, %s, %s", RTC.getDOWStr(), RTC.getDateStr(), RTC.getTimeStr());

Maybe it would help if you got those strings one at a time instead of all at once. Something like this:

#include <DS1302.h>

DS1302 RTC(2, 3, 4);

char RTCBuff[34];
char RTCBuff1[] = "abcdefghijkl";
char RTCBuff2[] = "mnopqrstuvwx";
char RTCBuff3[] = "yzABCDEFGHIJ";

void setup() {
 pinMode(13, OUTPUT);
 digitalWrite(13, LOW);
 RTC.halt(false);
 RTC.writeProtect(false);
 Serial.begin(9600);
 
 //RTC.setDOW(6);            // Set Day-of-Week format SATURDAY or 6
 //RTC.setTime(22, 44, 0);   // Set the time format 00:00:00
 //RTC.setDate(4, 7, 2015);  // Set the date format dd.mm.yyyy
 
}

void loop() {
  // Show Day, Date, Time
 sprintf(RTCBuff1, "%s", RTC.getDOWStr());
 sprintf(RTCBuff2, "%s", RTC.getDateStr());
 sprintf(RTCBuff3, "%s", RTC.getTimeStr());
 sprintf(RTCBuff, "%s, %s, %s", RTCBuff1, RTCBuff2, RTCBuff3);
 Serial.println(RTCBuff);
 RTCBuff1[0] = '\0'; 
 RTCBuff2[0] = '\0';
 RTCBuff3[0] = '\0';
 memset(RTCBuff, 0, sizeof(RTCBuff)); // Clear RTCBuff Buffer
 delay(1000);
 Serial.flush();
}

Try this out and let me know how it works.

odometer:
Yes, it would help if we could see the library you are using for the DS1302.

Anyway, I suspect that the problem lies here:

sprintf(RTCBuff, "%s, %s, %s", RTC.getDOWStr(), RTC.getDateStr(), RTC.getTimeStr());

Maybe it would help if you got those strings one at a time instead of all at once. Something like this:

#include <DS1302.h>

DS1302 RTC(2, 3, 4);

char RTCBuff[34];
char RTCBuff1 = “abcdefghijkl”;
char RTCBuff2 = “mnopqrstuvwx”;
char RTCBuff3 = “yzABCDEFGHIJ”;

void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
RTC.halt(false);
RTC.writeProtect(false);
Serial.begin(9600);

//RTC.setDOW(6);            // Set Day-of-Week format SATURDAY or 6
//RTC.setTime(22, 44, 0);   // Set the time format 00:00:00
//RTC.setDate(4, 7, 2015);  // Set the date format dd.mm.yyyy

}

void loop() {
 // Show Day, Date, Time
sprintf(RTCBuff1, “%s”, RTC.getDOWStr());
sprintf(RTCBuff2, “%s”, RTC.getDateStr());
sprintf(RTCBuff3, “%s”, RTC.getTimeStr());
sprintf(RTCBuff, “%s, %s, %s”, RTCBuff1, RTCBuff2, RTCBuff3);
Serial.println(RTCBuff);
RTCBuff1[0] = ‘\0’;
RTCBuff2[0] = ‘\0’;
RTCBuff3[0] = ‘\0’;
memset(RTCBuff, 0, sizeof(RTCBuff)); // Clear RTCBuff Buffer
delay(1000);
Serial.flush();
}



Try this out and let me know how it works.

Hi odometer,

Your example are working just fine! Thanks for that, but…

My code was working fine a few hours ago and suddenly it’s starts to mess up…and I don’t know why!?

I wish to make my code smaller, that was my goal in my posted code.

My code was working fine a few hours ago and suddenly it's starts to mess up...and I don't know why!?

Since this is for a clock, maybe the reason has something to do with the time.

What time was it when the code was working? What time did it stop working?

By the way, the library you are using looks rather complicated. Maybe the problem is somewhere in there.

odometer: Since this is for a clock, maybe the reason has something to do with the time.

What time was it when the code was working? What time did it stop working?

By the way, the library you are using looks rather complicated. Maybe the problem is somewhere in there.

It was happened between 21h-22h.

I was playing with RTCBuff size and rearranging the code.

beic: It was happened between 21h-22h.

I was playing with RTCBuff size and rearranging the code.

It seems as though your "rearranging" had something to do with it.

When you say you want to "reduce code size", what do you mean? Do you mean the size of the .ino file itself? Or do you mean the number of bytes of program space used on the Arduino chip? Because these are not the same thing.

odometer:
It seems as though your “rearranging” had something to do with it.

When you say you want to “reduce code size”, what do you mean? Do you mean the size of the .ino file itself? Or do you mean the number of bytes of program space used on the Arduino chip? Because these are not the same thing.

Yeah, I was trying to reduce the number of bytes of program space used on the Arduino chip (Reducing the code lines)!

I already tried to remove the battery from RTC module, etc…

The library is faulty.

char *DS1302::getTimeStr(uint8_t format)
{
    char *output= "xxxxxxxx";
char *DS1302::getDateStr(uint8_t slformat, uint8_t eformat, char divider)
{
    char *output= "xxxxxxxxxx";

For starters, these two methods write to string literals, which has undefined behavior. To make it worse (if anything can be worse than undefined behavior), the compiler is apparently merging the string literals (which is perfectly legal), meaning the two methods share the same string. The methods are simply stepping on each others' toes.

christop: The library is faulty.

char *DS1302::getTimeStr(uint8_t format)
{
    char *output= "xxxxxxxx";
char *DS1302::getDateStr(uint8_t slformat, uint8_t eformat, char divider)
{
    char *output= "xxxxxxxxxx";

For starters, these two methods write to string literals, which has undefined behavior. To make it worse (if anything can be worse than undefined behavior), the compiler is apparently merging the string literals (which is perfectly legal), meaning the two methods share the same string. The methods are simply stepping on each others' toes.

Can someone fix it?!

I would recommend a different library https://github.com/iot-playground/Arduino/tree/master/external_libraries/DS1302RTC

This library by Timur Maksimov is a drop-in replacement for the DS1307RTC.h library by Michael Margolis that is supplied with the Arduino Time library above. To change from using a DS1307 RTC to a DS1302 RTC, it is only necessary to change the

include statement to include DS1302RTC.h instead of DS1307RTC.h.

It uses the time library.

Yeah, I solved it by using another LIB (S1302 RTC library for Arduino by Matt Sparks)