Ds1307 using a MEGA

Hello all

This is a simple problem but I am trying to just read the date and time from a DS1307 module I purchased from spark fun. I modified the code to only Read the time as they set it properly before shipping.

#include <Wire.h>

int clockAddress = 0x68;  // This is the I2C address
long previousMillis = 0;  // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test; 

// 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) );

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
  // Reset the register pointer

  Wire.requestFrom(clockAddress, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.receive() & 0x7f);
  minute     = bcdToDec(Wire.receive());
  // Need to change this if 12 hour am/pm
  hour       = bcdToDec(Wire.receive() & 0x3f);  
  dayOfWeek  = bcdToDec(Wire.receive());
  dayOfMonth = bcdToDec(Wire.receive());
  month      = bcdToDec(Wire.receive());
  year       = bcdToDec(Wire.receive());

  Serial.print(hour, DEC);
  Serial.print(minute, DEC);
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print(dayOfMonth, DEC);
  Serial.print(year, DEC);


void setup() {

void loop() {


I am using a Mega and every single tutorial does not have a single decent tip on using this on anything but a single Uno. Can anyone help me with this? I need to be able to read it from pins 20 or 21 (SDA SCL) or 18, 19 (preferably) (TX1 RX1) Can you explain to me how to change this? I am assuming it is a ling in the library

Thanks much

Can you explain to me how to change this? I am assuming it is a ling in the library

I don't think that there are any lings in the library.

The Wire library is used to interface with devices connected (correctly) to the SDA and SCL pins.

What is it you think you need to change?

No sketch or library code changes needed. Only wiring changes required. Uno (or any 168/328 based board) uses analog pins 4 & 5 for the I2C data and clock signals respectively. The mega uses pins 20 and 21 for those two connections to the RTC board. Select the correct board in the IDE and upload the RTC sketch and it all should work, mine does on either my 328 board or mega1280 board.

You have no pin number choice the I2C hardware inside the controller chip only wires the data and clock signals to specific pins, depending on chip type.


That was “line” in the above post. Pins 20 and 21 work. One weird thing though. SparkFun claims they loaded the time onto it already but I am getting 0:00:00 00/00/0000 repeated. Am I looking at the wrong registers?

Thanks again

With values of all 0s I would think you have a wiring or software problem. I have a test sketch you can try.

Here is the sketch I use (and it works!) if you have the proper DS1307 library code loaded into your user’s libraries folder.
Upload as is and it will print time data to the serial monitor at 57600 baud rate. Uncomment out the time data section in the setup section and change the parameters to todays time and date and then upload again, it will update the new time and date and print values to the serial monitor. Finally re-comment out the setup portion and up load again to just display time and date.

/*Reads the value from a Real Time Clock (RTC) DS1307 and displays it in the serial monitor
*Created by D. Sjunnesson 1scale1.com d.sjunnesson (at) 1scale1.com
*Created with combined information from
*Big credit to mattt (please contact me for a more correct name…) from the Arduino forum
*which has written the main part of the library which I have modified

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by mattt on the Arduino forum and modified by D. Sjunnesson

void setup()
/* // update time/date constants and uncomment block to set new time & date
RTC.set(DS1307_SEC,1); //set the seconds
RTC.set(DS1307_MIN,50); //set the minutes
RTC.set(DS1307_HR,0); //set the hours note 24 hour clock
RTC.set(DS1307_DOW,5); //set the day of the week
RTC.set(DS1307_DATE,14); //set the date
RTC.set(DS1307_MTH,8); //set the month
RTC.set(DS1307_YR,9); //set the year


void loop()

Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
Serial.print(RTC.get(DS1307_SEC,false));//read seconds
Serial.print(" “); // some space for a more happy life
Serial.print(RTC.get(DS1307_MTH,false));//read month
Serial.print(RTC.get(DS1307_DATE,false));//read day
Serial.print(RTC.get(DS1307_YR,false)); //read year




Where did you get the two libraries? (Excluding the wire)

Thanks again

One more thing about the code you posted. Why do you proclaim "false" in the serial.print() command? I am not quite understanding this


psppb: One more thing about the code you posted. Why do you proclaim "false" in the serial.print() command? I am not quite understanding this


It's a function option of the library. when you read the first time element (HR is that example) using true the library goes ahead and reads all the time and date elements at that instant. So when you print the other elements it doesn't bother to ask the RTC for the elements, but just gets the data it received in the first transaction with the RTC. If just makes the sketch run faster.

The other two libraries are just Arduino core libraries that are needed. I got this DS1307 library from the playground over a year ago, and I can't find it there now for the life of me. The arduino playground is kind of a mess to find stuff with no attempt to delete dead links, etc. It's all user required maintenace and like I said it's a mess. If you can't find it and you wish to PM me your email address I can send them to you as attached files?

Here is a different library avalible from Adafruit people, there stuff is usually very good. https://github.com/adafruit/RTClib


I started this thread here… This is for V22, and I did the mods for a Mega 2560. I use the Ethershield and the DS1307 breakout from Sparkfun. (I2C interface)

Complete updated Time.h, DS1307 lib examples and everything – get the latest at the end of the thread. Clock sync routines for UDP/NTP and so on…

Install the Time Library from the Libraries section – then update completely from the work I did – it built on other work published here…

There are functions to use the Square wave generator and read/write the NVSRAM on the module.

I hope you find it useful.


Thanks for all the help. At the moment all I want to do is read the time in and compare it with a variable I created. I have it hooked into pins 20 and 21. I've noticed that it does nothing but output zeros either way the pins are connected. Do I hoke the SDA to the SDA and the SCL to the SCL or is it reversed?

I am still very new to the library creation and am painstakingly going through your code line by line. Do you have any example code or is that in the libraries themselves?

Thank you

Well I fetched out my Mega for the first time and wired it up the DS1307 breakout chip. I was worried about having so much raw power in my hands before (the Mega, not the clock), but your post got me motivated. :wink:

You connect the 4 wires: Gnd, +5V and SDA to D20 (it has SDA marked on it), and SCL to D21 (it has SCL marked on it). Then this sketch worked for me:

#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 = RTC.now();
    Serial.print(now.year(), DEC);
    Serial.print(now.month(), DEC);
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(now.minute(), DEC);
    Serial.print(now.second(), DEC);

I had previously noticed that if you don’t have the chip plugged in at all (or to the wrong pins) it just shows a zero date. But if you have it plugged in, but the time isn’t set it says “RTC is NOT running!”. So you can tell the difference. My module was from Adafruit but it is the same chip. Also make sure you have the backup battery inserted or it won’t work.