

The DS-3231 is a highly accurate real-time clock (RTC) module designed for timekeeping applications. It features an integrated temperature-compensated crystal oscillator (TCXO) to maintain precise time and date information, even under varying environmental conditions. The DS-3231 operates via an I2C interface, making it easy to integrate with microcontrollers and embedded systems. Additionally, it includes a backup battery input to retain timekeeping functionality during power outages.








The DS-3231 module typically has the following pins:
| Pin | Name | Description |
|---|---|---|
| 1 | GND | Ground connection |
| 2 | VCC | Power supply input (2.3V to 5.5V) |
| 3 | SDA | Serial Data Line for I2C communication |
| 4 | SCL | Serial Clock Line for I2C communication |
| 5 | 32K | Optional 32.768 kHz output (used for external timing applications) |
| 6 | SQW/INT | Square Wave/Interrupt output (programmable frequency or alarm interrupt signal) |
Below is an example of how to interface the DS-3231 with an Arduino UNO to read the current time and date:
#include <Wire.h>
#include <RTClib.h> // Include the Adafruit RTC library
RTC_DS3231 rtc; // Create an RTC object for DS-3231
void setup() {
Serial.begin(9600); // Initialize serial communication
Wire.begin(); // Initialize I2C communication
if (!rtc.begin()) {
Serial.println("Couldn't find RTC. Check connections!");
while (1); // Halt execution if RTC is not found
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
// Set the RTC to the current date and time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now(); // Get the current date and time
// Print the current date and time to the Serial Monitor
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
delay(1000); // Wait for 1 second before updating
}
RTClib library is used to simplify communication with the DS-3231. Install it via the Arduino Library Manager.rtc.adjust() function sets the RTC to the current date and time based on the computer's clock when the code is compiled.RTC Not Detected:
Incorrect Time/Date:
rtc.adjust() function to set the correct time and date.No Output on Serial Monitor:
Serial.begin(9600) matches the Serial Monitor's baud rate.Backup Battery Not Working:
By following this documentation, you can effectively integrate the DS-3231 into your projects for reliable and accurate timekeeping.