A Real-Time Clock (RTC) Module is an electronic component that maintains an accurate track of the current time and date. It is commonly used in applications such as data loggers, clocks, servers, and other systems where timekeeping is crucial. The RTC module typically includes a quartz crystal and a battery to keep the time updated even when the main power is off.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Connect to 3.3V or 5V power supply |
2 | GND | Connect to ground |
3 | SDA | Serial Data Line for I2C communication |
4 | SCL | Serial Clock Line for I2C communication |
5 | SQW | Square Wave/Output Driver (optional use) |
6 | 32K | 32kHz Output (optional use) |
7 | RST | Reset Pin (optional use) |
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc; // Replace with your specific RTC library and object
void setup() {
Wire.begin();
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);
}
Remember to consult the datasheet of your specific RTC module for more detailed information and troubleshooting tips.