RTClib

repository·master·Indexed 21 days ago

https://github.com/adafruit/rtclib

An 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.

Tokens
3.2K
Snippets
10
Records
17
Agent score
25%

What's inside RTClib

  1. Hardware Compatibility and Pin Assignments

    master

    RTClib is compatible with a wide range of MCUs. Note the following specific pin requirements for I2C (SDA/SCL) communication:

    • ATmega32u4 (e.g., Leonardo, Micro, Yun, Teensy 2.0, Flora, Bluefruit Micro): Use SDA/SCL on pins D3 & D2.
    • ESP8266 (e.g., Adafruit Huzzah): SDA/SCL default to pins 4 & 5, but you can assign any two pins using Wire.begin(SDA, SCL).
    • ATmega2560 (e.g., Arduino Mega): Use SDA/SCL on pins 20 & 21.
    • ATSAM3X8E (e.g., Arduino Due): Use SDA1 and SCL1.
  2. Use the TimeSpan class for time durations

    master

    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:

    • Initialization: Create from total seconds or from components (days, hours, minutes, seconds).
    • Extraction: Retrieve components via .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).
    • Arithmetic: Add or subtract two 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();
  3. Use RTC_Millis for software-based timekeeping

    master

    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:

    • Immunity: It is designed to be immune to millis() rollover events.
    • Initialization: You must call .begin(DateTime) to set the starting time before using it.
    • Usage: Use .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();
    }
  4. Interact with the PCF8523 RTC chip

    master

    The RTC_PCF8523 class provides an interface for the PCF8523 I2C RTC chip.

    Common Tasks:

    • Setup: Call .begin() to initialize.
    • Time Management: Use .adjust(DateTime) to set the time and .now() to retrieve the current time.
    • Timers: Enable/disable the second timer or a countdown timer using .enableCountdownTimer(PCF8523TimerClockFreq clkFreq, uint8_t numPeriods, uint8_t lowPulseWidth).
    • Calibration: Adjust accuracy using .calibrate(Pcf8523OffsetMode mode, int8_t offset).
    • SQW Pin: Configure the SQW pin using .writeSqwPinMode(Pcf8523SqwPinMode).
  5. Interact with the DS3231 RTC chip

    master

    The RTC_DS3231 class provides an interface for the DS3231 I2C RTC chip.

    Common Tasks:

    • Setup: Call .begin() to initialize the chip.
    • Time Management: Use .adjust(DateTime) to set the time and .now() to retrieve the current time.
    • Alarms: Configure two alarms using .setAlarm1(DateTime, Ds3231Alarm1Mode) and .setAlarm2(DateTime, Ds3231Alarm2Mode). You can check if an alarm triggered with .alarmFired(alarm_num).
    • Temperature: Retrieve the chip's internal temperature in Celsius using .getTemperature().
    • SQW Pin: Configure the Square Wave output pin using .writeSqwPinMode(Ds3231SqwPinMode).
    • Power Check: Use .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();
    }
  6. Use the DateTime class to manage date and time

    master

    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:

    • Initialization: Create from Unix time, specific components (year, month, etc.), or ISO 8601 strings.
    • Extraction: Retrieve components via .year(), .month(), .day(), .hour(), .minute(), and .second().
    • Formatting: Convert to strings using .toString() or .timestamp() (supports TIMESTAMP_FULL, TIMESTAMP_TIME, and TIMESTAMP_DATE).
    • Comparison: Use standard operators (<, >, ==, !=, <=, >=) to compare two DateTime objects.
    • Time Conversion: Get Unix time via .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);
  7. Interact with the DS1307 RTC chip

    master

    The RTC_DS1307 class provides an interface for the DS1307 I2C RTC chip.

    Common Tasks:

    • Setup: Call .begin() to initialize.
    • Time Management: Use .adjust(DateTime) to set the time and .now() to retrieve the current time.
    • SQW Pin: Configure the Square Wave output pin using .writeSqwPinMode(Ds1307SqwPinMode).
    • NVRAM: Read or write to the chip's non-volatile RAM using .readnvram(address) or .writenvram(address, data).
  8. Use RTC_Micros for high-precision software timekeeping

    master

    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:

    • Drift Compensation: Unlike RTC_Millis, you can tune this class to compensate for system clock drift using .adjustDrift(int ppm).
    • Constraint: You must call .now() more frequently than the micros() rollover period (approximately every 71.6 minutes) to prevent errors.
    • Initialization: Call .begin(DateTime) to set the starting time.