Computing stuff tied to the physical world

New date / time / RTC library

In AVR, Software on Feb 5, 2010 at 00:01

Not so long ago, I had the opportunity to work a bit on something which has bugged me for a long time – the lack of date and time handling in connection with RTC chips. There are a few libraries out there, but I think I could do better – i.e. make it simpler, smaller, yet sufficiently powerful for real day-to-day use.

Seeing where this was going on the Arduino developer mailing list (and disagreeing with just about everything that happens over there), I decided to put my money time where my mouth is, and build my own library.

Here’s the header file of the new RTClib Arduino-compatible library:

Screen shot 2010-02-04 at 13.52.13.png

This lets you do date / time calculations, and it provides two different ways to implement a clock: via a hardware chip or using the built-in millis() timer.

RTClib has been checked into subversion, see the CODE page for details on how to get it.

It includes four example sketches:

  • datecalc illustrates how to do calculate with dates and times
  • ds1307 interfaces with a DS1307 RTC chip, connected via the Wire library
  • plugrtc interfaces with the RTC Plug, connected via the Ports library
  • softrtc demonstrates how to do the same with just software

One fun trick I added, inspired by a comment from Limor Fried, is to allow initializing a DateTime object from the DATE and TIME strings generated by the C compiler. That means you can run that “softrtc” sketch without hardware support, and it’ll automatically have its clock set to the compilation date of the sketch, i.e. fairly close to correct. Not good enough for general use, but great during quick debug cycles when you’re re-compiling your sketch all the time anyway.

Note that to use RTClib, you need to include the “Wire.h” library – even if you don’t use it!

The inability to properly deal with libraries, particularly in a resource-constrained embedded processor context, is one of the aspects of the current Arduino direction which irritates me – see an older post for more details.

  1. It is recommended that you declare all the memebers of a class as protected or private. That way you can’t access a member function/local by using a pointer to it from a non-friend or child class.

    Just a thought ;) Good work though!

  2. Kind of, but you still have public methods. They should be protected. As far as the internal-class variables, I saw that you did make them private.

    So why do you make all methods public? Is it some kind of interfacing paradigm with C ? Say, I declare a variable and call one of your functions. Will it work if it were only protected, not public?

    Notice I say variable, not object, because I’m talking C.

    • ..I meant I saw you did make the internal, non-shared class variables private in the demo projects..

  3. Awesome, I could use this. I’d already built a more rudimentary, DS1307 specific library for my own use.

    One nice thing to add (at least for the DS1307) would be simply a function like isRunning to check the CH bit. I’m assuming begin() starts the clock using this same bit …

  4. GCC that Arduino IDE uses, optimises away functions (methods) and stuff that are not used. So it shouldn’t matter if you have to #include Wire.h, or whatever library you do (technically they are not really libraries, but let’s pretend they are :).

    I haven’t tested that thoroughfully, but I have noticed that GCC really optimises away functions that I don’t call anywhere in my program.

    • Yes – I see that too for static functions, but not for everything across files. I think the trick is to break up the code into lots and lots of separate source files. But that’s very tedious… will need to investigate further one day, thx.

Comments are closed.