DS1302 worked, but now doesn't

I have been learning programming Arduino with some success. In my project , I had the DS1302 RTC module from Velleman running fine, but it stopped working properly.

Once I understood what the code I took from several examples and tutorials did, I went through and cleaned up my final code.

After that, the time being read alternates between what I set - the correct time/date - and xxxxxxx or 00:00:00. Every other read of the clock is wrong/scrambled

Nothing I tried changed the result, so I went back to the DS1302 example and I get the same output.

Example output from Serial Easy:

Sunday 11.05.2017 -- 13:34:33
113:34:33 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:35
113:34:35 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:37
113:34:37 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:39
113:34:39 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:41
113:34:41 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:43
113:34:43 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:45
113:34:45 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:47
113:34:47 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:49
113:34:49 00.00.2000 -- 00:00:00
Sunday 11.05.2017 -- 13:34:51
113:34:51 00.00.2000 -- 00:00:

Example from Serial Hard:

Today is the 0. day of xxxxxxxxx in the year 2000.
It is the 0. day of the week (counting monday as the 1th), and it has passed 0 hour(s), 0 minute(s) and 0 second(s) since midnight.


Today is the 6. day of August in the year 2010.
It is the 5. day of the week (counting monday as the 1th), and it has passed 12 hour(s), 0 minute(s) and 1 second(s) since midnight.


Today is the 0. day of xxxxxxxxx in the year 2000.
It is the 0. day of the week (counting monday as the 1th), and it has passed 0 hour(s), 0 minute(s) and 0 second(s) since midnight.


Today is the 6. day of August in the year 2010.
It is the 5. day of the week (counting monday as the 1th), and it has passed 12 hour(s), 0 minute(s) and 3 second(s) since midnight.


Today is the 0. day of xxxxxxxxx in the year 2000.
It is the 0. day of the week (counting monday as the 1th), and it has passed 0 hour(s), 0 minute(s) and 0 second(s) since midnight.


Today is the 6. day of August in the year 2010.
It is the 5. day of the week (counting monday as the 1th), and it has passed 12 hour(s), 0 minute(s) and 5 second(s) since midnight.


Also, tried a different RTC module with same result

Any ideas what I broke?

Thanks!

  • a -

AjaxOfTheRockies:
Any ideas what I broke?

Hard to tell without a crystal ball.

Really?

Are you trying to tell me something, or just being flippant?

I was hoping for some help/insight to the problem

Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

Can you please read Sticky Topic How to use this forum - please read.

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

I believe the problem is very likely to be in your code. But until I can see some code I can't offer a more detailed diagnosis.

Steve

Are you trying to tell me something, or just being flippant?

He isn't being flippant at all. He is saying that you've left out a ton of detail. You're doing something that works for everyone else and want to know why it doesn't work for you. Can you imagine what sorts of things a person would need to be able to see in order to figure out what you did differently? It may even be something you think you did right.

I think a wiring diagram might be in order. Posting the code as it appears on your screen might help, but I get that we are working with examples here.

