Some help with DS1307

Hello
I'm trying to use the DS1307 to see time (for start.....)
This is the connection I have made:
SDA------> Analog IN 4
SCL------> Analog IN 5
5V-->5V
GND-->GND.

this is the code I'm trying to use
//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
Wire.begin();
Serial.begin(9600);
setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
printDate();
delay(1000);
}

void setDateTime(){

byte second = 45; //0-59
byte minute = 40; //0-59
byte hour = 0; //0-23
byte weekDay = 2; //1-7
byte monthDay = 1; //1-31
byte month = 3; //1-12
byte year = 11; //0-99

Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator

Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));

Wire.write(zero); //start

Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}

void printDate(){

// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();

Wire.requestFrom(DS1307_ADDRESS, 7);

int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());

//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);

but when I open the serial print all I see is:
165/165/165 45:165:165

this is the first time I'm using the DS1307 so maybe I need to define something first?

Thanks for the help,

You need pullups for SDA and SCL (for a single device I2C bus 4.7k should be a good value).

this is were I took the code+connection
http://bildr.org/2011/03/ds1307-arduino/

and it didn't say anything about pullups.

can you see this please?

thanks ,

With just the internal pullups it may work but chances are that it don't. See this page for the reasons:

I sometimes had success without pullups, sometimes it failed, depends a lot on the actual circuitry I guess. I would try the pullups, if it then still don't work it's one error source less to look for.

o.k.
thanks
I will go buy 4.7K and cheek again,

Do you have any other resistors at hand in the range 1.5k to 4.7k? If yes, use these.

did it
and stiil get the same time as before
165/165/165 45:165:165

I have notice that even if I remove the power from the DS1307 - I still get the same result.
How is it possible?

after a while I get this

1/1/0 0:0:80

Simply: no return values are checked. If there is an error the code simply continues with some default or old values. The values you get means you read only 1 bits (165 in BCD = 0xff). You don't get that from the DS1307 as in the answer from the chip the highest bit of the seconds (address 0x00) is always 0 and you get a 1 there too. So the numbers displayed are not read from the chip but are a sign that there is no communication.

I guess you don't have an oscilloscope or a logic analyzer to check the electrical signals, don't you?

Have you tried your code without the call to setDateTime()? Your reading code is almost identical to what I'm using to read my DS1307 and that works.

Are you using a breakout board for the DS1307? Have you checked all connections? Can you post a picture of your circuitry? Is the backup battery already in place?

There is a RTC library. Why not use that?

I copied your code which reads and prints the date and time into a sketch I'm using to play with the DS3231 which has the same register layout as the DS1307.
You code works just fine. So it looks like you have a hardware problem somewhere. Double check the connections from SDA to Analog pin 4 and from SCL to Analog pin 5.
Also, get Nick Gammon's I2C Scanner code from here Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino and see if it can even find the DS1307.
When I run that code the result for the DS3231 is:

I2C scanner. Scanning ...
Found address: 104 (0x68)
Done.
Found 1 device(s).

Pete

thank you all ,
now it's working.
i don't know what was the problem - I just reconnect the wires and the device and copied the code again...

Thanks.

now I have another question :

if I want to use the clock as a time stamp, how can I use it?
let say every time the sensor is on he will show me the time

what I want to know is how to take the code for the "viewing time" and where to put it in the main code , and how to call that function when I want to.

?

never mind.
I play with the code try it , and after a few versions able to to this.

Thanks anyway

:slight_smile:

sorry for "jumping"
but
after lest week the RTC has stop working for me
I'm using SparkFun-ProEthernet
I am getting time: 165\165\165 45:165:165

now I have check my wires (and it's all good)
but when I run

Nick Gammon's I2C Scanner code from here Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

I get

Found 0 devices

the BAT gives ~3V , and it's getting 5V
how can I know if the DS1307 is "dead" ?
is there another way to see if the device is working? or to check the A4 and A5 (analog inputs)?

Thanks ,

Something is obviously flaky but it doesn't appear to be the clock. I'd suggest that you remove all the wiring and start again.

Pete

did it 3 times
even took new wires , still doesn't work.
something else maybe?

This is an old post but..
Have you found a solution to this?

I have the same problem. (Tiny RTC module from Ebay)
This happens every time arduino power is lost.
I have tested this with the I2C scanner.

To get connection to the RTC again, I just have to disconnect and reconnect the 5v to the RTC.
Then RTC communicates on the I2C bus.
By the way, adding 4.7K pullups did not help either.

Do somebody else have the same issue?

Per Emil

Are you alluding to this?

but when I open the serial print all I see is:
165/165/165 45:165:165

I believe it is caused by inadequate power.

I had this just a couple of days ago, and all I had done was change the motherboard in the desktop. Getting power from a 9v wall art solved the problem. This is not as inspired as it might sound, there had been signals before about sailing too close to the edge, and I was fortunate in that both my code and proto shield had been proven 100% kosher. An LCD display, or firing up the Ethernet or more likely to be the villain. I see no evidence of that here, but it could be just a bit of slack arsed soldering, which would lead to the same result.

If what I say sounds right but you have no external PS, you might be able to fix it by running off a USB at the back of your box. I was using a non-powered hub in the front.

When I was having this same problem I eventually figured out it was because that code only applies to the Uno board. The SCL and SDA lines are different depending on the board.
Refer here Wire - Arduino Reference for proper connections for the various boards.