CNAP

DS1307 RTC Reference

A real-time clock (RTC) module that keeps time over I²C, typically battery-backed on real hardware so it retains the date/time through power loss.

Pin names

NameDescription
GNDGround
5VPower (5V)
SDAI²C data
SCLI²C clock
SQWSquare-wave/interrupt output

Attributes

This component has no configurable attributes.

Usage

Connect 5V to 5V, GND to GND, SDA to the board's SDA pin (A4 on an Uno), and SCL to SCL (A5 on an Uno). Read/set the time with the RTClib library.

#include <RTClib.h>

RTC_DS1307 rtc;

void setup() {
  Serial.begin(9600);
  rtc.begin();

  if (!rtc.isrunning()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set to compile time
  }
}

void loop() {
  DateTime now = rtc.now();
  Serial.print(now.year());  Serial.print('/');
  Serial.print(now.month()); Serial.print('/');
  Serial.print(now.day());   Serial.print(' ');
  Serial.print(now.hour());  Serial.print(':');
  Serial.println(now.minute());
  delay(1000);
}

Examples

DescriptionAttrs
Default DS1307 RTC{}

See also

  • MPU-6050 IMU — another common I²C module using the same SDA/SCL wiring pattern
  • SSD1306 OLED — pair with an RTC to display the current time