Go Down

Topic: RTC acting strange (Read 4748 times) previous topic - next topic


Any chance of a link to the RTC module you bought? Or is it a home brew? 
Beginners guide to using the Seeedstudio SIM900 GPRS/GSM Shield


Oct 15, 2012, 12:31 am Last Edit: Oct 15, 2012, 12:37 am by megamax42 Reason: 1
Homebrew, but here's my part list:

Product: Plastic Battery Holder 1-cell ø20mm PC 120591-1

Product: Lithium Battery CR2032 Ф20mm 225mAh 3V CR2032

Product: 4.7k Ohm 1/4W Resistor ±1% 4k7 472

Product: 32.768kHz Crystal Cylinder

Product: DS1307 Dallas Maxim 64 x 8 Serial Real-Time Clock DIP8

And I wired it up based on the 'typical operating circuit', except without the scl being pulled to 5.5V.

It's also all wired up on a breadboard.


My confusion was the datasheet at the top has a picture of a 'typical operating circuit', which has both SDA and SCL with a pullup resistor to Vcc, however further down the datasheet it says only SDA needs the pullup resistor and SCL doesn't say anything about the resistor, only that it should have a pullup voltage of 5.5V?

Possibly worded a little awkward, but 'have a pullup voltage...' means also having a pull-up resistor. The I2C standard dictates that both data and clock signal use external pull-ups as these signals are bidirectional so they cannot rely on one side always providing a valid logic voltage level output. The actual value of the pull-up resistor can depend on things like the speed the bus will be operated on, the number of slave modules wired to the bus, that total length of the bus, etc. But 4.7K ohms is a pretty standard recommendations.

Now to confuse you further (not intended, really) one can sometime get away with wiring a I2C device to an arduino board with no pull-ups at all. That is because the arduino I2C library enables the internal 'weak pull-ups' on the open drain clock and data I2C pins. I've run a 1307 I2C RTC module with several of my arduino board without using external pull-ups and it works fine, however that is not per I2C standards and is not be a recommended practice.



Hmm, interesting, I appreciate the informative post, though you are correct about me being confused   XD.

I only have one 4.7k ohm resistor, so I had both SDA and SCL going through that to the voltage, but then that turned the entire system off, outputting nothing to the serial monitor. Is this just a case of requiring another resistor?

One other thing I should note is I have SQW/OUT wired to nothing.


You use the same setup I use on all my phi-1 and phi-2 shields (same source too), literally hundreds of them spread across the world. No reason it won't run unless you made some mistakes. First, make sure you have very short wires. Then make sure you did the pull-up correctly. Actual setup pictures are way better than your picture-perfect diagram. It is totally correct but whether you followed the diagram is up for debate. You can only end the debate by providing actual pictures. Is that clear enough?

Correct connections won't produce this:

I only have one 4.7k ohm resistor, so I had both SDA and SCL going through that to the voltage, but then that turned the entire system off, outputting nothing to the serial monitor.

The following is not going to affect the output:
One other thing I should note is I have SQW/OUT wired to nothing.


Very strange things are going on. I rewired almost everything with shorter wires like you suggested (I was using cheap male-to-male wire connectors and switched to stripping solid core 22 gauge wire) and this time I did nothing to pullup SCL, like before. This time the output seemed to be working, but displayed the time as 3:50, which is approximately the time I tried pulling SCL up with the same resistor as SDA. So I tried pulling SCL up again, and just like the last time I pulled it up, nothing worked and nothing output to the serial monitor. Just afterwards I tried the circuit without pulling up SCL and this time it reads 4:39. The actual time was 4:41, and 4:39 is when I tried to pull up SCL.

So I did this procedure over again, except tried to switch it as fast as possible, between having a pullup resistor and having no pullup resistor for SCL. I found the quicker I can switch it, the more accurate the time displays. So for some reason the chip's time is set when SCL is pulled up, but can only display the time when there's no voltage?

Just took some pictures, they'll up in a couple minutes.


Hmm, interesting, I appreciate the informative post, though you are correct about me being confused   XD.

I only have one 4.7k ohm resistor, so I had both SDA and SCL going through that to the voltage, but then that turned the entire system off, outputting nothing to the serial monitor. Is this just a case of requiring another resistor?

One other thing I should note is I have SQW/OUT wired to nothing.

Using one resistor wired to +5vdc and then to both SDA and SCL signal lines results in just shorting the two signal wires to each other, a total disaster as wiring schemes go. Yes each signal line requires its own separate pull-up resistor.
No problem not using the SQW/OUT pin, it's an optional feature that you are not required to use.



