DS1307 Real-Time Clock (RTC)
The DS1307 is a widely used real-time clock module with I2C communication. It supports leap year compensation, battery-backed operation, and 56 bytes of user-accessible SRAM, making it ideal for embedded systems and low-power applications.

On this page
DS1307 pinout
The DS1307 pinout includes I2C communication pins (SDA, SCL), power supply (VCC, VBAT), ground, and crystal oscillator connections (X1, X2). It provides 56 bytes of battery-backed SRAM.
| Pin | Type | Description | Notes |
|---|---|---|---|
| VCC | Power | Primary power supply input (4.5V to 5.5V) | Typically 5V for normal operation |
| GND | Ground | Ground connection | Common ground |
| SDA | I2C Data | I2C Serial Data line | Bidirectional data line (requires pull-up) |
| SCL | I2C Clock | I2C Serial Clock line | Clock line (requires pull-up) |
| X1 | Crystal | 32.768 kHz crystal oscillator input | Crystal input |
| X2 | Crystal | 32.768 kHz crystal oscillator output | Crystal output |
| VBAT | Backup Power | Battery backup input (2.0V to 3.5V) | CR2032 battery for timekeeping during power loss |
| SQW/OUT | Output | Square wave/output driver | Programmable square wave output (optional) |
Real-time clock: seconds, minutes, hours, day, date, month, year
Leap year compensation up to 2100
56 bytes of battery-backed SRAM for user data
I2C interface with standard address 0x68
Operating voltage: 4.5V to 5.5V (VCC), 2.0V to 3.5V (VBAT)
Low power consumption: <500nA in battery backup mode
Automatic switchover to battery backup when VCC fails
Requires external 32.768 kHz crystal
Wiring the DS1307 to ESP32
Connect the DS1307 to your ESP32 via I2C (SDA and SCL pins). The module requires 5V power, a 32.768 kHz crystal, and optionally a CR2032 battery for backup. Pull-up resistors (typically 4.7kΩ) are required on SDA and SCL lines.
| DS1307 pin | ESP32 pin | Purpose |
|---|---|---|
| VCC | 5V | Primary power supply (5V preferred) |
| GND | GND | Ground connection |
| SDA | GPIO21 | I2C data line (with 4.7kΩ pull-up) |
| SCL | GPIO22 | I2C clock line (with 4.7kΩ pull-up) |
| VBAT | CR2032 Battery | Backup battery (3V) · optional |
| SQW/OUT | GPIO (optional) | Square wave output (optional) · optional |
I2C address: 0x68 (fixed, not configurable)
DS1307 prefers 5V operation, but some modules work at 3.3V
Pull-up resistors (4.7kΩ) required on SDA and SCL
Most modules include pull-up resistors on board
VBAT typically connected to CR2032 coin cell battery (3V)
32.768 kHz crystal usually included on module
Automatic battery backup when main power fails
Use RTClib or DS1307RTC library for Arduino/ESP32
Less accurate than DS3231 (no temperature compensation)
Lower cost alternative to DS3231
DS1307 code examples
DS1307 Arduino example
Copy#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()) {
rtc.adjust(DateTime(2023, 12, 4, 14, 30, 0)); // Set initial date/time: YYYY, MM, DD, HH, MM, SS
}
}
void loop() {
DateTime now = rtc.now();
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
Serial.print("Date: ");
Serial.print(now.year());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.println(now.day());
delay(1000);
}This Arduino sketch demonstrates how to use the DS1307 RTC module for timekeeping. The RTClib library simplifies I2C communication and RTC management. The rtc.adjust() function initializes the DS1307 with a specific date and time if it is not already running. In the loop(), the current date and time are fetched using the rtc.now() method and displayed on the Serial Monitor.
DS1307 ESP-IDF example
Copy// Requires the esp-idf-lib DS1307 driver from the ESP Component Registry:
// idf.py add-dependency "esp-idf-lib/ds1307^1.0.8"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "ds1307.h"
#define SDA_GPIO GPIO_NUM_21
#define SCL_GPIO GPIO_NUM_22
void app_main(void)
{
ESP_ERROR_CHECK(i2cdev_init());
i2c_dev_t dev;
memset(&dev, 0, sizeof(i2c_dev_t));
ESP_ERROR_CHECK(ds1307_init_desc(&dev, 0, SDA_GPIO, SCL_GPIO));
// Set the clock once, e.g. to 2026-09-01 12:00:00 (then comment this out)
struct tm time = {
.tm_year = 126, .tm_mon = 8, .tm_mday = 1,
.tm_hour = 12, .tm_min = 0, .tm_sec = 0,
};
ESP_ERROR_CHECK(ds1307_set_time(&dev, &time));
while (1) {
if (ds1307_get_time(&dev, &time) == ESP_OK)
printf("%04d-%02d-%02d %02d:%02d:%02d\n",
time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
time.tm_hour, time.tm_min, time.tm_sec);
else
printf("Could not read time from RTC\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}ESP-IDF ships no DS1307 driver of its own, so this example uses the maintained esp-idf-lib DS1307 driver from the ESP Component Registry. Install it into your project first with idf.py add-dependency "esp-idf-lib/ds1307^1.0.8", then build as usual.
The example sets the clock once with ds1307_set_time() - comment that block out after the first flash, or the RTC will be reset on every boot - and then reads it back every second with ds1307_get_time(), which fills a standard struct tm (note tm_year counts from 1900 and tm_mon from 0). i2cdev_init() sets up the shared I2C layer used by all esp-idf-lib drivers.
DS1307 ESPHome example
Copyi2c:
sda: GPIO21
scl: GPIO22
time:
- platform: ds1307
id: rtc_time
text_sensor:
- platform: template
name: "DS1307 Date and Time"
lambda: |-
char buf[20];
auto now = id(rtc_time).now();
if (!now.is_valid()) return {"unknown"};
now.strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S");
return {buf};
update_interval: 1sESPHome's ds1307 platform is a time source: it reads the RTC over I2C and can also write the current time to it (ds1307.write_time). The custom sensor platform that older examples used for displaying the time was removed from ESPHome in 2025 - the template text_sensor shown here formats the RTC time for display instead. To set the clock, sync it once from Home Assistant or SNTP and call ds1307.write_time.
DS1307 PlatformIO example
Copy[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
adafruit/RTClib @ ^2.1.4#include <Arduino.h>
#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()) {
rtc.adjust(DateTime(2023, 12, 4, 14, 30, 0)); // Set initial date/time: YYYY, MM, DD, HH, MM, SS
}
}
void loop() {
DateTime now = rtc.now();
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
Serial.print("Date: ");
Serial.print(now.year());
Serial.print("/");
Serial.print(now.month());
Serial.print("/");
Serial.println(now.day());
delay(1000);
}This PlatformIO code demonstrates how to use the DS1307 RTC with I2C communication (SDA: GPIO21, SCL: GPIO22). The code initializes the RTC, sets the initial date and time if required, and fetches the current time and date in a loop, displaying them every second.
DS1307 MicroPython example
Copyfrom machine import I2C, Pin
import time
# DS1307 I2C address
DS1307_ADDRESS = 0x68
def bcd_to_decimal(bcd):
return (bcd >> 4) * 10 + (bcd & 0x0F)
def decimal_to_bcd(decimal):
return ((decimal // 10) << 4) | (decimal % 10)
def set_time(i2c, year, month, day, hour, minute, second):
data = [decimal_to_bcd(second), decimal_to_bcd(minute), decimal_to_bcd(hour),
decimal_to_bcd(day), decimal_to_bcd(month), decimal_to_bcd(year - 2000)]
i2c.writeto_mem(DS1307_ADDRESS, 0x00, bytes(data))
# Start the clock by ensuring the CH (clock halt) bit is cleared
control = i2c.readfrom_mem(DS1307_ADDRESS, 0x00, 1)[0] & 0x7F
i2c.writeto_mem(DS1307_ADDRESS, 0x00, bytes([control]))
def get_time(i2c):
data = i2c.readfrom_mem(DS1307_ADDRESS, 0x00, 7)
second = bcd_to_decimal(data[0] & 0x7F)
minute = bcd_to_decimal(data[1])
hour = bcd_to_decimal(data[2] & 0x3F)
day = bcd_to_decimal(data[4])
month = bcd_to_decimal(data[5])
year = bcd_to_decimal(data[6]) + 2000
return year, month, day, hour, minute, second
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Set initial time
set_time(i2c, 2023, 12, 4, 14, 30, 0)
# Loop to read time
while True:
year, month, day, hour, minute, second = get_time(i2c)
print(f"Time: {hour:02}:{minute:02}:{second:02}, Date: {year:04}/{month:02}/{day:02}")
time.sleep(1)This MicroPython script interfaces with the DS1307 RTC over I²C using SDA (GPIO21) and SCL (GPIO22). The set_time() function initializes the DS1307 with the provided date and time, ensuring the clock is running by clearing the CH (clock halt) bit. The get_time() function reads the current time and date from the DS1307, decodes the BCD values into integers, and returns them. The main loop continuously retrieves the current date and time and prints them every second.
DS1307 specifications
About the DS1307
The DS1307 is the RTC most secondhand tutorials still point to: a simple I2C real-time clock and calendar with leap-year compensation to 2100 and 56 bytes of battery-backed SRAM for small application data, replacing the DS1302’s 3-wire interface with a standard two-wire bus that shares pins with everything else on an I2C-based project.
The one spec worth reading carefully is the supply voltage: the datasheet calls for 4.5V to 5.5V on VCC, which makes the DS1307 a genuinely 5V-only chip on paper. In practice it is commonly run at 3.3V on ESP32 projects anyway, and plenty of builders report it working fine outside that spec - but it is outside the spec, and the cheap breakout boards this chip usually ships on often pull their I2C lines up to whatever powers the module itself. Feed one of those boards 5V for a safety margin on the RTC and its SDA/SCL lines idle at 5V too, above what an ESP32 GPIO is rated to see; a level shifter, or sourcing the module’s pull-ups from 3.3V instead, avoids the mismatch. Like the DS1302, the DS1307 has no ppm accuracy spec of its own - its timekeeping rides entirely on the external crystal, so drift in the range of a minute or more per month is typical and worse with temperature swings.
For anything that needs to hold accurate time for months without a resync, the DS3231 drops into the same 2-wire wiring with a temperature-compensated oscillator for a small cost premium, and the PCF8563 undercuts the DS1307 on both power draw and its 1.0-5.5V range if a coin-cell-powered design is the priority.
DS1307 troubleshooting
Incorrect Time or Date Displayed
›
Issue: The DS1307 RTC module displays incorrect time or date information.
Possible causes include improper initialization, incorrect data retrieval methods, or communication errors.
Solution: Ensure that the RTC is properly initialized in your code, disabling write protection and setting the clock to run mode. Use reliable libraries and functions to set and retrieve time data. Verify that the communication between the microcontroller and the RTC is functioning correctly, and consider implementing error-checking mechanisms to detect and handle communication issues.
RTC Not Advancing Time Correctly
›
Issue: The DS1307 RTC module displays a constant time or advances time incorrectly.
Possible causes include insufficient power supply, incorrect wiring, or a defective module.
Solution: Ensure that the module is connected to a stable power source, with VCC connected to 5V and GND to ground. Verify that the SDA and SCL pins are correctly connected to the appropriate digital pins on the microcontroller. If the problem persists, consider replacing the DS1307 module, as some units, especially from unreliable sources, may be faulty.
Communication Issues with Microcontroller
›
Issue: The microcontroller fails to communicate with the DS1307 RTC module.
Possible causes include incorrect I2C address configuration, improper wiring, or lack of pull-up resistors on the I2C lines.
Solution: Verify that the sensor's I2C address matches the address specified in your code; the default address is 0x68. Ensure that the SDA and SCL lines are correctly connected to the corresponding pins on the microcontroller. Check for the presence of appropriate pull-up resistors (typically 4.7kΩ) on the I2C lines if they are not already included on the sensor module.
Time Resets After Power Loss
›
Issue: The DS1307 RTC loses track of time after a power cycle.
Possible causes include a missing or depleted backup battery, or incorrect wiring of the backup power supply.
Solution: Install a backup battery (e.g., a CR2032 coin cell) to the VBAT pin to maintain timekeeping during power loss. Ensure that the battery is fresh and properly connected. Verify that the VCC pin is connected to the main power supply, and that the module is configured to switch to the backup battery when the main power is unavailable.
Where to buy the DS1307

Resources
Similar sensors





