blog

Ultimate DS3231 RTC Module

DS3231 Cover

    As we all know, most of the MCUs used for projects are based on the concept of time. To put it simply, they do not know the exact current time. This is acceptable for most of our projects, but sometimes you might think that maintaining time is the top priority. The DS3231 Precision RTC module is the savior for this problem. It is highly suitable for projects that require data recording, clock setup, timestamps, timers, and alarms.

    In today’s world, time is everything. And for specific electronic products, time is of utmost importance to us. Just like we humans, they also need a way to keep track of time. So how do electronic products manage this? The answer is the RTC clock DS3231, a real-time clock, often referred to as RTC, is a timing device integrated within an integrated circuit or IC. It is used in many applications and devices that require strict timekeeping, such as servers, GPS, and data recorders. Let’s take a look at what makes it tick.

What is DS3231 RTC module?

    The RTC DS3231 module is a low-cost, high-precision I2C real-time clock that features an integrated temperature-compensated crystal oscillator (TCXO) and crystal. This device includes a battery input terminal, allowing it to maintain timing even when the main power is disconnected. The integrated crystal oscillator enhances the long-term DS3231 precision and reduces the number of components on the production line. The DS3231 RTC module offers both commercial and industrial temperature ranges.

    The RTC stores information on seconds, minutes, hours, day of the week, date, month, and year. For months with fewer than 31 days, the end-of-month date is automatically adjusted, including corrections for leap years. The clock’s operation format can be either 24-hour or 12-hour format with AM/PM indication. Two configurable calendar alarms and a configurable square wave output are provided. The address and data are transmitted serially over a I2C bidirectional bus.

    It has two calendar alarms with different precision levels, which can generate interrupt signals on the pins at the specified time.

    The clock accuracy ranges from ±2 ppm at 0℃ to +40℃, meaning that a month may deviate by more than 5 seconds. At -40℃ to +85℃, it is ±3.5 ppm.

    Apart from its high timing accuracy, the DS3231 real time clock module also has several other functions that expand the additional functions and selection range of the system master. The DS3231 internally integrates a very accurate digital temperature sensor that can be accessed via the IIC interface (just like time). This temperature sensor has an accuracy of ±3°C. The on-chip control circuit can perform automatic power detection and manage the power switching between the main power and the backup power source (i.e., a low-voltage battery). If the main power fails, the device can still provide accurate timing and temperature, and its performance is not affected. When the main power is reconnected or the voltage value returns to the allowable range, the on-chip reset function can be used to restart the system microprocessor.

Working Principle

DS3231 Schematic

DS3231 Schematic

RTC DS3231 Module Composition

RTC DS3231 Module Composition

    The key components of a typical DS3231 RTC module board are the DS3231 IC and the AT24C32 EEPROM IC, which are used to store time and date data. Other components include an on-board power-on LED, some resistors, capacitors, a battery holder, and pins for connection to the microcontroller.

DS3231

    When the main power supply is turned off, the DS3231 module incorporates a battery input and maintains accurate time. The built-in power detection circuit continuously checks the status of VCC to identify power loss and automatically switch to the backup power source. Therefore, even if the power is cut off, your MCU can still maintain time tracking. There is a CR2032 battery holder on the DS3231 RTC module. The battery holder for 20 mm 3V lithium button cells is located at the bottom of the circuit board. Any CR2032 battery can be used.

Charging capability:

    The design of this module is that when external power is supplied, it can charge the battery installed on the module. However, be careful when using an unchargeable CR2032 battery, because the module will also charge the battery. CR2032 is an unchargeable 3V battery, but rechargeable batteries can be charged up to 4.2V. To prevent the module from charging the CR2032 unchargeable battery, the U4 (220R) resistor or D1 (1N4148) diode should be desoldered.

DS3231 Pin Functions

Pin Name

Definition

32K

32K Oscillator out

SQW

Square wave output pin

SCL

Serial clock pin (I2C interface)

SDA

Serial data pin (I2C interface)

VCC

Positive pole

GND

Grounding

    When we use an oscilloscope to measure the 32K pin of the RTC DS3231 module, we will obtain a 32kHz signal from the internal oscillator of the IC.

DS3231 Benefits and Features

  • Highly Accurate RTC Completely Manages All Timekeeping Functions

   ① Real-Time Clock Counts Seconds, Minutes, Hours, Date of the Month, Month, Day of the Week, and Year, with Leap-Year Compensation Valid Up to 2100

   ② Accuracy ±2ppm from 0°C to +40°C

   ③ Accuracy ±3.5ppm from -40°C to +85°C

   ④ Digital Temp Sensor Output: ±3°C Accuracy

   ⑤ Register for Aging Trim

   ⑥ /RST Output/Pushbutton Reset Debounce Input

   ⑦ Two Time-of-Day Alarms

   ⑧ Programmable Square-Wave Output Signal

  • Simple Serial Interface Connects to Most Microcontrollers

    ① Fast (400kHz) I2C Interface

  • Battery-Backup Input for Continuous Timekeeping

     ① Low Power Operation Extends Battery-Backup Run Time

     ② 3.3V Operation

  • Operating Temperature Ranges: Commercial (0°C to +70°C) and Industrial (-40°C to +85°C)
  • Underwriters Laboratories® (UL) Recognized Applications

