Library for DS1307 Real-Time Clock

Hello guys, I wrote a library to make setting and getting date/time from DS1307 easier on Arduino.
DS1307, from Dallas Semiconductor, is the one of the most famous RTC ICs. It uses I²C (or Two-Wire Interface - TWI) to communicate with Arduino.

That are a lot of examples on the Web showing how to use Wire library to get and set date/time. This library is just a layer above Wire library to make DS1307 RTC easier to use.

The code is available at my GitHub account: GitHub - turicas/DS1307: Arduino library for reading and setting date and time for the DS1307 Real-Time Clock IC

Please let me know what do you think and if it can be included in official Arduino examples on the IDE.

Looks like interesting work. How does this differ from the already existing Time library?

http://www.arduino.cc/playground/Code/Time

Interesting coincidence... I just wrote a library for the 1307, too :slight_smile: Was trying to share mine through playground, but the registration page seems to be down. I'll look into putting it somewhere.

The main difference between my library and the other two is that I need to use the square wave output and the chip's battery-backed RAM memory (rather than burn out the EEPROM). So am providing functions for more of the nitty-gritty details of the chips capabilities.

I appreciate the mention of the Playground code; even though it's far simpler than I need, I'm still new at Arduino and C++, so models that show how to do the extern thing to set up the class for the user are helpful. (Also wondering about intializing Wire for the class object. What if other libraries work with other two-wire devices? Are we going to have scope issues for the Wire object? Multiple Wire objects in different scopes? But that's something for another thread.)

Was looking at your code, alvarojusten, saw the function to represent the day of week as a string. Don't know if you noticed, but all the clock does in increment the day of the week by one (rolling from 7 to 1) every time it hits midnight. It does not seem to have any fixed association between the date/month/year and what day of the week it is. Convenient in some ways as you are perfectly able to define 1=Sunday, 1=Monday... or 1=Thursday for that matter (if you can get the hang of Thursdays...).

Similarly, it is perfectly happy to let you set the date to Feb 30... and then it continues to the 31st. Seems to know if the current m/d/y is the last day of the month -- and if so, it will roll to the next month at midnight. But it won't force the date to be valid for the month/year.

Sincerely, I didn't know this implementation before I start my implementation - I only know the links showing Wire examples in SparkFun product page.
The "Time" library is very very complete compared to my library - but I think for now my library is optimized for what I want (know date/time) -- it have a lot of features that I don't need (NTP, time deltas etc.). The Time library also "polutes" the namespace since it add a lot of functions (hour(), minute() etc.) instead of creating a class, instantiate it etc.

davidhbrown:
Was looking at your code, alvarojusten, saw the function to represent the day of week as a string. Don't know if you noticed, but all the clock does in increment the day of the week by one (rolling from 7 to 1) every time it hits midnight. It does not seem to have any fixed association between the date/month/year and what day of the week it is. Convenient in some ways as you are perfectly able to define 1=Sunday, 1=Monday... or 1=Thursday for that matter (if you can get the hang of Thursdays...).

Similarly, it is perfectly happy to let you set the date to Feb 30... and then it continues to the 31st. Seems to know if the current m/d/y is the last day of the month -- and if so, it will roll to the next month at midnight. But it won't force the date to be valid for the month/year.

I didn't read about the day of week in the datasheet and don't know if DS1307 have calendar capabilities yet. If yes, so I can determine the day of week based on year, month and day. If no, it is just another information we can get (it'll increment from 1 to 7 for us each 24 hour) if we set the right value.
I'll add to ToDo of the project the test of calendar capabilities such months like Feb etc. - I don't know what DS1307 stores if we add a date that "does not exist". Thanks!

Had a chance to learn Git and put what I've done up at GitHub - davidhbrown/RealTimeClockDS1307: Yet another DS1307 Real-Time Clock library for Arduino (obsolete).

My goal in creating yet another DS1307 library was to provide easy access to some of the other functions I needed from the chip, specifically its square wave output (for an interrupt) and its battery-backed RAM (to allow configuration info to persist across restarts of the Arduino. (It's annoying, but I suppose necessary, that the Arduino restarts every time you open the Serial Monitor.)

I've included an example sketch which allows you to set most everything interactively from the Serial Monitor. Also a Fritzing board based on the Sparkfun module.

davidhbrown:
Had a chance to learn Git and put what I've done up at GitHub - davidhbrown/RealTimeClockDS1307: Yet another DS1307 Real-Time Clock library for Arduino (obsolete).

My goal in creating yet another DS1307 library was to provide easy access to some of the other functions I needed from the chip, specifically its square wave output (for an interrupt) and its battery-backed RAM (to allow configuration info to persist across restarts of the Arduino. (It's annoying, but I suppose necessary, that the Arduino restarts every time you open the Serial Monitor.)

I've included an example sketch which allows you to set most everything interactively from the Serial Monitor. Also a Fritzing board based on the Sparkfun module.

Nice! What do you think in merging these features in just one library?

(the Time library has) a lot of features that I don't need (NTP, time deltas etc.)

The Arduino build tools are smart enough to only include code that is used by your sketch so code that is not used does not consume resources. The advantage of a rich library is that if you later want to add enhanced capabilities you can easily do so without having to change code to use another library.

The Time library also "polutes" the namespace since it add a lot of functions (hour(), minute() etc.) instead of creating a class, instantiate it etc.

The first version of this library used class members for time values but the person responsible for defining the Arduino API (David Mellis) suggested the current form is more Arduino friendly

The old code is no longer supported but you can see it here: Arduino Playground - DateTime

Perhaps we should contunue here the discussion started in this thread: http://arduino.cc/forum/index.php/topic,66054.15.html