Keeping time on Arduino projects is not as easy as you might think: if the computer is not connected, the Arduino without power will simply stop working, including its internal ticker.

In order to keep your Arduino in sync with the outside world, you’ll need what’s called a «real time clock module». Here’s how to use one.

What’s the point of Real Time Clock (RTC)?

Your computer is most likely syncing its time with the Internet, but it still has an internal clock that keeps running even without an Internet connection or power off. When you use an Arduino connected to a computer, it has access to the exact time provided by your system clock. This is pretty useful, but most Arduino projects are meant to be used outside of the computer — at that point, any time the power is turned off or the Arduino is restarted, it will have absolutely no idea what time it is. The internal clock will be reset and start counting from zero again the next time it is turned on.

If your project has anything to do with the need for time — for example, my dawn dawn — that will obviously be a problem. In this project, we got around the problem by manually setting the time each night rather crudely — the user pressed the reset button just before going to bed, ensuring the time was manually synchronized. Obviously, this is not an ideal long-term solution.

The RTC module is an optional piece of circuitry that requires a small coin cell battery that keeps timing even when the Arduino is turned off. Once installed, it will last the lifetime of the battery, typically a year or so.


The most popular RTC for Arduino is called TinyRTC and can be bought for about $5-10 on eBay. You’ll likely need to supply your own battery (it’s illegal to ship it overseas to many places) and a few headers (pins that fit into holes you need to solder in yourself).

This is the module I have:

RTK module

It even has a built-in temperature sensor, though the battery will last longer if you don’t use it.

The number of holes on this thing looks pretty scary, but you only need four of them; GND, VCC, SCL and SDA — you can use the corresponding pins on either side of the RTC module. You are talking to the clock using the I2C protocol, which means that only two pins are used — one for the «clock» (the serial communication data clock that has nothing to do with time) and one for the data. In fact, you can even connect up to 121 I2C devices on the same two pins. Visit this Adafruit page for a selection of other I2C devices you can add because there are many!


Connect your TinyRTC module according to the diagram below — the pink DS line is not needed as this is for the temperature sensor.


Then download the Time and DS1307RTC libraries and place the resulting folders in the folder / library .

Exit and restart the Arduino environment to load the libraries and examples.
ds1307rtc menu example

In the DS1307RTC menu you will find two examples: first download and run the example settime — this will set the correct RTC time. The actual code is not worth getting into, just know that you need to run it once to do the initial time sync.

Let’s look at an example of using read test .

#include  #include  #include  void setup() { Serial.begin(9600); while (!Serial) ; // wait for serial delay(200); Serial.println("DS1307RTC Read Test"); Serial.println("-------------------"); } void loop() { tmElements_t tm; if ( { Serial.print("Ok, Time = "); print2digits(tm.Hour); Serial.write(':'); print2digits(tm.Minute); Serial.write(':'); print2digits(tm.Second); Serial.print(", Date (D/M/Y) = "); Serial.print(tm.Day); Serial.write('/'); Serial.print(tm.Month); Serial.write('/'); Serial.print(tmYearToCalendar(tm.Year)); Serial.println(); } else { if (RTC.chipPresent()) { Serial.println("The DS1307 is stopped. Please run the SetTime"); Serial.println("example to initialize the time and begin running."); Serial.println(); } else { Serial.println("DS1307 read error! Please check the circuitry."); Serial.println(); } delay(9000); } delay(1000); } void print2digits(int number) { if (number >= 0 && number < 10) { Serial.write('0'); } Serial.print(number); } 

Please note that we have also included basic library Wire.h - it comes with Arduino and is used to communicate via I2C. Download the code, open the serial console at 9600 baud and watch your Arduino output the current time every second. Wonderful!

The most important code in the example is the creation tmElements_t tm - this is structure, which we will fill with the current time; and™ function which receives the current time from the RTC module, puts it in our structure tm and returns true if everything went well. Add your debugging or logic code to this "if" statement, such as printing out the time or reacting to it.

Now that you know how to get the correct time with an Arduino, you can try rewriting your sunrise alarm project or building an LED word clock - the possibilities are endless! What will you do?

Image Credits: Snootlab Via Flickr

Похожие записи