The Real-Time Clock (RTC) DS3231 is a low-cost, extremely accurate I2C real-time clock with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted. The integration of the crystal resonator enhances the long-term accuracy of the device and reduces the piece-part count in a manufacturing line. Common applications of the DS3231 include timekeeping for embedded systems, network servers, data loggers, and other devices that require precise time stamping.
Pin Number | Name | Description |
---|---|---|
1 | 32K | 32kHz Output |
2 | SQW | Square Wave/Interrupt Output |
3 | SCL | Serial Clock Input |
4 | SDA | Serial Data Input/Output |
5 | VCC | Supply Voltage |
6 | GND | Ground |
7 | BAT | Battery Input for Any Standard 3V Lithium Cell or Other Energy Source |
8 | NC | No Connection (Do not connect) |
To use the DS3231 with an Arduino, connect the following pins:
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// The following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
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.print(now.second(), DEC);
Serial.println();
delay(1000);
}
Q: Can the DS3231 be used in battery-powered applications? A: Yes, the DS3231 is suitable for battery-powered applications due to its low power consumption.
Q: What is the purpose of the 32K pin? A: The 32K pin outputs a 32.768kHz signal that can be used to drive a microcontroller timer or as a clock reference.
Q: How do I set alarms on the DS3231? A: Alarms can be set using the RTC library functions. Refer to the library documentation for specific instructions.
Q: What is the battery life expectancy when using the DS3231? A: Battery life depends on the type of battery used and the environmental conditions but typically lasts for 2+ years with a standard 3V lithium cell.