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.
TinyRTC
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:
It even has a built-in temperature sensor, though the battery will last longer if you don’t use it.