DS1307 Real-Time Clock (RTC)

View on Amazon
Overview
About DS1307 Real-Time Clock (RTC)
The DS1307 is a widely used real-time clock (RTC) module designed to maintain accurate timekeeping, including automatic leap year compensation. Unlike the DS1302, it communicates via I²C, simplifying integration with ESP32, Arduino, and other microcontrollers.
⚡ Key Features
- Real-Time Clock Functionality – Tracks seconds, minutes, hours, day, date, month, and year.
- I²C Communication – Easier integration than 3-wire serial RTCs like the DS1302.
- 56-Byte Battery-Backed SRAM – Stores small user data for embedded applications.
- Battery Backup Support – Maintains timekeeping during power loss (no trickle charger like DS1302).
With its I²C interface and reliable backup capabilities, the DS1307 is an excellent choice for data loggers, automation systems, and real-time event tracking. 🚀
🔗 Looking for a different RTC? Check out the DS1302 for a 3-wire alternative with trickle charging.
Get Your DS1307
Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
DS1307 Specifications
Complete technical specification details for DS1307 Real-Time Clock (RTC)
📊 Technical Parameters
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.
Visual Pinout Diagram

Pin Types
Quick Tips
clock: seconds, minutes, hours, day, date, month, year,Leap year compensation up to 2100,56 bytes of battery-backed SRAM for user data
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
switchover to battery backup when VCC fails,Requires external 32.768 kHz crystal
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 VCC | Power | Primary power supply input (4.5V to 5.5V) | Typically 5V for normal operation |
2 GND | Ground | Ground connection | Common ground |
3 SDA | I2C Data | I2C Serial Data line | Bidirectional data line (requires pull-up) |
4 SCL | I2C Clock | I2C Serial Clock line | Clock line (requires pull-up) |
5 X1 | Crystal | 32.768 kHz crystal oscillator input | Crystal input |
6 X2 | Crystal | 32.768 kHz crystal oscillator output | Crystal output |
7 VBAT | Backup Power | Battery backup input (2.0V to 3.5V) | CR2032 battery for timekeeping during power loss |
8 SQW/OUT | Output | Square wave/output driver | Programmable square wave output (optional) |
Wiring 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.
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| DS1307 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 VCC Required | 5V | Primary power supply (5V preferred) | |
2 GND Required | GND | Ground connection | |
3 SDA Required | GPIO21 | I2C data line (with 4.7kΩ pull-up) | |
4 SCL Required | GPIO22 | I2C clock line (with 4.7kΩ pull-up) | |
5 VBAT Optional | CR2032 Battery | Backup battery (3V) | |
6 SQW/OUT Optional | GPIO (optional) | Square wave output (optional) |
address: 0x68 (fixed, not configurable)
prefers 5V operation, but some modules work at 3.3V
resistors (4.7kΩ) required on SDA and SCL
modules include pull-up resistors on board
typically connected to CR2032 coin cell battery (3V)
kHz crystal usually included on module
battery backup when main power fails
RTClib or DS1307RTC library for Arduino/ESP32
accurate than DS3231 (no temperature compensation)
cost alternative to DS3231
DS1307 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
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.
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.
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.
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.
Debugging Tips
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
DS1307 Programming Examples
Ready-to-use code examples for different platforms and frameworks
#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);
}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.#include <stdio.h>
#include "driver/i2c.h"
#include "ds1307.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
void app_main(void) {
i2c_config_t config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000
};
i2c_param_config(I2C_NUM_0, &config);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
ds1307_init(I2C_NUM_0);
ds1307_set_datetime(2023, 12, 4, 14, 30, 0);
while (1) {
ds1307_datetime_t now;
ds1307_get_datetime(&now);
printf("Time: %02d:%02d:%02d\n", now.hour, now.minute, now.second);
printf("Date: %04d/%02d/%02d\n", now.year, now.month, now.day);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}ds1307_set_datetime() function sets the initial date and time, while the ds1307_get_datetime() function retrieves the current time and date in a loop, displaying them on the console.i2c:
sda: GPIO21
scl: GPIO22
sensor:
- platform: ds1307
id: ds1307_time
update_interval: 1s
text_sensor:
- platform: custom
lambda: |-
auto my_sensor = new DS1307Sensor(id(ds1307_time));
return {my_sensor};
sensors:
- name: "DS1307 Date and Time"sensor platform is used to periodically fetch the date and time, which are displayed with an update interval of 1 second.platformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200main.cpp
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA: GPIO21, SCL: GPIO22
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
}
}
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);
}from 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)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.Wrapping Up DS1307
The ESP32 DS1307 Real-Time Clock (RTC) is a powerful RTC sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the DS1307 into your ESP32 project and bring your ideas to life!
Explore Alternative Sensors
Looking for alternatives to the DS1307? Check out these similar sensors that might fit your project needs.
The DS3231 is a highly accurate I²C real-time clock with an integrated temperature-compensated crystal oscillator, providing precise...
The PCF8563 is a low-power real-time clock/calendar with I2C interface, offering timekeeping functions, programmable clock output, alarm and...








