Arduino due DS1307 RTC library

Hi,

I've tried using RTClib and DS1307RTC libraries but they aren't compatible with the Arduio Due.
I've found this library, but it doesn't use an external RTC.

Any help?

Liad:
Hi,

I've tried using RTClib and DS1307RTC libraries but they aren't compatible with the Arduio Due.
I've found this library, but it doesn't use an external RTC.

Any help?

Hello Liad

Did you have an external crystal?

Witch line did you use?

RTC_clock rtc_clock(RC);
or
RTC_clock rtc_clock(XTAL);

The RTC_clock rtc_clock(RC) should work allways.

Markus

Hi Markus,

I'm using an external XTAL.

I uploaded the Due_Rtc_Simple_Sample once and all worked fine.
When I commented out:

 //rtc_clock.set_time(10, 29, 9);
  //rtc_clock.set_date(22, 10, 2012);

The date was 1.1.2007

Am I missing something?

Liad:
Hi Markus,

I'm using an external XTAL.

I uploaded the Due_Rtc_Simple_Sample once and all worked fine.
When I commented out:

 //rtc_clock.set_time(10, 29, 9);

//rtc_clock.set_date(22, 10, 2012);




The date was 1.1.2007

Am I missing something?

No you missed nothing the 1.1.2007 is the the Resetvalue in the Calenderregister (look at the SAM3X Manuel on page 251) so this Date is set after Powering on. I don't know why this Date is set, but there is nothing wrong if it shown.

So how do I set the time on the DS1307?
I want to set it once, and read the time from it.

Liad:
So how do I set the time on the DS1307?
I want to set it once, and read the time from it.

Btw. there is no DS1307 on the Due Board. the SAM3X has an RTC on Die.

#include <rtc_clock.h>

// Select the Slowclock source
//RTC_clock rtc_clock(RC);
RTC_clock rtc_clock(XTAL);

