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 DS-1307 is often the default choice for an RTC. However, if you need advanced features like alarms or square wave outputs, it’s best to look upmarket.

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)

The DS3234 has the distinction of using an SPI interface instead of I2C. It also packs in 256 bytes of user-addressable RAM.

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

The RV-1805 draws so little power that this Sparkfun module gets by without a backup battery at all. Instead, a supercap keeps time for up to 35 days without external power.

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 RTC Pi, a cheap and simple solution to keeping the Pi in sync when away from the Internet.

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!



Source link