When it comes to measuring time on microcontrollers, there’s plenty of ways to go about things. For most quick and dirty purposes, such as debounce delays or other wait states, merely counting away a few cycles of the main clock will serve the purpose. Accurate to the tens of milliseconds, they get the average utility jobs done without too much fuss.
However, many projects are far more exacting in their requirements. When you’re building a clock, or a datalogger, or anything that relies on a stable sense of passing time for more than a few minutes, you’ll want a Real Time Clock. So called due to their nature of dealing with real time, as we humans tend to conceive it, these devices take it upon themselves to provide timekeeping services with a high degree of accuracy. We’ve compiled a guide to common parts and their potential applications so you can get things right the first time, every time.
Requirements Are Key
The RTC you choose for a given project will come down to a variety of factors. Battery-powered projects will benefit from a low power draw, while scientific measurement devices may need the ultimate in accuracy over time. Interfaces are important too, as implementing an RTC in an already-existing design may necessitate part choice due to the availability of I2C or SPI. Budget is not commonly a problem for the home experimenter in this space, as parts are cheap, however a production run in the thousands will make huge savings in quantity from a cheaper part. As always, having a clear idea of the project requirements is key to choosing the right component for the job.
I Just Need To Know What Time It Is!
Suggested Part: DS1307
The DS1307 has the proud title of being the de facto RTC for those experimenting with microcontrollers. Running off 5 V, with a 500 nA current draw on battery backup, it’s the barebones solution for a project that needs to keep track of time. Unlike more advanced solutions, the DS1307 relies on an external 32.768 KHz crystal for timekeeping. As per the datasheet, this means the accuracy of the timekeeping is somewhat affected by the design implementation, due to varying capacitances and noise. Maxim don’t provide a proscribed accuracy value for this reason, but for the average clock build or basic datalogger, the DS1307 will do the trick. DS1307 modules are readily available, and often the cheapest RTC solution available at most outlets.
I Need Something Accurate, With Alarms!
Suggested Part: DS3231 (I2C), DS3234 (SPI)
For those that need accuracy they can bank on, an RTC with an integrated oscillator is key. These devices guarantee accuracy from the factory, as there’s no user design error possible in the installation of the crystal. Parts like the DS3231 and DS3234 fit this bill, with their temperature-compensated crystal oscillator (TCXO) boasting accuracy as good as ±2 ppm in temperatures from 0°C to +40°C. This comes at a sacrifice to standby current, with 1500 nA more typical, meaning backup batteries don’t last as long.
These parts are more modern, able to operate at voltages between 2.3-5.5V, making them easier to use with a variety of different microcontrollers. They also feature square-wave and alarm outputs. These can be used to generate clocks for other hardware, or to wake microcontrollers out of sleep via a GPIO pin set as an interrupt. RTCs are commonly used in this way to enable battery powered projects to get the most out of their batteries, by keeping the main microcontroller in a low-power sleep mode and only waking up when necessary. There’s also a bonus temperature sensor onboard that can be read out, which may come in handy in some projects.
These modules are marketed with terms like DeadOn due to their accuracy. The additional features and more sophisticated hardware does come at a higher price, however.
I Need An RTC That Barely Sips Power, And All The Rest!
Suggested Part: RV-1805, RV-3028
When it comes to battery power, every last bit of juice matters. For projects deployed out in the field, it’s even more crucial, as it’s often not practical to regularly change batteries or guarantee an ongoing supply of energy from alternate sources like solar or wind.
For applications like these, the RV-1805 and its newer replacement, the RV-3028 come in. Complete with alarms and other high end features, their party piece is an absolutely miniscule current draw. With a special mode that relies on a low-power RC oscillator with a crystal as calibration backup, the RV-1805 draws just 22 nA in timekeeping mode. This is low enough that Sparkfun’s module specifies only a supercap, which is capable of keeping the set time for a full 35 days, where other RTCs must rely on batteries instead. The RV-3028 ups that to 100 nA, with Pimoroni claiming a potential run time of 9 years on the included battery. Accuracy is also top notch with these parts (±2 ppm RV-1805, ±1 ppm RV-3028), making them perfect for accurate datalogging in far-flung environments.
Special Mention: Raspberry Pi Modules
The Raspberry Pi, like many computers from the 1980s, lacks a real time clock. This obviates the need for a battery, but can also cause havoc with system logging and other duties, particularly in non-networked environments. Thankfully, many of the above listed RTC devices are available in special modules designed for the Raspberry Pi’s particular form factor. They’re available in all kinds, from the humble DS1307 to the razor-sharp DS3231.
Installing these requires a little work. The vast majority communicate over I2C, which can be a headache for projects already using the interface for other purposes. It’s also necessary to install a utility to communicate with the RTC and use it to properly update the system time where necessary.
Conclusion
Whether you’re trying to make a sweet timepiece for your loungeroom, or maintain accurate debug logs for a complicated system, accurate timekeeping is key. Thanks to the ready availability of modern hardware, it doesn’t have to be too hard. We hope this guide helps you on your way, and when your project is done, be sure to drop us a line. Happy hacking!
There are a bunch on Tindie as well, with the side benefit of helping support Hackaday’s parent company and individual designers: https://www.tindie.com/search/?q=rtc
I note that the RV-1805 and RV-3028 accuracy specs are specified at 25 deg C, but have no temperature compensation.
Also, saying that “backup batteries don’t last as long” for chips like the DS3231 leaves out a bunch of details. The averaged timekeeping current for a DS3231 (which assumes periodic temperature compensation) is around 3 microamps at 3.3V. A CR2032 coin cell battery would power the chip for about 9 years, which is essentially the shelf life of the battery itself.
Sure, different projects have space, size, and cost constraints, but in general I’ve found any project that needs reasonably accurate time would do far better with a DS3231 over a DS1307.
They have crystal for calibration when such variation occurs. RC’s frequency might drift heavily by temperature, but crystals do not. As long as change in temperature is not too sharp, this configuration is good to go in my opinion.
I’m a big fan of crystals for timekeeping, but crystals absolutely do drift heavily with changes in temperature. Take a look at the RV-3028 datasheet: it’s +/- 1ppm at 25 deg C, but is nearly 8ppm off at 40C, which can be easily reached in direct sunlight. At 0 deg C it’s nearly 22ppm off. The DS3231 is specified to stay within 2ppm between 0-40 deg C, and within 3.5 ppm from -40 to +85 deg C since it has built-in temperature compensation while the RV-3028 (and other ordinary crystals) do not.
Sure, an environment with a stable temperature will keep a crystal much more accurate than one outdoors, but having temperature compensation built into the chip makes things a lot more convenient.
Be careful with the DS3231 chips and modules, especially the modules from China. The DS3231 modules from China use chips labeled DS3231M which are less stable than the DS3231S parts (MEMS vs. VCTCXO oscillators). Even then, many of the Chinese DS3231M modules don’t keep time per the DS3231M data sheet, they may be fakes (or repackaged DS1307 chips?) Also most of the Chinese DS3231 modules have a lousy backup-battery charge circuit that should be disabled. I detail all of this in my detailed comment here:
https://hackaday.com/2020/09/19/code-for-hackers/#comment-6279686
Good advice. I’ve taken apart several DS3231s and DS3231M’s (see https://hackaday.com/2017/08/09/get-inside-a-tcxo-clock-chip/ for a starting point and a link to my site) and detailed their inner workings.
The Chinese crystal-based ones seem to be legit (but possibly remarked on the package) and meet spec in my experience, though that was several years ago and I have no way of telling if they’re actually meeting the “SN” level spec they’re marked with but they worked well enough for several years at room temperature. It seems like the supply of cheap crystal ones has dried up — all the stuff they’re selling now claim to be Ms on the packaging but the eBay (and other site) listings say “SN”.
As for the MEMS being 1307s in disguise, that’d be unlikely: the 1307 has an external crystal that would be really obvious on a board. I wonder what they are on the inside. If you want to send me one (my email is pete@[my username].com and I can send you my address) I’d be happy to try to take it apart and examine its guts.
Check out the RV-8803-C7 then. Outperforming the DS3231 at ±1.5 ppm 0 to +50°C and a current consumption of 240nA at 3V.
For precision or low power, MicroSemi seems to be quite top currently.
Thanks!
Setting time in raspberry pi as as simple as calling stime() which part of posix time functions. As long as your linux application can interface with RTC clock using wiring pi, libusb, usb-uart, etc, you can set time using it. And add this executable to cron tab for every 1 minute and for start up and shut down and you are good to go. (for services that require timing right at the boot, you probably need to configure it as systemd compatible service as well)
It would be nice to mention how much 1ppm is in real world terms. It’s about 2.6s of drift per month.
There also are GNSS modules that have an internal RTC and can output time without having a GNSS signal.
I’ve had recurring problems with external crystals failing during usage, which I haven’t experienced with the 3231, for what that’s worth. I’ve also had a lot of (as yet unsolved) issues with trying to get a SPI-based RTC and an sdcard to share SPI lines with just separate CS lines to each, on an arduino. That should work but doesn’t, so I end up using software SPI to the one I use less.
I’m not sure if this is helpful, but a lot of SPI SD card breakout boards don’t play nice with other SPI devices:
https://forum.arduino.cc/index.php?topic=584416.0
Not just SD cards; my company once had to abandon a peripheral board product that used a programmable ADC with up to 24 bits resolution because the CS* signal wasn’t used to qualify the clock and data lines. The part kept its shift registers active, and just looked for a rising edge on CS* to move data from the shift registers into the programming registers.
The board hadn’t sold well enough for us to justify a redesign that would prevent transitions on the clock and data lines from reaching the chip unless CS* was low.
I bet you’re right. Cool find. Thanks!
This article is grossly shoving aside so much…
Firstly, most crystals used for uC’s are the cheapest, which often give you an accuracy of 200ppm and these also have quite bad drift specifications. But if you can live with that, they are perfectly viable for data logging applications and lots of fairly accurate timing and the whole “You need RTC” is nonsense. It just needs a little bit of thought to setup. Use a hardware timer to generate a periodic ISR, every ms, or 10ms is an easy number, and from that ISR do the rest of the counting.
With this the uC used is pretty much irrelevant, and no extra hardware needed. If 200ppm is too much drift ( One ppm = 24*60*60/1e6 = 0.0864 = 86ms per day, 200ppm is 17.2s/day) the first consideration should be a more accurate crystal. A quick search for cystals at digikey / mouser / farnell / etc. gives a price of 31ct for a 10ppm crystal. For more accurate timekeeping you can go to compensated crystal oscillators. Oven Controlled Crystal Oscillators (OCXO) are maybe a bit out of fashion these days, but TCXO (Temperature Compensated) oscillators are common, and routinely go to 100ppb (B for Billion, not M from million) So of you put the budget of such an external time keeping device into a better oscillator for the uC itself you can easily outclass most RTC chips on the time keeping accuracy. And all by just making a 1kHz ISR and counting from that. TCXO with 100ppb accuracy start at around EUR15. (And go upto at least EUR100 )
Another thing that is completely skipped over is that there are many uC’s which have an option to add an extra 32kHz crystal for time keeping purposes, and these can run from a different power domain and periodically wakeup the CPU.
Then there is also the realm of low power microcontrollers.
This is a market that is continually shifting to ever lower power levels.
Some have specifications of 500nA per MHz and I have not even tried hard to find really low power uC’s. Sleep modes may be down to single digit nA these days.
Adding a complete chip just to do some timekeeping for your uC is mostly just the easy and lazy way out.
Just because the manufactures of those time keeping chips make big advertisements of how “wonderful” their latest / newest and probably slightly improved new chip is, does not mean it is the best fit for your application.
FYI: I have 2 projects on github that uses a firmware based RTC. One is called Timer and the other one is LED-Clock.
https://github.com/FPGA-Computer/LED-Clock
The accuracy of these 12MHz crystals are horrible as they are very old and around 150ppm uncalibrated. I have implemented a 24-bit NCO inside a 20Hz ISR. The 10Hz from NCO is used for updating the RTC. The 3 lines of code NCO allows a very high resolution firmware trim for the offset. I have implemented Day light saving time and calendar.
One is just below my window and the other one is in my living room next to my computer. The temperature range is about 25C to 30-ish. It is not as tightly as it would be if I had air conditioning. The one by my computer runs slower in the winter and faster in the summer. Both of these use regular crystals and achieved +/-1 sec/month.
I use time dot is for calibrating – just a binary search based on faster/slower. The final calibration is over a few days. It turns out Windows time on my PC has a very large drift even with automatic sync.
AFAIK: STM32F103 has a basic counter based RTC while the STM32F030 has a RTC with claendar and 20-bit trimming that increments/decrements a time offset periodically.
An OCXO is nicely stable but normally contradicts battery operation (except e.g. car batteries).
The reason your old time wristwatch was so good was the human oven attached. 5hose that took watches off at night say a much higher drift.
I was most interested in which RTCs are just simple counters vs RTCs which have formatted years, months, days… seconds in different register fields. The former are far more versatile, because at some point you’re likely to have to convert from Formatted representations to UTC l, eg when you need to work out time differences. Unfortunately, that aspect seems to have been left out.
DS1307 does offer square wave output, but no alarm.
The DS3234 is nice, but very expensive. ($8 USD in single quantities!) I prefer the NXP PCF2129AT/PCF2127AT. They have similar advertised accuracy (±3ppm) but are less than $3 USD. However, they can’t do 5V like the DS3234, which makes them less Arduino-friendly.
I have used ST’s [M41T62](https://www.st.com/en/clocks-and-timers/m41t62.html) for years and it is a pretty good stand-alone RTC. Accurate to +/- 1 ppm with calibration and requiring no externals but a bypass cap. It is also super small. It is basically the same as the RTCs embedded in all STM32 MCUs. I was able to get ~18 days of power using a 3V3 11 mF [supercap](https://www.digikey.com/en/products/detail/taiyo-yuden/PAS3225P3R3113/3662197).
Watch out for fake/defective DS3231s on eBay! I got some dirt cheap ones that lose tim elike crazy, but my Nixie clock I built that has a real one in it has only lost 2 seconds since “spring forward”
Don’t forget inexpensive GPS modules with PPS outputs like the NEO-6M. They’re higher power usage than most RTCs and won’t work in every environment, but for $5-10 they offer access to atomic clock level time accuracy.
UPDATE: See this announcement: https://www.microcrystal.com/en/products/real-time-clock-rtc-modules/rv-1805-c3-not-recommended-for-new-design-in/