char* daynames[]={"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

void setup() {
  Serial.begin(9600);
  rtc_clock.init();
  rtc_clock.set_time(10"hours", 29"minutes", 9"seconds");                <- this line set the time you set the hours, minutes and seconds with that
  rtc_clock.set_date(22, 10, 2012);
}

void loop() {
  Serial.print("At the third stroke, it will be ");
  Serial.print(rtc_clock.get_hours());                                            <- with rtc_clock.get_hours() you get the hours as an int
  Serial.print(":");                                                                          same for the fowolling minutes and seconds.
  Serial.print(rtc_clock.get_minutes());
  Serial.print(":");
  Serial.println(rtc_clock.get_seconds());
  Serial.print(daynames[rtc_clock.get_day_of_week()-1]);
  Serial.print(": ");
  Serial.print(rtc_clock.get_days());
  Serial.print(".");
  Serial.print(rtc_clock.get_months());
  Serial.print(".");
  Serial.println(rtc_clock.get_years());
}

May I had to set more comments in the sample.

We're talking on 2 different things.

I didn't know that the SAM3X has an RTC built in it.
I've tried to connect a DS1307 to the Arduino Due.
That is why I didn't understand how your library can help me with my project.

If there is a RTC built-in, how can I save the time there. is there a way to connect a battery to the SAM3X, like the one connected to a DS1307 circuit?

Thanks.

Liad:
We're talking on 2 different things.

I didn't know that the SAM3X has an RTC built in it.
I've tried to connect a DS1307 to the Arduino Due.
That is why I didn't understand how your library can help me with my project.

If there is a RTC built-in, how can I save the time there. is there a way to connect a battery to the SAM3X, like the one connected to a DS1307 circuit?

Thanks.

The SAM3X has connections for an backup-batterie to power the RTC if the "core" is powered of but the Arduino Due doesn't has Pins for that option.

Sorry for that, but this library is only for the build in RTC for an DS1307 you can use any library from the playground or else (so I think) the wire library is allready portet for the Due so they should work.

  1. Assuming I'll manage to solder a battery to the apropriate pins, is there anything else I should do? [force the SAM3X to enter sleep mode or such] or is it enough to connect the battery?

  2. Those RTC libraries I mentioned before doesn't work with an Arduino Due. anyone knows how to fix it?

While trying to use the Time library from the playground [TimeRTC example] I get the following errors:

In file included from TimeRTC.pde:9:
/Applications/Arduino/Arduino 2.app/Contents/Resources/Java/libraries/DS1307RTC/DS1307RTC.h:19: error: 'tmElements_t' has not been declared
/Applications/Arduino/Arduino 2.app/Contents/Resources/Java/libraries/DS1307RTC/DS1307RTC.h:20: error: 'tmElements_t' has not been declared
/Applications/Arduino/Arduino 2.app/Contents/Resources/Java/libraries/DS1307RTC/DS1307RTC.h:27: error: expected ')' before '*' token
/Applications/Arduino/Arduino 2.app/Contents/Resources/Java/libraries/DS1307RTC/DS1307RTC.h:27: error: expected ')' before '*' token
TimeRTC.pde: In function 'void setup()':
TimeRTC:13: error: request for member 'get' in '1074666080u', which is of non-class type 'Rtc*'
TimeRTC:13: error: 'setSyncProvider' was not declared in this scope
TimeRTC:14: error: 'timeStatus' was not declared in this scope
TimeRTC:14: error: 'timeSet' was not declared in this scope
TimeRTC.pde: In function 'void digitalClockDisplay()':
TimeRTC:28: error: 'hour' was not declared in this scope
TimeRTC:29: error: 'minute' was not declared in this scope
TimeRTC:30: error: 'second' was not declared in this scope
TimeRTC:32: error: 'day' was not declared in this scope
TimeRTC:34: error: 'month' was not declared in this scope
TimeRTC:36: error: 'year' was not declared in this scope

Good evening,

I just finished sorting through the DS1307 RTC library that is incombatable with the Due, and I have successfully ported it to the due.

I ran one of the examples and was happily reading today's date and time from the external chip.

I have attached the modified library folder.

Also, I believe the examples are all in the old Arduino Sketch format. You may have to open them and do a save-as to a different location in order to get them to compile.

P.S, if you are using adafruit's DS1307 shield, be careful about the voltage being outputted on the I2C pins. I actually removed the pull-up resistors from mine and used my own resistors on a protoboard so that I could run 3.3V to the data pins.

Good Luck!

RTClib.zip (5.54 KB)

SRG, thank you very much for the library. Thank you, thank you, thank you, thank you...

I did have to do one modification to get it to compile:

RTC_DS1307 RTC;
RTC.begin();
if (! RTC.isrunning())
RTC.adjust(DateTime(__DATE__, __TIME__));
DateTime now = RTC.now();

I changed RTC to rtc

RTC_DS1307 rtc;
rtc.begin();
if (! rtc.isrunning())
rtc.adjust(DateTime(__DATE__, __TIME__));
DateTime now = rtc.now();

I think Marcus in another thread said that compiler does not like the all capital letters variables. I got your library, compiled it, it gave errors. I changed the RTC to rtc, compiled it, it worked. Uploaded to the Adafruit DS1307 board and it worked.

I got a question about the voltage levels for the i2c for the Adafruit DS1307 board and the Due. Could you chime in. I read your post, but I am not sure which resistors you changed. My thread: http://arduino.cc/forum/index.php/topic,149030.0.html

Here's updated version:

Nothing else expect what you said..

RTClib.zip (11.5 KB)

And here's even better..

I've combined Henning Karlsen's library with RTClib and fixed day of week calculation as well. Library does not require wire.h and works just like RTClib except, you can set alternative sda and scl pins if you like.
Library is available here: GitHub - oskarirauta/RTCdue: DS1307 library for DUE that doesn't need wire.h

not working

Hi Guys,
Thank you very much for the libraries and the indications. Just a question for you, HOW to set SDA and SCL pins into the CODE?

For Arduino UNO there were A4, A5 as default pins, but now, with Arduino DUE they don't work.
I've tried with this line of code:
RTC_DS1307 rtc(A4,A5);

instead of RTC_DS1307 rtc;

but it doesn't work even if it doesn't give any errors.
Any solutions??
Thank you.

Hello Alex,

To answer your questions I need to know two things:

  1. Which library are you using?
  2. Which DS1307 shield or breakout board are you using?

In experience with DS1307 and Arduino Due I use only SDA/SCL pins and the Arduino Wire library. Regards,

p

Hello,
I'm trying to connect a RTC2 Proto Version to an arduino due and I can´t.
I used it with an arduino uno with no problems.
I connect the RTC to pins 20 and 21 (SCA and SCL) and upload the code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();

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('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.print(" since 1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");

// 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('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();

Serial.println();
delay(3000);
}

Thanks in advance for some help.
João Rocha

Hello rocha-jppb,

Try the following code. I've been using it with Due without problems.

#include "Wire.h"
#define DS1307_ADDRESS 0x68

void setup(){
  Wire.begin();
  Serial.begin(9600);
  
}

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

byte bcdToDec(byte val)  {
  return ( (val/16*10) + (val%16) );
}

void printDate(){
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  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());
  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);
}

-p