The Real-Time Clock (RTC) DS3231 is a low-cost, extremely accurate I2C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input, and maintains accurate timekeeping when the 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, data loggers, time stamps, and other time-critical applications.
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 |
To use the DS3231 in a circuit:
#include <Wire.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
// Check if the RTC is connected correctly
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC lost power and if so, set the time
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);
}
rtc.lostPower()
function to check if the RTC has lost power and to set the time again if necessary.Q: How do I set the alarm function on the DS3231?
A: The DS3231 has two alarm functions that can be programmed using the RTClib library. Refer to the library documentation for alarm setup instructions.
Q: Can the DS3231 be used in battery-only mode?
A: Yes, the DS3231 can be powered solely by the battery, but VCC must be connected to a high-impedance state to prevent leakage current from the main supply.
Q: What is the purpose of the 32K pin?
A: The 32K pin outputs a 32kHz square wave that can be used to drive timing circuits or as a clock reference for other devices.