RTC DS1307 Module Time Setting problem


I’ve got an RTC DS1307 module from Sparkfun. Hardware connections are as follows:
SDA- Analog pin #4, SCL-Analog pin #5, 5v-5v Arduino, and gnd-gnd Arduino are connected. Two 1/8w 4.7k’s were connected as pull-up resistors for SDA and SCL.

I found that john at tronixstuff.com has prepared wonderful step-by-step tutorials for the RTC ds1307.
I tried the pde example from tronixstuff.com, Example 7.3(pdf), with minimum changes(date, time,…).
The code was compiled, and uploaded to Arduino Diecimila.(setting mode)
The code section for the time-setting was then commented out for ‘running time’ mode. Again, it was compiled, and uploaded to the Diecimila, and Serial monitor was initialized.

The result?
45:165:15 165/165/165 Day of week:165
45:165:15 165/165/165 Day of week:165
45:165:15 165/165/165 Day of week:165
45:165:15 165/165/165 Day of week:165
45:165:15 165/165/165 Day of week:165
This output pattern is not very consistent, though.
I am not sure whether this problem is due to the RTC or the pde.

Any suggestions are welcome.

Example 7.3  -- YR, DATE, TIME adjusted---
reading and writing to the Maxim DS1307 real time clock IC
based on code by Maurice Ribble
17-4-2008 - http://www.glacialwanderer.com/hobbyrobotics
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
return ( (val/10*16) + (val%10) );
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
return ( (val/16*10) + (val%16) );
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
// Reset the register pointer
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
void setup()
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.

second = 0;
minute = 12;
hour = 23;
dayOfWeek = 7;
dayOfMonth = 25;
month = 7;
year = 10;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
void loop()
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
Serial.print(hour, DEC);// convert the byte variable to a decimal number when being displayed
if (minute<10)
Serial.print(minute, DEC);
if (second<10)
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print(month, DEC);
Serial.print(year, DEC);
Serial.print(" Day of week:");
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
 Serial.println(dayOfWeek, DEC);


See code at bootom of this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257362679

Hi, Ron:

I really appreciate your prompt help. The interactive time setting mode is very convenient. I used the sketch shown at the bottom of the thread. It works fine upto the time setting mode. I typed in the values for the prompt each step.

It still gives me the following: Enter 'y' to set clock Enter 2 dig year Enter 2 dig month Enter 2 dig day of month (1-31) Enter day of week where 1=SUN 2=MON 3=TUE 4=WED 5=THU 6=FRI 7=SAT Enter clock mode - 12 or 24 Enter 'a' or 'p' for AM or PM Enter 2 dig hour (1-12) Enter 2 dig minute 25:165:15 PM 165/165/165 ² 25:165:15 PM 165/165/165 ² 25:165:15 PM 165/165/165 ² 25:165:15 PM 165/165/165 ² 25:165:15 PM 165/165/165 ² 25:165:15 PM 165/165/165 ²

Well, then would the problem lie in the hardware setting or the RTC module?

Thank you again for your help.

I would check your circuit and connections. I see the SparkFun module has a 0.1uf decoupling capacitor across pins 4 & 8 - that's good. I use 10k pullups across SDA SCL and SQW.

Hi, Ron C:

After a couple of more trials with my Sparkfun RTC module, I moved to a breadboard type trial with RTC DS1307, as shown by tronixstuff.com. This time, I've removed the pullup resistors. With your interactive Arduino sketch, setting the date and time is really convenient.

It works perfect!

Monitor ouput------ 06:29:50 PM 08/01/10 Sun 06:29:51 PM 08/01/10 Sun 06:29:52 PM 08/01/10 Sun

06:29:53 PM 08/01/10 Sun

btw, I will have a look into the Sparkfun module, which doesn't seem to work very well. ;)

Thank you again, Bryan

I have a board from Futurlec http://www.futurlec.com/Mini_DS1307.shtml and one I made myself- they both work great. - they both work great.

If you bought the pre-assembled module from sparkfun, then you don't need the pull-up resistors. They included everything necessary on the board provided. So just as you found, just connect the data pins directly to the boards.

Just set one up a few weeks ago and that's how mine works.

Ron C: Thank you for your picture.

JerryRigged: With my Sparkfun RTC module, I'll try without the pullup resistors. Thank you for your help.