Add a Real Time Clock To Your Arduino Project with DS3231 RTC Module
Adding a DS3231 Real Time Clock to your Arduino project makes sense, and it’s easy.
Giving your Arduino project the capability of keeping track of time is handy for a variety of DIY projects. Most obvious to benefit would be a DIY electronic clock. There are other projects which would benefit as well, though. For example, if you have an Arduino based project that uses a type of sensor, such as a temperature or gas sensor, the capability of real-time would allow you to log the readings from the sensor over time. This data could then be charted to provide a bigger picture than just a snapshot in time. Another use of having real time capability might be to perform some function at specific time intervals. You might want to open and close the blinds at certain times of day, or run a motor for an hour, then turn it off for 20 minutes to let it cool, then turn it back on. There are myriad applications for having a stable time reference.
Get a Genuine Adafruit DS3231 RTC Module
Trying to keep accurate time in your Arduino program is extremely difficult. You can of course approximate, but there are unpredictable timing events in the logic flow of nearly every program. There are indeterminate length processes like waiting for the serial port to initialize, a display to be ready, and communicating data over a bus. These things vary in the amount of time they take, and those variations add up over time. Having a separate module keeping track of the time for you is extremely handy. That’s where the Real Time Clock (RTC) module from Adafruit based on the DS3231 IC from Maxim comes in. The DS3231 is a small, highly accurate timekeeper that can operate from 3.3V or 5V and draws 110uA to 300uA, and is built to run from battery power. The designers of the DS3231 placed great emphasis on accuracy. The IC monitors its own temperature and adjusts an on-board capacitance array to compensate for any timing variations that occur with temperature. In addition to being temperature compensated, the DS3231 has an Aging setting that you can use to further fine-tune the temperature compensation adjustment. Most people won’t have a need for such extreme accuracy, but knowing the design has incorporated it provides a great confidence level in the chip.
I read through several reviews on Amazon written by people who bought low-cost DS3231 RTC modules. It seems that many suppliers randomly substitute pin-compatible variations of the DS3231 in their modules. A review from one buyer of a 5-pack said he received 2 modules with one IC on them, and 3 modules with another IC on them. There were functional differences between them. When you buy a DS3231 Real Time Clock module, I highly recommend you buy the one distributed by Adafruit. In some cases the knockoffs are a great way to save money, but in some cases you need to get the real thing, and this is the time to get the real thing. It’s (at the time of writing) $14 on Adafruit.com and $19 on Amazon. And get a CR1220 battery for uninterrupted timekeeping while you’re there.
Additional Capabilities of the DS3231 Real Time Clock
The DS3231 doesn’t just keep accurate time. It also has 2 alarm settings which can be set to function on a recurring basis. The alarms can be set to repeat each second, hour, minute, day, or date. In addition, the DS3231 can activate an interrupt line when an alarm setting is matched, so you could just write an interrupt service routine for handling the alarm, with no need to check its status in your main loop. A Snooze function is not built in, so that’s software you can write. You can also check the alarm status periodically in software if you prefer. If you use that approach, the interrupt output from the DS3231 can be set to provide a square wave output. It can be set to one of 4 frequencies:1Hz, 1.024KHz, 4.096KHz, or 8.192KHz. You could use this square wave to measure the accuracy of the oscillator in the DS3231 and fine-tune the aging registers to get it right-on at a specific temperature, or you might use the 1Hz square wave to trigger seconds indicator of some type, flash an LED, trigger a stepper motor to move one step to move a second hand, who knows?
And if I didn’t mention this yet, the DS3231 has an internal calendar as well. It increments the day value from 1 through 7 and then wraps back to 1. It changes at midnight, and it’s up to you to decide which day is day 1, day 2, etc. The date cycles similarly from 1 through 31 and back, so it’s up to you keep track of months with 28, 29, and 30 days and update the date register when needed. Months work the same way, but from 1 to 12. There are enough year bits to count up to 2199, so that should suffice.
Which Arduino Can Use The DS3231 Real Time Clock?
The DS3231 uses serial communication over the two-wire I2C bus. This makes it compatible with nearly every Arduino board out there. Just confirm there are pins labeled SCL and SDA (or 2SCL and 2SDA) on the board you’re using. I like to develop using a board with lots of capability, and then see what the smallest board I can squeeze my project into is once I’m happy with it. I started this with a ATmega2560 knockoff board – yes, for the main board I have a third-party board, I’ve been happy with it.
I2C support is so prevalent among microcontrollers, the DS3231 is compatible with far more than just Arduino. Virtually any type of microcontroller or minicomputer, from an 8-pin Microchip PIC ATTiny to full-blown computer like Raspberry Pi can interface with the DS3231. You could implement I2C logic in software with 2 GPIO pins, but it’s much easier if I2C is built-in to the controller you’re using. Again, look for a pin labeled SCL (serial clock) and another labeled SDA (serial data).
Connecting and Communicating with DS3231 from Arduino
In the next post, I’ll walk you through the basics of connecting and communicating with the DS3231 RTC module from an Arduino. There are several free third-party libraries available to communicate with the DS3231. For such a simple device, I prefer to write my own interface. For complex devices, other people’s code can be a real time-saver. Unfortunately I find libraries other people write sometimes don’t expose the full functionality of the device, they can be awkward to incorporate into your project, and some are just poorly documented or hard to use. For something as straightforward as this, I’ll be writing my own. I will use the Arduino Wire library to handle the I2C communications though. There’s no point in trying to improve on that.
I’ve also created a fully documented library which you can use when building an Ardiuno alarm clock with the DS3231 real-time clock module. See the link for documentation and the Github repository info.
Like TechIsSoCool on FaceBook, and follow @theTechIsSoCool on Twitter to get notified when new articles are posted. In the meantime, did you see the clock I built with a Raspberry Pi? Check it out!
2 thoughts on “Add a Real Time Clock To Your Arduino Project with DS3231 RTC Module”