DS3231 Parameters

Supply Voltage VCC: The operating voltage range for the main power supply is from 2.3V to 5.5V.
VBAT: The backup power supply voltage range is from 2.3V to 5.5V.
Working Current Active supply current (ICCA): Under different VCC and VBAT voltages, the current range is from 200μA to 300μA.
Standby power current (ICCS): Under the condition where the I2C bus is inactive, the 32kHz output is enabled, and the SQW output is disabled, the current range is from 110μA to 170μA.
Temperature Conversion Current (ICCSCNV): Under the condition where the I2C bus is inactive, the 32kHz output is enabled, and the SQW output is disabled, the current range is from 575μA to 650μA.
Output Voltage Power supply fault voltage (VPF): Between 2.45V and 2.70V.
Output Current and Leakage Logic 0 output current (IOL): At 32kHz, INT/SQW, and SDA output, the current is 0.4V. When the output is in high impedance state, it ranges from -1μA to +1μA.
Input leakage current (ILI): On the SCL pin, it ranges from -1μA to +1μA.
RST pin I/O leakage current: When the RST is in high impedance state, it ranges from -200μA to +10μA.
VBAT leakage current (IBATLKG): It ranges from 25nA to 100nA when VCC is active.
Output Frequency (fOUT) When VCC = 3.3V or VBAT = 3.3V, it operates at a frequency of 32.768 kHz.
Frequency Stability The relationship between frequency stability and temperature: Commercial-grade products have an error of ±2 ppm within the range of 0°C to +40°C, while industrial-grade products have an error of ±3.5 ppm within the range of -40°C to +85°C.
The relationship between frequency stability and voltage: 1 ppm per volt.
Aging offset: ±5.0 ppm within 0 to 10 years after production.
Temperature Characteristic Operating temperature range: The DS3231S model operates within the range of 0°C to +70°C, while the DS3231SN model operates within the range of -40°C to +85°C.
Storage temperature range: -40°C to +85°C.
Operating temperature: Maximum is +125°C.
Welding temperature: The maximum is +260°C.

    These technical specifications ensure that the DS3231 can provide reliable timekeeping and alarm functions in various applications, while maintaining low power consumption and high precision. Designers can use these specifications to design power management strategies, select appropriate power supply voltages, determine suitable operating temperature ranges, and ensure compatibility with the I2C interface of microcontrollers or other systems.

DS3231 VS DS1307

Feature

DS3231

DS1307

Key Difference

Timekeeping Accuracy

​±2ppm​ (±~1 min/year)

±~23ppm​ (±~1-2 min/​month)

The most critical difference.​​ DS3231 is far more accurate due to its internal temperature-compensated oscillator.

Oscillator Type

​Internal TCXO​ (Temperature Compensated Crystal Oscillator)

​External 32.768kHz Crystal

DS3231 has a built-in, compensated oscillator. DS1307 relies on an external crystal, prone to drift with temperature.

Temperature Sensor

​Yes, with digital output

No

DS3231 uses its sensor for internal compensation and can provide temperature data.

Operating Voltage

2.3V to 5.5V

4.5V to 5.5V

DS3231 has a wider operating voltage range, making it better for 3.3V systems.

Operating Temperature

​​-40°C to +85°C​

0°C to +70°C

DS3231 is suitable for harsher, industrial environments.

Power Characteristics

Integrated temp sensor & TCXO. Periodic temperature compensation causes current peaks but ensures ​very high accuracy.

Simple structure, no temp compensation. Stable power consumption but ​accuracy drifts​ with temperature changes.

This is a ​trade-off between precision and base power consumption. DS3231 uses intelligent, intermittent higher power for stability; DS1307 maintains minimal power at the cost of accuracy.

Typical Use Cases

Data loggers, medical devices, scientific instruments, any application requiring ​high precision​.

Clocks, basic timers, DIY projects where ​cost is key​ and moderate accuracy is acceptable.

DS3231 is for precision; DS1307 is for economy.

Cost

Higher

​Lower​

DS1307 is a classic, low-cost option.

