Communications with DS1307

Hello everyone!
I am trying to use the DS1307 clock but I am having some issues with the hardware (I suppose).

First of all, am I compelled to use the analog input 4 and 5?

Then, to the problem: I have connected all the cables as needed and I tried to use the Set_Time example contained in the library DS1307RTC.h.

I have added only a couple of Serial.println lines as I wantend to see where the code freezes. On the serial monitor I see only two printed lines, so the code stops at the first occurrence of a call to the device (line 26 in the code).

The code is the following:

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"

tmElements_t tm;

void setup() {
  bool parse=false;
  bool config=false;
  while (!Serial) ; // wait for Arduino Serial Monitor
  Serial.println("All is fine");

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    Serial.println("All is fine");
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;

  Serial.println("All is fine");
  if (parse && config) {
    Serial.print("DS1307 configured Time=");
    Serial.print(", Date=");
  } else if (parse) {
    Serial.println("DS1307 Communication Error :-{");
    Serial.println("Please check your circuitry");
  } else {
    Serial.print("Could not parse info from the compiler, Time=\"");
    Serial.print("\", Date=\"");
  Serial.println("All is fine");

void loop() {

bool getTime(const char *str)
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;

bool getDate(const char *str)
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;

Can someone show me a solution?

Thank you very much!

What arduino are you using? If its a Mega, sda and scl are on 20 and 21. On the Leonardo, SDA and SCL are digital pins D2 & D3

Can you confirm that the DS1307 is recognized on the i2c bus? Run this i2c scanner program and see what it reports. If it hangs up and does not report a device, run it again with nothing connected to SDA/SCL pins. Does it still hang or report “Found 0 devices”?

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  for (byte i = 8; i < 120; i++)
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

First of all, am I compelled to use the analog input 4 and 5?

There are software i2c libraries, but what reason do you have not to use the hardware i2c pins? It could be a possible work around if the arduino is defective.

Thank you for your reply.

When the device is connected I'm stuck at the output "I2C scanner. Scanning ..." and then nothing happens...

With no device I get no deivices detected: " I2C scanner. Scanning ... Done. Found 0 device(s). "

Should I guess that the clock is defective?

Should I guess that the clock is defective?

Yes. The fact that you "Found 0 device(s). " with nothing connected indicates that the processor chip and arduino board are OK.

If you are using a clock module, you might be able to find and fix a short between SDA/SCL, or between either one of them to ground on the module. You could also have a defective lead between the arduino and module.