Come on with your pics already!!!


Oct 15, 2012, 03:08 am Last Edit: Oct 15, 2012, 03:10 am by megamax42 Reason: 1
Sorry!  :smiley-red: Had to sprint out the door to radioshack to get another 4.7k resistor before they closed.

Well, it seems to be working now that I have two separate resistors. It's still approximately one minute behind but I'm not too bothered by that.

And now everything is printing in the correct place, the format for the serial monitor is:

Current Date/Time
# of seconds and days since 1970
One week and 30 seconds from now Date/Time

For example:
2012/10/14 18:0:56
since 1970 = 1350237656s = 15627d
now + 7d + 30s: 2012/10/21 18:1:26

Before today, when the circuit was semi-working, and I didn't have SCL pulled up with a resistor, it would occasionally count time correctly. The odd thing is the actual date and time would always print in the "One week and 30 seconds from now Date/Time" slot.
Whereas now the actual date and time prints in the "Current Data/Time" slot, as it should.

Anyways, thanks for everyones help and clarification. This was driving me nuts!

Here's the pictures if you were still curious:
The Battery on the right kind of got cut off, but that's what the red and black wires disappearing go to.

No Pullup resistor

Additional pullup resistor added


If you want a more permanent setup, this one will help (you only need to get the PCB):


I made these a while back before I designed the phi-1 and phi-2 shields.

Oh wait, your battery holder is different from mine :(


Thanks for the advice! This would definitely look a lot cleaner on a PCB, and since they're so cheap maybe I'll just order a new battery holder.

Also thank you very much robtillaart for the updated code! It compiled perfectly the first time, and worked very well in the serial monitor.

The only thing I'm currently stuck on now is the time that is set to the arduino when I compile/upload is about 5 minutes ahead of the actual time. According to the code it should be set exactly to my computers time, but it's consistently ~5 minutes fast, with both robtillaart's code and the unmodified code I've been using. I'm very confused as to what could be causing this error. I've also tried ungrounding the battery each time to 'reset' the DS1307 chip.


about 5 minutes ahead of the actual time.

If it is just at start-up you might compensate it by add/sub enough seconds,
better is try to catch the root cause, which might be in the RTC lib ?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


a longer term observation is needed to see if the clock is fast or not. If you can leave your RTC on for weeks and check later, then you will know if it runs slow or just has an offset.


Oct 17, 2012, 07:57 am Last Edit: Oct 17, 2012, 07:59 am by megamax42 Reason: 1
Well, increased observation of the serial monitor has brought me back to where I started pretty much. Using both robtillart's code and my original code I get this on the serial monitor:

2012/10/16 22:49:46
since 1970 = 1350427786s = 15629d
now + 7d + 30s: 2012/10/23 22:50:16
2165/165/165 165:85:47
since 1970 = 2028815851s = 23481d
now + 7d + 30s: 2034/4/23 15:58:1
2012/10/16 22:49:48
since 1970 = 1350427788s = 15629d
now + 7d + 30s: 2012/10/23 22:50:18
2165/165/165 165:85:49
since 1970 = 2028815853s = 23481d
now + 7d + 30s: 2034/4/23 15:58:3
2165/165/25 22:49:50
since 1970 = 2016202894s = 23335d
now + 7d + 30s: 2033/11/28 16:22:4
2165/165/165 165:85:51
since 1970 = 2028815855s = 23481d
now + 7d + 30s: 2034/4/23 15:58:5
2165/25/16 22:49:52
since 1970 = 1908548496s = 22089d
now + 7d + 30s: 2030/7/1 16:22:6

And although when I watched the serial monitor for a while after adding the resistor for SCL, it was printing perfectly, now it's messed up again. I also highlighted the dates, as I believe they may be important in troubleshooting this problem. I checked the libraries and downloaded the latest ones so I know they're all good. Could it be a bad chip or would they react differently? :~ :~


Continued analyzing of the serial monitor has shown a certain pattern, where every once in a while the date/time is replaced by one in the year 2165 and a random time, though usually in the hour 5 p.m. or 3 p.m. (15 or 17). This is very troublesome for me because my fade function for the LEDs is based off of decreasing the brightness each second if the condition is met, and the condition is a time. Sometimes the monitor will display 15 correct values, sometimes every other value will be in the year 2165. I can't wrap my head around it

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131