RTC? OMG!!! How difficult can it be?

I bought a cheapie DS1302 module from ebay - what was I thinking!!
Well actually - I was thinking “It’s only a dollar…” OK - buying anything on ebay, from China, for a dollar… I guess it was doomed to be destined for the bin - but before it gets chucked - can anyone offer any advice or cast-iron assured code that is known to work?
I have tried 3 or 4 different libraries, and even all the code that would normally be a library, but just in a sketch form, but do you think I can get any sense out of the damn thing? Even loading the example sketches that come with the libraries doesn’t help me - which makes me think I have a dud.

If I get any data out of it at all, it is nonsensical, like this…

Current Date / Time: 45/25/2125 45:45:45
Current Date / Time: 0/0/2000 0:0:0
Current Date / Time: 45/25/2125 45:45:45
Current Date / Time: 0/0/2000 0:0:0
Current Date / Time: 45/25/2125 45:45:45
Current Date / Time: 0/0/2000 0:0:0
Current Date / Time: 45/25/2125 45:45:45

OR

DS1302 Real Time Clock
Version 2, March 2013
Time = 45:45:45, Date(day of month) = 45, Month = 25, Day(day of week) = 7, Year = 2125
Time = 00:00:00, Date(day of month) = 0, Month = 0, Day(day of week) = 0, Year = 2000
Time = 45:45:45, Date(day of month) = 45, Month = 25, Day(day of week) = 7, Year = 2125
Time = 00:00:00, Date(day of month) = 0, Month = 0, Day(day of week) = 0, Year = 2000
Time = 45:45:45, Date(day of month) = 45, Month = 25, Day(day of week) = 7, Year = 2125
Time = 00:00:00, Date(day of month) = 0, Month = 0, Day(day of week) = 0, Year = 2000

I don’t seem able to get a time value to stick in the chip from one sketch to the next (I tried setting up a very basic ‘Set’ sketch, and an equally basic ‘Get’ sketch - but the Set sketch was giving me junk like above, then the Get sketch just said that there was no time set in the chip).

I have tried the following libraries, all to no avail…
DS1302.h
DS1302RTC.h
VirtuabotixRTC.h
RTCLib.h
…and more besides

Can anyone offer advice beyond “You should have known better” or "You should have gone for a "? I spent a whole dollar on this thing - I don’t want to be beaten :wink:

I would be looking at the wiring before leaping to other assumptions.

Have you initialized the RTC with a valid date and time? Also, have you googled "Arduino C code for DS1302"? I just did and got 117,000 hits. I'll bet one of those will give you a solution.

Hopefully, not the 116,999th one. 8^)

smithg27: from China...

is there any other kind?

KeithRB:
Hopefully, not the 116,999th one. 8^)

:>)

Sorta makes me wonder when I hear someone say: “My car keys were in the last place I looked!” Well…duh…

econjack: :>)

Sorta makes me wonder when I hear someone say: "My car keys were in the last place I looked!" Well...duh...

Psst: It's a joke, son. (Did you ever hear the whoosh?)

I use Sodaq_DS3231.h

Not sure off the top of my head which Chinese i2c clock I have. But the Sodaq works with it. Mines roughly 2 inches long and just a bit wider than the battery. If that helps at all. Has 6 pins of which I only use 4.

aarg: I would be looking at the wiring before leaping to other assumptions.

Thanks aarg - it looks OK, the three signal wires are the correct way around (CLK,DATA,RESET) and connected to the appropriately labelled pins in my code and the rtc declaration. The specs say you can use ANY digital pins, and I am currently using 2-4, but tonight I will try using different pins in case there is a problem on one of my UNO pins,

econjack: Have you initialized the RTC with a valid date and time? Also, have you googled "Arduino C code for DS1302"? I just did and got 117,000 hits. I'll bet one of those will give you a solution.

Sadly, yes - I have initialised it with a valid date - well I think so anyway... I used the example codes that came with each library to get the appropriate format of each one's 'set' command. Some set the time with individual parameters directly to the DS1302, while others work in hand with the Time.h library, so you set the time there first, and then use a single time_t value to set the DS1302. But neither seemed to work for me. And while I gave gave up after only about 30-40 of the posts you mentioned, it certainly feels like I read all 117,000 of them!!

