Home

Awesome

DS1307 high precision I2C RTC library for Arduino

Build Status

This is a DS1307 I2C RTC library for Arduino.

DS1307

Library features

Hardware

Any Arduino hardware with a TWI interface and Wire.h support.

Pins

Pins board - DS1307VCCGNDSDASCLSQW
Arduino UNO (ATMega328 boards)5VGNDA4A5D2 (INT0)
Arduino Mega25605VGNDD20D21D2 (INT4)
Arduino Leonardo5VGNDD2D3D7 (INT6)
Arduino DUE (ATSAM3X8E)3V3GND20212
ESP82663V3GNDGPIO4 (D2)GPIO5 (D1)GPIO0 (D3)
ESP323V3GNDGPIO21GPIO22GPIO0

Note: Tested ESP8266 / ESP32 boards:

Other unlisted MCU's may work, but are not tested.

Examples

Arduino IDE | Examples | Erriez DS1307 RTC:

Documentation

Usage

Initialization

#include <Wire.h>
#include <ErriezDS1307.h>

// Create RTC object
ErriezDS1307 rtc;

void setup()
{
    // Initialize TWI with a 100kHz (default)
    Wire.begin();
    Wire.setClock(100000);
    
    // Initialize RTC
    while (!rtc.begin()) {
        Serial.println(F("RTC not found"));
        delay(3000);
    }
}

Check oscillator status at startup

// Check oscillator status
if (!rtc.isRunning()) {
    // Error: RTC oscillator stopped. Date/time cannot be trusted. 
    // Set new date/time before reading date/time.

    // Enable oscillator
    rtc.clockEnable(true);
}

Set time

// Write time to RTC
if (!rtc.setTime(12, 0, 0)) {
    // Error: Set time failed
}

Get time

uint8_t hour;
uint8_t minute;
uint8_t second;

// Read time from RTC
if (!rtc.getTime(&hour, &minute, &second)) {
    // Error: RTC read failed
}

Set date and time

// Write RTC date/time: 13:45:09  31 December 2019  2=Tuesday
if (!rtc.setDateTime(13, 45, 9,  31, 12, 2019,  2) {
    // Error: RTC write failed
}

Get date and time

uint8_t hour;
uint8_t min;
uint8_t sec;
uint8_t mday;
uint8_t mon;
uint16_t year;
uint8_t wday;

// Read RTC date/time
if (!rtc.getDateTime(&hour, &min, &sec, &mday, &mon, &year, &wday) {
    // Error: RTC read failed
}

// hour: 0..23
// min: 0..59
// sec: 0..59
// mday: 1..31
// mon: 1..12
// year: 2000..2099
// wday: 0..6 (0=Sunday .. 6=Saturday)

Write date/time struct tm

struct tm dt;

dt.tm_hour = 12;
dt.tm_min = 34;
dt.tm_sec = 56;
dt.tm_mday = 29;
dt.tm_mon = 1; // 0=January
dt.tm_year = 2020-1900;
dt.tm_wday = 6; // 0=Sunday

if (!rtc.write(&dt)) {
    // Error: RTC Read failed
}

Read date/time struct tm

struct tm dt;

// Read RTC date/time
if (!rtc.read(&dt)) {
    // Error: RTC read failed
}

Read Unix Epoch UTC

time_t t;

// Read Unix epoch UTC from RTC
if (!rtc.getEpoch(&t)) {
    // Error: RTC read failed
}

Write Unix Epoch UTC

// Write Unix epoch UTC to RTC
if (!rtc.setEpoch(1599416430UL)) {
    // Error: Set epoch failed
}

Square Wave Out (SQW)

rtc.setSquareWave(SquareWaveDisable);	// Disable
rtc.setSquareWave(SquareWave1024Hz);	// 1024Hz
rtc.setSquareWave(SquareWave4096Hz);	// 4096Hz
rtc.setSquareWave(SquareWave8192Hz);	// 8192Hz
rtc.setSquareWave(SquareWave32768Hz);	// 32768Hz

Library dependencies

Library installation

Please refer to the Wiki page.

More Arduino Libraries from Erriez