Question regarding RTC libraries

First, I'd like to say I'm quite new to working with electronics and programming; in fact, my specialization in university and graduate school was microbiology, and not anything to do with computers/electronics.

That being said, I have good computer knowledge (building/Windows OS/networking), but have very little actual programming knowledge (did a little bit of Pascal back in the day...)

Needless to say, the Arduino IDE (from what I understand, an extension of C++) is quite intimidating, but I've managed to work my way through some of the more basic examples, and am working on trying to get a more complicated project.

Right now, I'm trying to get a DS1307 breakout board working, but for the life of me, can't seem to get it to work. Hardware wise, I have it connected to an Arduino Mega 2560.

The Vcc is connected to the 5V pin, the ground to ground, SDA to SDA and SCL to SCL. I have tried with and without pull up resistors (connected between 5V-SDA and 5V-SCL).

Using the Adafruit library (GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library) and the following code, I can get the RTC to work just fine.

  // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
void setup () {
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
void loop () {
    DateTime now =;
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(, DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);
    Serial.print(" since 1970 = ");
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print(future.month(), DEC);
    Serial.print(, DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(future.minute(), DEC);
    Serial.print(future.second(), DEC);

However, when I try to use Henning Karlsen's RTC library (Electronics - Henning Karlsen) and the following code:

   // DS1307_Serial_Easy (C)2010 Henning Karlsen
// web:
// A quick demo of how to use my DS1307-library to 
// quickly send time and date information over a serial link

#include <DS1307.h>

// Init the DS1307
DS1307 rtc(20, 21);

void setup()
  // Set the clock to run-mode
  // Setup Serial connection

  // The following lines can be commented out to use the values already stored in the DS1307
  //rtc.setDOW(SUNDAY);        // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(3, 10, 2010);   // Set the date to October 3th, 2010

void loop()
  // Send Day-of-Week
  Serial.print(" ");
  // Send date
  Serial.print(" -- ");

  // Send time
  // Wait one second before repeating :)
  delay (1000);

All I get is

xxxxxxxxx 85.85.2165 -- 27:85:85

Anyone have any clue about this?

One might wonder why not just use the Adafruit library and coding, and not Henning Karlsen's, but that is because the other project I am working on is based around his libraries and recoding it would be too big of a hurdle for me right now.

// I assume you know how to connect the DS1307.
// DS1307:  SDA pin   -> Arduino Digital 4
//          SCL pin   -> Arduino Digital 5

I assume you know that the I2C pins are analogue pins 4 & 5?


// I assume you know how to connect the DS1307.

// DS1307:  SDA pin   -> Arduino Digital 4
//          SCL pin   -> Arduino Digital 5

I assume you know that the I<sup>2</sup>C pins are **analogue** pins 4 & 5?

On the Arduino Mega2560 board, I thought the I2C pins were the SDA and SCL pins (pins 20 and 21, respectively)?

I will give it a try plugging it into the analog pins 4 and 5....

Sorry - missed the point that you were using a Mega.
Maybe lose the comment?

Sorry - missed the point that you were using a Mega.
Maybe lose the comment?

I've removed the confusing comment that was in the coding; it's strange how some other libraries (Adafruit, Matt Joyce's old library that compiles in pre-1.0 Arduino IDE) work seemingly without problem.

The only difference that I can discern is that the other libraries use Wire.h whereas Karlsen decides to forgo using Wire.h.

Hmm, I guess I'll have to rewrite my code if nobody else has any ideas.

Edit: I managed to figure out how to adjust the time using the Adafruit library, but now in trying to rewrite the coding, I am getting another error:

RTClib\RTClib.cpp.o: In function `date2days':
C:\Arduino-1.0.3\libraries\RTClib/RTClib.cpp:26: multiple definition of `i'
Project_1.cpp.o:(.bss.i+0x0): first defined here
c:/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
c:/arduino-1.0.3/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Warning: size of symbol `i' changed from 1 in Project_1.cpp.o to 2 in RTClib\RTClib.cpp.o


I haven't the faintest clue how to fix this.

I am wondering if it might be easier just to figure out what is going on with the Karlsen library...

...All I get is

xxxxxxxxx 85.85.2165 -- 27:85:85

Anyone have any clue about this?

I've figure out this:
without TFT it is working fine
when TFT is on the place - it give this result :xxxxxxxxx 85.85.2165 -- 27:85:85
Did you find a solution?

I was also using a TFT Touch screen when I encountered that problem.

I tried testing various values of pull up resistors, and it turns out that it required 4.7k Ohm (I was using a higher value before, and it did not seem to like it).

does it work now?

It works just fine.

with TFT and 4.7k Ohm resistors?

Yes, I am using the TFT Touchscreen LCD with 4.7k Ohm resistors and using the sample sketch that Henning Karlsen wrote with no problems.

I did switch from a DS1307 to a DS3231, but using the exact same code still works (all I did was change one component).