The Adafruit DS3231 is a precision real-time clock (RTC) module that maintains accurate timekeeping even when the main power to the device it's connected to is turned off. Utilizing a temperature-compensated crystal oscillator, the DS3231 provides high stability and accuracy. Common applications include timekeeping in embedded systems, data logging with timestamps, and scheduling in various electronic projects.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (2.3V to 5.5V) |
2 | GND | Ground connection |
3 | SDA | I2C Data line |
4 | SCL | I2C Clock line |
5 | SQW | Square Wave/Interrupt output |
6 | 32K | 32kHz Output |
7 | RST | Reset input (active low) |
#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, setting 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();
// Print the current date and time
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();
// Wait for a second
delay(1000);
}
Q: How do I set the time on the DS3231?
A: The time can be set using the rtc.adjust(DateTime(...));
function in the Arduino sketch.
Q: Can the DS3231 be used in low-power applications? A: Yes, the DS3231 has a very low power consumption, especially when running off the backup battery.
Q: What is the purpose of the 32K pin? A: The 32K pin outputs a 32kHz square wave that can be used as a clock reference for other devices.