The DS3231 real time clock
The DS3231 is a real time clock from Maxim that allows us to measure time with a very good accuracy, making it ideal for many projects that require some kind of timekeeping functionalities, such as logging.
The device can be bought integrated in an easy to use board, as seen in figure 1 below, making it much easier to interact with a microcontroller. One interesting aspect is that the board shown in figure 1 also includes an AT24C32 chip, which corresponds to a 4K EEPROM from Atmel . This board can be bought at eBay for less than 1 euro.
The DS3231 real time clock maintains information about the current second, minute, hour, day, month and year, with automatic adjustments for months with less than 31 days and compensation for leap years .
It also has 2 programmable alarms (generating an active-low interrupt when the clock matches one of the 2 pre-configured times) and a programmable square-wave output, with 4 configurable frequencies (1 Hz, 1.024 kHz, 4.026 kHz and 8.192 kHz) . Those functionalities share a common pin, so we need to configure which one of them is active at a given moment.
The DS3231 also has a pin that outputs a 32 kHz square wave, which corresponds to the signal produced by the internal resonance circuit .
The DS3231 uses a crystal integrated in the chip, with temperature compensation . This compensation is achieved by the existence of adjustable capacitors in the resonance circuit, which are automatically tuned according to the temperature.
This feature is highly relevant since the accuracy of a resonance circuit based on an external quartz crystal is affected by the ambient temperature. So, this is a superior choice when compared, for example, with the DS1307 (also from Maxim) which uses an external quartz crystal .
So, as stated in the product datasheet, the DS3231 can maintain date and hour with an accuracy of ± 2 minutes per year. Taking into account the price mentioned before, this is a very good solution for typical applications that require some kind of timestamping on measurements (for example, IoT applications where we do some measurements of the environment and send them to a remote server).
One interesting feature of the DS3231 is that it actually lets us read the temperature values used to internally compensate the resonator circuit. So, we can also use it to measure ambient temperature.
From the interface point of view, this device can be configured by a microcontroller using the I2C communication protocol, which is a standard and well supported interface. Of course that, if we are using an Arduino to interact with the DS3231, there are a few libraries that simplify the task and provide easy to use functions, without the need to worry with low level details.
The main supply voltage of the DS3231 can be between 2.3 and 5.5 V and it can also be powered from a battery. The DS3231 has a comparator circuit that monitors the main power supply to detect failures and automatically switch to the battery. Internally, the device operates at 3.3 V .
As described above, this is a very good solution for a very low price, which can be used in a great variety of projects. Personally, I’ve used it in my smart medication dispenser prototype, amongst others, with very good results.
This is also a good solution to allow the synchronization of time and date with a external, more precise date and time source, when implementing a timekeeping module in a microcontroller, as I described in a previous post.