14 September 2012

Yet Another Real-Time Clock

Real-Time Clocks (RTCs) are popular add-ons to microcontroller projects. I am no exception, I have a lot of them kicking around. The most common RTC seems to be the Maxim DS1307. A lot of my RTCs are DS1307s. It's a real workhorse chip, and easy to use. Still, it has disappointed me in a couple ways. For one, it's not always as accurate as I would like. I've used inexpensive no-name crystals and I've used more expensive ones from the top-shelf distributors. Sometimes they don't even seem to operate within the crystal's specs (typically ±20ppm). Not sure why this is, and it certainly could be my fault, but there it is. Another thing is that sometimes I have a 3.3V microcontroller circuit, but the DS1307 requires 5V.

Another alternative is the Maxim DS3231 (or its SPI relative, the DS3234). I love this chip, it is so cool. The integrated, temperature-compensated crystal makes it very accurate (±2ppm from 0°C to +40°C), and it will operate on 3.3V. This addresses all of my gripes with the DS1307. But it is more expensive, nearly $9 from Mouser in single quantities as I write this, where the DS1307 is about half that. (Yes, I know DS1307s can be had for significantly less from other sources.) But the DS3231 is harder to find at discount prices, and is only available in a surface-mount package if that makes a difference to you.

Enter the Microchip MCP79412. It operates off a crystal similar to that used by the DS1307, so the basic accuracy is about the same. But, it can be calibrated by setting an internal register. It will operate down to 1.8V. And while it does require a few more external passive components than the DS1307, it costs only $1.23 in single quantities. I popped for ten and paid $0.98 per copy. Again, it is only available as a surface-mount component.

The MCP79412 has some other cool tricks up its sleeve as well, including alarms, tracking power outages, and EEPROM in addition to SRAM. I've detailed these in the table below.

To summarize, I've been tinkering with the MCP79412 on and off for the last couple months and have come to like it quite well. I designed a breakout board for it (pictures below) and wrote an Arduino library to support it. I've only used three of the ten chips so far, but they have all operated well within the ±20ppm spec of the crystal I chose. One unit seems to be within 2ppm, and so hardly needs trimming.

If you are also using this chip, or would be interested in it, I'd love to hear from you!

PS: For a comprehensive example using the MCP79412, see my Power Outage Logger project.

Real-Time Clock Comparison
Feature MCP79412 DS1307
On-Chip Calibration ±127 ppm N/A
Alarms Dual alarms (single output) N/A
Power Fail/Restore Timestamps Yes N/A
Unique ID 64-bit ID N/A
EEPROM 128 bytes N/A
Battery-Backed SRAM 64 bytes 56 bytes
Vcc 1.8 - 5.5V 4.5 - 5.5V
I2C Interface Clock Frequency 400 kHz (Vcc ≥ 2.5V) 100 kHz
Square-Wave Output 1, 4096, 8192 or 32,768 Hz 1, 4096, 8192 or 32,768 Hz

MCP79412 RTC Breakout Board, Top
MCP79412 RTC Breakout Board, Bottom


  1. Hello,

    Wonderfull work !

    I'm having an issue compiling your job :

    In file included from MCP79412RTC.cpp:25:
    MCP79412RTC.h:102: error: 'time_t' does not name a type
    MCP79412RTC.h:103: error: 'time_t' has not been declared

    I'm running on MacOS and using arduino 1.0.1
    Can you help me solving this?


  2. The time_t data type is defined in the Time.h library, so be sure to install it and #include it. Get it at: http://www.arduino.cc/playground/Code/Time

  3. Thanks for your answer, this is super weird because I installed it and included it. I even can't run the example program from Time library. I should be missing something but ... what

  4. That is odd, not sure what to think but you're barking up the right tree. Make sure the TimeRTC example from the Time library compiles before proceeding. I might reinstall the Time library. I assume there are other libraries installed? Do they work OK?

  5. The other installed libraries are working and compiling. The problem is only with Time and your (really good) library. It's not compiling. Even the TimeRTC has errors :

    TimeRTC.cpp: In function 'void setup()':
    TimeRTC.pde:-1: error: 'RTC' was not declared in this scope
    TimeRTC.pde:-1: error: 'setSyncProvider' was not declared in this scope

    So strange

    1. You have presumably downloaded the Time.zip folder and unpacked it to the Library directory as a single folder 'Time'. It actually contains 3 subdirectories with libraries Time, TimeAlarms and DS1307RTC. Each of these libraries have to put into the Arduino library directory as seperate folders, otherwise they will not be recognised. That solved the problem for me anyway...

    2. THANK YOU! I had moved the libraries into folders with their .h names and nothing worked. Tricky...much to learn.

    3. Did you get it working? See the link below for the procedure for installing libraries. If you continue to have trouble, use the Contact Me tab at the top of the page.

  6. Where can I buy the already made rtc board ?
    If I search onlu with MCP79412 I find only the chip, and not the entire board.

  7. Oby One, thanks for your comment. Please use the "Contact Me" tab at the top of the page so that I can contact you via email. Right now I've just made a few boards for myself. I am expecting more boards soon, if you'd like to try one out, I'd be glad to build one for you.

  8. Jack - you're a lifesaver on this one. I really need the power failure timestamp for my project and this is the only Arduino example I've found with this. I hope more of the community steps away from the DS 1307 to something more accurate like this.

    Anyway, I'm trying to incorporate your set-up in my pcb and I have a few questions. I don't see a parts list, so am trying to figure it all out through Eagle. I can't figure out which crystal you're using. Do you have a part number? Do you have a part number on the battery holder?

    Also, I see a big blue square under the battery that is not connected to anything. What is this?

    If you would sell these around $15, I would just buy it and plug it in. I'm sure others would, too.



    1. Joshua, thanks for the encouragement! I can sell you one if you like, use the Contact Me tab at the top of the page so I have your email address.

      The parts list is at https://github.com/JChristensen/rtc79412

      The blue square is the ground connection for the battery, in Eagle, click the Ratsnest button to fill in the ground planes then the connections can be seen.

  9. This comment has been removed by the author.

  10. Another RTC worth looking at : PCF85063 . NXP already has a breakout OM11059A,598 available for $17

  11. Module works great! Finally found the perfect project to use it in. Blog post coming out Thursday...

  12. Jack, this is great stuff...

    I was hoping you could provide a little guidance. I am trying to run a sketch that is time dependent, and looking to use my DS1307 RTC to keep everything on schedule. I'd like to sync the RTC with my GPS but heron is my problem. It seems that the GPS does not like any pauses, delays or otherwise to interrupt its feed to the arduino. I tried calling the time in a function but cannot get that to work.

    Ideally, I'd like to sync as little as once a day...

    any advice? I'm using TinyGPS and can get both a GPS clock and RTC clock running...

    1. Jim B, I regret that I cannot debug projects via this blog. I'd encourage you to post your inquiry on the Arduino forum. I check it regularly and there are a lot of other helpful people there as well.

  13. CAn you please add Eagle Library for MCP74XXXX.