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:
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.