Compatible RTC Breakouts
masterRTClib is designed to work with several Adafruit RTC breakout boards, including:
- DS3231 Precision RTC (standard breakout and Stemma QT versions)
- PCF8523 RTC
- DS1307 RTC
repository·master·Indexed 21 days ago
https://github.com/adafruit/rtclibAn Arduino library for interfacing with Real Time Clock (RTC) modules, optimized for Adafruit breakouts including the DS3231, PCF8523, and DS1307. It provides the DateTime and TimeSpan classes for managing date, time, and durations, and includes support for software-based timekeeping via RTC_Millis and RTC_Micros. Requires Adafruit BusIO.
RTClib is designed to work with several Adafruit RTC breakout boards, including:
RTClib is compatible with a wide range of MCUs. Note the following specific pin requirements for I2C (SDA/SCL) communication:
Wire.begin(SDA, SCL).RTClib requires the following library to function:
To install the RTClib library, open the Arduino IDE, use the Library Manager, search for "RTClib", and select install.
# No CLI command provided, use Arduino Library ManagerdayOfTheWeek() method, the returned value is an integer ranging from 0 to 6 inclusive, where 0 represents 'Sunday'.The TimeSpan class represents a duration of time with seconds accuracy. It is used to perform arithmetic with DateTime objects (e.g., adding or subtracting time).
Key capabilities:
.days(), .hours(), .minutes(), and .seconds(). Note that .hours(), .minutes(), and .seconds() return the component within the larger unit (e.g., .hours() returns 0-23, not total hours).TimeSpan objects.// Create a span of 1 day, 2 hours, 30 minutes, and 5 seconds
TimeSpan span(1, 2, 30, 5);
// Add span to a DateTime
DateTime futureTime = dt + span;
// Get total seconds
int32_t total = span.totalseconds();The RTC_Millis class provides a way to track time using the Arduino millis() function. This is useful when no hardware RTC is available.
Key Characteristics:
millis() rollover events..begin(DateTime) to set the starting time before using it..now() to get the current DateTime based on the elapsed millis().RTC_Millis rtc;
void setup() {
// Initialize with current time
rtc.begin(DateTime(__DATE__, __TIME__));
}
void loop() {
DateTime now = rtc.now();
}The RTC_PCF8523 class provides an interface for the PCF8523 I2C RTC chip.
Common Tasks:
.begin() to initialize..adjust(DateTime) to set the time and .now() to retrieve the current time..enableCountdownTimer(PCF8523TimerClockFreq clkFreq, uint8_t numPeriods, uint8_t lowPulseWidth)..calibrate(Pcf8523OffsetMode mode, int8_t offset)..writeSqwPinMode(Pcf8523SqwPinMode).The RTC_DS3231 class provides an interface for the DS3231 I2C RTC chip.
Common Tasks:
.begin() to initialize the chip..adjust(DateTime) to set the time and .now() to retrieve the current time..setAlarm1(DateTime, Ds3231Alarm1Mode) and .setAlarm2(DateTime, Ds3231Alarm2Mode). You can check if an alarm triggered with .alarmFired(alarm_num)..getTemperature()..writeSqwPinMode(Ds3231SqwPinMode)..lostPower() to check if the RTC has lost its battery backup.RTC_DS3231 rtc;
void setup() {
rtc.begin();
// Set time to a specific DateTime
rtc.adjust(DateTime(2023, 1, 1, 12, 0, 0));
}
void loop() {
DateTime now = rtc.now();
float temp = rtc.getTemperature();
}The DateTime class stores date and time information in a broken-down form (year, month, day, hour, minute, second). It supports dates from 1 Jan 2000 to 31 Dec 2099. It does not handle time zones, daylight saving time, or leap seconds; time is stored in the user's chosen time zone.
Key capabilities:
.year(), .month(), .day(), .hour(), .minute(), and .second()..toString() or .timestamp() (supports TIMESTAMP_FULL, TIMESTAMP_TIME, and TIMESTAMP_DATE).<, >, ==, !=, <=, >=) to compare two DateTime objects..unixtime() or seconds since 2000 via .secondstime().// Example: Creating a DateTime object
DateTime dt(2023, 10, 25, 14, 30, 0);
// Example: Getting components
uint16_t year = dt.year();
uint8_t month = dt.month();
// Example: Formatting as ISO 8601 string
String ts = dt.timestamp(DateTime::TIMESTAMP_FULL);The RTC_DS1307 class provides an interface for the DS1307 I2C RTC chip.
Common Tasks:
.begin() to initialize..adjust(DateTime) to set the time and .now() to retrieve the current time..writeSqwPinMode(Ds1307SqwPinMode)..readnvram(address) or .writenvram(address, data).The RTC_Micros class tracks time using the Arduino micros() function. It is more precise than RTC_Millis but requires more frequent updates.
Key Characteristics:
RTC_Millis, you can tune this class to compensate for system clock drift using .adjustDrift(int ppm)..now() more frequently than the micros() rollover period (approximately every 71.6 minutes) to prevent errors..begin(DateTime) to set the starting time.