Thanks, Gentlemen. I do appreciate the constructive criticism (I'm not good with sarcasm). Anyway, I didn't post code because I went backwards through my versions trying to find where i changed to the problem and ended clear back at the built-in example from the DS1302 library - called "DS1302_Serial_Easy". DS3102_Serial_Easy is generating the output I copied in my original post - that isn't coming from my attempt at my own custom code, although it displays the same alternating valid-invalid data.

Also, I spent significant time on tip #4 and did not find an answer that fit my issue/environment - I much prefer to find a thread that explains my problem because that saves time getting a new thread going.

That said, I will post full code along with schematic and details later this evening. Have to run errands with the wife right now.

Respectfully,

  • a -

You mentioned stop working properly. Do you mean the system was working just fine with nobody touching it… and it was being used regularly… and then it started to change behaviour?

Doesn’t look like anything broke. Looks like coding issue.

The thing is… it appears that the system is functioning at least. Just need to figure out what was done to the code. And maybe need to re-download the original tutorial code.

@AjaxOfTheRockies, when posting code and diagram, please provide a link to the library that you use as well.

@Southpark, if I'm not mistaken, OP indicates that even the example does not work correctly.

@AjaxOfTheRockies, Here are some info on these problem

Those chip from eBay, the clock cycles are much slower than they have to be to meet the datasheet spec.

You can use the Library which have fine tune for this

Thanks bill. Could this mean that the RTC module’s battery has dropped in voltage? And the reduced voltage led to this situation?

May be the battery or the crystal?
or may be a defective counterfeit chip?

Thanks, Bill. I started reading the threads, but it is going to take me a while to get through all that, so that I understand it.

I wanted to post code I wrote tonight. It is based on my project but stripped down to just time and display on the Serial Monitor; for the discussion.

Also for the discussion, I checked the battery on the RTC module and it is at 3.31v. The module isn’t from eBay, it is a Velleman VMA301 I bought at Micro Center. The chip itself could be a knock-off; no Maxim name or logo.

While I’m reading your recommended topics, here is my clock code:

No schematic tonight, because I’ve stripped the hardware down:

RTC Vcc > Arduino 5v
RTC Gnd > Arduino Gnd
RTC RST > Arduino D5
RTC DAT > Arduino D6
RTC CLK > Arduino D7

Nothing else connected

#include <DS1302.h>

// Set RTC pins with these defines !
#define RTC_RST  5    // (RST)Arduino pin for the Chip Enable
#define RTC_DAT  6    // (DAT)Arduino pin for the Data I/O
#define RTC_CLK  7    // (CLK)Arduino pin for the Serial Clock

String Month;
String IsMon;
String IsDay;
int IsDate= 0;
int IsYear= 0;
int IsHour= 0;
int IsMin= 0;
int IsSec= 0;

DS1302 rtc(RTC_RST, RTC_DAT, RTC_CLK); // Init the DS1302 RTC
Time t; // Init a Time-data structure

void setup()
{
 // Set the clock to run-mode, and disable the write protection
     rtc.halt(false);
     rtc.writeProtect(false);

  // Setup Serial connection
     Serial.begin(9600);delay(1500);

    uint16_t time = millis();
    time = millis() - time;
}

void loop()
{
  t=rtc.getTime();
  
  //GET DATA
 //Date pieces for proper display
   Month=(rtc.getMonthStr()); //returns full name of month
   IsMon=(Month.charAt(0)); //grabs first character
   IsMon=(IsMon+(Month.charAt(1))); //adds second character
   IsMon=(IsMon+(Month.charAt(2))); //adds third character resulting in Month abbreviation
   IsDay=(rtc.getDOWStr());
   IsDate= (t.date);
   IsYear= (t.year);

 //Time pieces for proper display & blanking
   IsHour=(t.hour);
   IsMin=(t.min);
   IsSec=(t.sec);

//Send results to Serial Monitor
 Serial.print("Today is ");
 Serial.print(IsDay);
 Serial.print(" ");
 Serial.print(IsMon);
 Serial.print(" ");
 Serial.print(IsDate);
 Serial.print(", ");
 Serial.println(IsYear);
 Serial.print("The time is ");
 Serial.print(IsHour);
 Serial.print(":");
 Serial.print(IsMin);
 Serial.print(":");
 Serial.println(IsSec);
 Serial.println(" ");

 
delay (1000);
}

Parts of the above code are clipped from other people’s examples. I understand most all of it, but there are a couple of things I’m not clear on what they do and why.

The output looks like this:

Today is Tuesday Nov 9, 2017
The time is 20:17:1

Today is xxxxxxxxx xxx 0, 2000
The time is 0:0:0

Today is Tuesday Nov 9, 2017
The time is 20:17:3

Today is xxxxxxxxx xxx 0, 2000
The time is 0:0:0

Today is Tuesday Nov 9, 2017
The time is 20:17:5

Today is xxxxxxxxx xxx 0, 2000
The time is 0:0:0

Today is Tuesday Nov 9, 2017
The time is 20:17:7

Today is xxxxxxxxx xxx 0, 2000
The time is 0:0:0

Today is Tuesday Nov 9, 2017
The time is 20:17:9

Today is xxxxxxxxx xxx 0, 2000
The time is 0:0:0

Today is Tuesday Nov 9, 2017
The time is 20:17:11

Today is xxxxxxxxx xxx 0, 2000
The time is 0:0:0

I’m off to the recommend threads to see if I can find the problem.

  • a -

Bill's link number two describes exactly what I'm experiencing (no idea how I missed that during my search :confused: ). one of the last entries indicated that 5v presented the invalid data and 3.3v did not

I found the same thing except that on 3.3v the data read was valid each 1 second loop, but the time only updated every other loop. Time was accurate, just not updated

Output:

Today is Tuesday Nov 9, 2017
The time is 21:6:18

Today is Tuesday Nov 9, 2017
The time is 21:6:18

Today is Tuesday Nov 9, 2017
The time is 21:6:20

Today is Tuesday Nov 9, 2017
The time is 21:6:20

Today is Tuesday Nov 9, 2017
The time is 21:6:22

Today is Tuesday Nov 9, 2017
The time is 21:6:22

Today is Tuesday Nov 9, 2017
The time is 21:6:24

Today is Tuesday Nov 9, 2017
The time is 21:6:24

Today is Tuesday Nov 9, 2017
The time is 21:6:26

Today is Tuesday Nov 9, 2017
The time is 21:6:26

FWIW

  • a -

Also, in that thread, Snejq suggested “Try connecting i/o by the 1k resistor” but I have no idea what he means by that.

  • a -

AjaxOfTheRockies:
Also, in that thread, Snejq suggested "Try connecting i/o by the 1k resistor" but I have no idea what he means by that.

  • a -

He means to pull-up the I/O with 1K resistors to 5V

Thanks for the clarification!
So, I just tried that but no change. Also tried more delay before the next loop and read. No change - every other read is trashed.

One of the threads you linked me to mentioned a bad crystal. I have some new ones and will try replacing it on one of the modules.

I'll report back results. Also, will try a different Arduino if I get time tonight.

  • a -

FWIW, new crystal had no effect

  • a -

following links in the links Bill provided, I found this code

http://playground.arduino.cc//Main/DS1302?action=sourceblock&num=1

and it appears to work. Time reported has been correct each second for the last ten minutes.

It works, but good grief it is Comp. Li. Cate. ED. :fearful:

Anyway, that confirms that the Uno is good, the module is good, my code is not.

The above code is not usable for me at this point because I understand almost none of it. I will work through it to see if I can find what is wrong with the simple version I came up with.

Any further input from readers is REALLY appreciated!

  • a -