How to choose between these two chips depends on your project requirements:

    ​Seeking high precision, high reliability and a wider temperature range​: Choose the DS3231. Its temperature compensation function ensures that it can provide extremely accurate timing in various environments, making it ideal for data recording, scientific instruments, industrial control, or any project that requires long-term stable operation and does not require frequent time calibration.

    ​Budget limited, basic needs​: Choose the DS1307. For a simple electronic clock, basic timer, or a teaching experiment or DIY project that is not sensitive to a few minutes of error per month, the DS1307 is still a feasible option with its low cost.

In simple terms, the DS3231 is the high-performance choice, while the DS1307 is the economical choice.

DS3231 Arduino

DS3231 Pinout

  • DS3231      Arduino                      OLED      Arduino

       VCC  ——>  3.3V                         VCC  ——>  3.3V

       GND  ——>  GND                        GND  ——>  GND

        SCL  ——>  SCL                          SCL  ——>  A5

        SDA  ——>  SDA                         SDA  ——>  A4

DS3231 Arduino Code

				
					#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"

// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Create RTC object
RTC_DS3231 rtc;

// Days of week names
const char* daysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup() {
  Serial.begin(9600);
  
  // Initialize OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  
  // Clear the display buffer
  display.clearDisplay();
  display.display();
  
  // Initialize RTC
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  // Uncomment to set the RTC to the compile time
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  // Check if RTC lost power and set time if needed
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting time to compile time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // Get current date and time from RTC
  DateTime now = rtc.now();
  
  // Clear display
  display.clearDisplay();
  
  // Set text color
  display.setTextColor(SSD1306_WHITE);
  
  // Display day of week (smallest font)
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print(daysOfWeek[now.dayOfTheWeek()]);
  
  // Display date (medium font)
  display.setTextSize(1);
  display.setCursor(0, 10);
  if (now.day() < 10) display.print("0");
  display.print(now.day());
  display.print("/");
  if (now.month() < 10) display.print("0");
  display.print(now.month());
  display.print("/");
  display.print(now.year());
  
  // Display time (larger but still compact font)
  display.setTextSize(2);
  display.setCursor(0, 25);
  if (now.hour() < 10) display.print("0");
  display.print(now.hour());
  display.print(":");
  if (now.minute() < 10) display.print("0");
  display.print(now.minute());
  display.print(":");
  if (now.second() < 10) display.print("0");
  display.print(now.second());
  
  // Display temperature (smallest font)
  display.setTextSize(1);
  display.setCursor(0, 50);
  display.print("Temp: ");
  display.print(rtc.getTemperature());
  display.print(" C");
  
  // Update display
  display.display();
  
  // Small delay to prevent flickering
  delay(200);
}
				
			

DS3231 Effect Demonstration

DS3231 Application Scenarios

  1. Time synchronization in embedded systems: In embedded systems, the rtc DS3231 can serve as the system clock source, providing accurate time information. Through the I2C interface, the microcontroller can read the current time of the DS3231 clock and synchronize the time when necessary.
  2. Timestamp for data recorder: In data recorder applications, the DS3231 rtc clock can provide an accurate timestamp for each recorded data item. This is very important for subsequent data analysis and processing.
  3. Time synchronization in industrial control systems: In industrial control systems, multiple devices need to maintain time synchronization. The DS3231 can serve as the common clock source for these devices to ensure the accuracy of time synchronization between them.
  4. Temperature monitoring: The built-in temperature sensor of the DS3231 module can be used to monitor the ambient temperature. By connecting the DS3231 to the microcontroller, real-time temperature data can be read and processed or displayed.

Relative Information

DS3231 RTC Module Purchase Link

FAQ

Q:How accurate is the DS3231 RTC module?

    The DS3231 RTC module is ​extremely accurate, with a typical precision of ​±2ppm​ (parts per million), which translates to an error of about ​±1 minute per year. Its integrated temperature-compensated crystal oscillator (TCXO) automatically adjusts for environmental changes, ensuring high reliability.

Q:Can DS3231 keep time without power?

    Yes, the DS3231 can keep time without main power. It features a dedicated backup battery input (VBAT). When the main power supply (VCC) is interrupted, the chip automatically and seamlessly switches to the backup battery (e.g., a CR2032 coin cell) to maintain continuous and accurate timekeeping.

Q:What is the battery life of RTC DS3231?

    The battery life of a DS3231 RTC typically lasts ​3-10 years​ with a standard CR2032 coin cell, thanks to its ultra-low backup current of ​~3 µA. Actual lifespan depends on battery quality, environmental temperature, and how often the internal temperature sensor is triggered.

Q:What does it mean when RTC battery is low?

    A low RTC battery means the backup power (e.g., CR2032 coin cell) is nearly depleted. This risks ​losing time and date settings​ when main power is disconnected, potentially causing system errors or data logging inaccuracies until the battery is replaced.

Leave a Reply

Your email address will not be published. Required fields are marked *