DangerToMyself: I use Sodaq_DS3231.h

Not sure off the top of my head which Chinese i2c clock I have. But the Sodaq works with it. Mines roughly 2 inches long and just a bit wider than the battery. If that helps at all. Has 6 pins of which I only use 4.

The description sounds like just about all the generic DS1302 cards unfortunately - except the 6-pin bit... You mention it is I2C - perhaps that is the difference - mine is the standard 5 pin module (VCC/GND/CLK/DTA/RES). Interesting that you use a 3231 library - hadn't thought of trying other flavours, so I may give that a go later as well.

smithg27: The description sounds like just about all the generic DS1302 cards unfortunately - except the 6-pin bit... You mention it is I2C - perhaps that is the difference - mine is the standard 5 pin module (VCC/GND/CLK/DTA/RES). Interesting that you use a 3231 library - hadn't thought of trying other flavours, so I may give that a go later as well.

This guy here (https://youtu.be/teIRizvk4ss) uses pretty much exactly what I have. I should mention that it uses the analog pins too. So, the Sodaq library may or may not work.

take a look at this page.

I think the problem is that you are trying to use a ic2 library with a serial rtc.

http://playground.arduino.cc/Main/DS1302RTC

notice the statement

It is not I2C, not OneWire, and not SPI. So the standard libraries can not be used.

they do have a library listed that should work.

If you bought a cheap one, you need to modify it to work with the battery. They're supposed to come with a rechargeable CR2032 or some such. The cheap ones come with a standard cell that you can't rely on to save the time if you cut power to it. There's a guide somewhere online for the mod.

INTP: If you bought a cheap one, you need to modify it to work with the battery. They're supposed to come with a rechargeable CR2032 or some such. The cheap ones come with a standard cell that you can't rely on to save the time if you cut power to it. There's a guide somewhere online for the mod.

This sounds suspiciously like the ZS-042 module mod. But it is a DS3231 board. I don't know about the DS1302, but in the case of the ZS-042, a standard cell will provide working backup for about 10 years under normal environmental conditions. That is probably about as long as a rechargeable one will last.

The problem is not that it can't be relied on to save the time. The problem is that, without the mod, the standard battery can heat up when charging and cause damage or maybe even a fire.

What are you ultimately trying to make that requires an RTC?

If your RTC really is defective, and you don't want to wait for a new one to arrive before you proceed with your project, you can "fake" an RTC in code. The timekeeping will not be the best, but for testing purposes, it will probably be adequate.

odometer: What are you ultimately trying to make that requires an RTC?

If your RTC really is defective, and you don't want to wait for a new one to arrive before you proceed with your project, you can "fake" an RTC in code. The timekeeping will not be the best, but for testing purposes, it will probably be adequate.

You don't even need to write it, the Time library already does it.

odometer: What are you ultimately trying to make that requires an RTC?

...you can "fake" an RTC in code. The timekeeping will not be the best, but for testing purposes, it will probably be adequate.

Ironically - the DS1302 was to be the final step in a project to test various timekeeping mechanisms, starting from a basic delay loop, then delays with periodic adjustments to prevent drift, then using time.h, and finally introducing an RTC.

But if you really want to know, one of the projects I have in mind is a min/max thermometer that will be left unattended for weeks and may experience power interruptions, but I still want it to be able to record time and date against min and max temperatures even after a power interruption.

Just as well. The DS1302 is a lame chip. Get yourself a decent DS3231 RTC like the Chronodot.

I know you listed DS1302 as a library you tried, but for what it's worth it worked perfectly with my 1302. I use one sketch to set it and forget about it, then read it when necessary in other sketches. (Assuming of course it's the same library: one can't really rely on the library name to differentiate. Mine's the one from Henning Karlsen.)

My DS1302 is a loose chip and loose xtal, no battery backup, not a module like yours, but that shouldn't make a difference should it?

The other libraries like for 1307 and 3231 will surely not work, since the 1302 is not I2C.