Skip to content
Home > Components > Logic Components > DS1307 RTC Module

DS1307 RTC Module

An I2C real-time clock (RTC) that maintains the current date and time even when the microcontroller is powered off.

Component Preview

DS1307 RTC ModuleDS1307 RTC

The DS1307 maintains date and time data in BCD (Binary Coded Decimal) format. It communicates with microcontrollers using the I2C bus at address 0x68. The onboard CR2032 battery ensures the clock continues ticking even when main power is removed.

Logic ComponentsI2C (0x68)Time

Overview

Microcontrollers like the Arduino do not inherently know the current time, and their internal millis() counter resets to 0 every time they lose power. The RTC module solves this by providing a permanent time reference.

Pin Reference

PinTypeDescription
VCCpowerPower supply. Connect to 5V.
GNDpowerGround. Connect to Arduino GND.
SDAdigitalI2C Data line. Connect to Arduino A4 (or SDA).
SCLdigitalI2C Clock line. Connect to Arduino A5 (or SCL).
SQdigitalOptional square wave output (1Hz, 4kHz, 8kHz, 32kHz).
*(Note: Some DS1307 modules have a second set of identical pins on the opposite side to allow daisy-chaining multiple I2C devices).*

Configurable Attributes

AttributeTypeDefaultDescription
datetimestring2024-01-01T00:00:00The starting date and time for the simulation clock. You can change this dynamically during simulation by right-clicking the module.
i2cAddressstring0x68Internal I2C address. Generally fixed at 0x68 for the DS1307.

Working Principle

The DS1307 contains 64 bytes of NVRAM, the first 8 of which are reserved for timekeeping registers (Seconds, Minutes, Hours, Day, Date, Month, Year, Control). To read or write the time, you request specific register addresses over I2C.

Wiring Diagram

  1. Connect VCC to 5V.
  2. Connect GND to GND.
  3. Connect SDA to Arduino analog pin A4.
  4. Connect SCL to Arduino analog pin A5.

Wiring Diagram

Example Arduino Code

Install the RTClib library by Adafruit before running this code.

cpp
#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 rtc;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  
  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // Set 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);
}

Simulation Notes

  • In OpenHW Studio, the DS1307 simulates its internal clock oscillator. The clock automatically ticks in real-time as long as the simulation is playing.
  • If you stop and restart the simulation, the clock will reset to the value specified in the datetime attribute unless you write new values to it via I2C code.

Released under the MIT License.