Tuesday, June 4, 2013

Arduino RTC: TinyRTC v1 with Arduino Mega 2560

This post is slightly out of sync with my previous ones. I was digging around in my parts box and found my real time clock (RTC) module, a TinyRTC v1. I then realized that I had not posted any of my findings when I used it. Well I had a few minutes today, so I decided to dust off the RTC and see if it was still working.

A real time clock is something many new hobbyist might take for granted. Living in a world of computers with integrated RTCs and internet connections, it's easy to forget just what it takes to keep track of the time. While any Arduino can give you the time since it's last restart (or pretty close to it anyway). To keep track of the time displayed on your cuckoo clock you will need some external hardware and a continuous power supply. Various people have come up with  good combinations of said hardware, and all you need to do is buy a RTC module.

The module I will be using is a Tiny RTC v1 module. They are commonly found on Ebay called "Real Time Clock DS1307 I2C AT24C32" or similar. Communication is done over an I2C interface. It has a battery on-board that can supposedly last for several years.

I didn't really remember how I set up the RTC, so I started digging around. I found 2 sketches that I picked up from somewhere. I believe the Ebay seller posted them. I tried them out, and they do work as expected. SetRTC sets the RTC with a time you hardcode into the sketch. GetRTC simply displays the time given from the RTC. Both sketches require the Wire library and the I2C address. To find the I2C address, use THIS I2C scanner.

First wire it up. Connect SCL and SDA to the appropriate pins (21 and 20 on the Arduino Mega 2560). Connect Vcc to 5v and GND to GND. Ignore the rest of the pins. To the right is a diagram of the connections for an Uno if you are confused. Next set the time in the function and upload SetRTC to your Arduino. Then hit the reset button at the moment you want to set the clock.

Now we can upload the GetRTC. Open the Serial Monitor and watch the seconds tick by. An interesting note, if you unplug the GND and reattach it, the time gets corrupted, and you will have to reset the time.

Now, while this method works, I would be remiss if did not mention the Time library. It has many other functions that may be useful depending on your situation. I have not explored them, but I assume it works well. To set the time using the library you will need to download Processing. By using a Processing sketch, you can sync the clock's time to that of your computer.

That's all for today. Go forth and make data loggers, binary clocks, and other exciting projects.
-Matthew

3 comments:

  1. Well, my experience with Tiny RTC is that Cant keep time while ardruino is not powered. I have 2 of them and tested with Uno and Mega. Please test this and report if you can.

    1) sync your TinyRTC to actual time with any method you like. (by code, by Processing, by hand, etc)

    2) power on and off your arduino proyect several times ( I did 10 times with 10 seconds waiting each step) making sure you are ONLY sync-ing Tiny RTC to arduino clock and NOT resyncing with external source as done in step 1. This test will show you that Tiny RTC has lost several seconds.

    3) power off your arduino proyect for 15 minutes and reconnect. At this time you surely lost minutes in the Tiny RTC

    If this is not your experience, please post library you are using and code.

    Many people are experiencing this.

    http://zygzax.com/2013/04/22/reloj-con-arduino-y-el-modulo-tiny-rtc-i2c/
    http://forum.arduino.cc/index.php?topic=177297.0

    Thanks.

    ReplyDelete
  2. I've experienced that some samples of the 1307 RTC from ebay doesn't work with battery backup. Other samples do. So it may be a defective RTC in issue here.

    ReplyDelete
  3. This does seem likely. Sometimes you get what you pay for. I've had other parts from Ebay not live up to expectations.


    I have been wanting to go back and test my RTC again. I just haven't had time. Too many other things are ahead of it on the list.

    ReplyDelete