DS1302 Real-Time Clock (RTC)

View on Amazon
Overview
About DS1302 Real-Time Clock (RTC)
The DS1302 is a low-power real-time clock (RTC) chip capable of maintaining accurate time and date, including leap year compensation up to 2100. It features built-in SRAM storage and a 3-wire serial interface, making it easy to integrate into time-sensitive applications.
ā” Key Features
- Real-Time Clock Functionality ā Tracks seconds, minutes, hours, day, month, and year.
- Integrated 31-Byte SRAM ā Provides small data storage for system use.
- Dual Power Supply with Trickle Charger ā Supports battery backup for continuous operation.
- 3-Wire Serial Interface ā Ensures efficient communication with ESP32, Arduino, and other microcontrollers.
With its battery-backed operation and compact design, the DS1302 is perfect for low-power embedded systems, data loggers, and real-time event tracking. š
Get Your DS1302
š” Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
DS1302 Specifications
Complete technical specification details for DS1302 Real-Time Clock (RTC)
š Technical Parameters
DS1302 Pinout
The DS1302 pinout includes power supply pins (VCC1 for primary, VCC2 for backup), a 3-wire serial interface (SCLK, I/O, RST), ground, and crystal oscillator connections (X1, X2).
Visual Pinout Diagram

Pin Types
Quick Tips
clock with seconds, minutes, hours, day, month, year,Leap year compensation up to 2100,31 bytes of SRAM for data storage
trickle charger for backup battery/capacitor,3-wire serial interface (simpler than I2C),Operating voltage: 2.0V to 5.5V
power consumption: <500nA in standby,Requires external 32.768 kHz crystal
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 VCC1 | Power | Primary power supply input (2.0V to 5.5V) | Main power source for normal operation |
2 VCC2 | Backup Power | Backup power supply input | Battery or capacitor for timekeeping during power loss |
3 GND | Ground | Ground connection | Common ground |
4 SCLK | Serial Clock | Serial clock input | Clock signal for 3-wire communication |
5 I/O | Data | Bidirectional data input/output | Data line for 3-wire communication |
6 RST | Control | Reset input (Chip Enable) | Must be HIGH to enable communication |
7 X1 | Crystal | 32.768 kHz crystal oscillator connection | Crystal input |
8 X2 | Crystal | 32.768 kHz crystal oscillator connection | Crystal output |
Wiring DS1302 to ESP32
Connect the DS1302 to your ESP32 using three GPIO pins for the 3-wire serial interface. The module requires a 32.768 kHz crystal and optionally a backup battery (e.g., CR2032) for continuous timekeeping during power loss.
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| DS1302 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 VCC1 Required | 3.3V or 5V | Primary power supply | |
2 VCC2 Optional | CR2032 Battery | Backup power (battery or capacitor) | |
3 GND Required | GND | Ground connection | |
4 SCLK Required | GPIO18 | Serial clock input | |
5 I/O Required | GPIO23 | Bidirectional data line | |
6 RST Required | GPIO5 | Reset/Chip Enable (active HIGH) |
can be 3.3V or 5V (ESP32 uses 3.3V)
typically connected to CR2032 coin cell battery (3V)
pin must be HIGH to enable communication
kHz crystal required (usually included on module)
charger can charge backup battery/supercapacitor
trickle charger carefully to avoid battery damage
DS1302 library (e.g., ThreeWire + RtcDS1302)
write protection before setting time
cost alternative to I2C RTCs like DS1307/DS3231
DS1302 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: The DS1302 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 CE, I/O, and SCLK pins are correctly connected to the appropriate digital pins on the microcontroller. If the problem persists, consider replacing the DS1302 module, as some units, especially from unreliable sources, may be faulty.
Issue: The DS1302 module displays incorrect or corrupted date and time 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 DS1302 module becomes excessively hot during operation.
Possible causes include incorrect power connections, short circuits, or defective components.
Solution: Double-check all power connections to ensure they are correct, with VCC connected to the appropriate voltage and GND to ground. Inspect the module and wiring for any signs of short circuits or solder bridges. If the module continues to overheat, it may be defective and should be replaced.
Issue: The DS1302 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 VCC1 pin to maintain timekeeping during power loss. Ensure that the battery is fresh and properly connected. Verify that the VCC2 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
DS1302 Programming Examples
Ready-to-use code examples for different platforms and frameworks
#include <DS1302.h>
DS1302 rtc(19, 21, 22); // RST, I/O, SCLK
void setup() {
Serial.begin(9600);
rtc.halt(false);
rtc.writeProtect(false);
rtc.setDateTime(2023, 12, 4, 14, 30, 0); // Set initial date/time: YYYY, MM, DD, HH, MM, SS
}
void loop() {
DS1302::DateTime now = rtc.getDateTime();
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);
}DS1302 library simplifies communication with the RTC. The setDateTime() function initializes the RTC with a specific date and time. In the loop(), the current date and time are fetched and displayed on the Serial Monitor.#include <stdio.h>
#include "driver/gpio.h"
#include "ds1302.h"
void app_main(void) {
ds1302_config_t config = {
.rst_pin = GPIO_NUM_19,
.io_pin = GPIO_NUM_21,
.sclk_pin = GPIO_NUM_22
};
ds1302_init(&config);
ds1302_set_datetime(2023, 12, 4, 14, 30, 0);
while (1) {
ds1302_datetime_t now;
ds1302_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));
}
}ds1302_get_datetime() function.uart:
tx_pin: GPIO21
rx_pin: GPIO22
baud_rate: 9600
time:
- platform: ds1302
id: ds1302_time
update_interval: 1s
sensor:
- platform: custom
lambda: |-
auto my_sensor = new DS1302Sensor(id(ds1302_time));
return {my_sensor};
sensors:
- name: "DS1302 Date and Time"time platform is used to interact with the RTC, and a custom sensor is defined to periodically retrieve and display the date and time. The update interval is set to 1 second for regular updates.platformio.ini
[env:arduino_uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 9600main.cpp
#include <DS1302.h>
DS1302 rtc(19, 21, 22); // RST, I/O, SCLK
void setup() {
Serial.begin(9600);
rtc.halt(false);
rtc.writeProtect(false);
rtc.setDateTime(2023, 12, 4, 14, 30, 0);
}
void loop() {
DS1302::DateTime now = rtc.getDateTime();
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 Pin
import time
# Pin definitions
CLK = Pin(22, Pin.OUT) # Clock pin
DAT = Pin(21, Pin.INOUT) # Data pin
RST = Pin(19, Pin.OUT) # Reset pin
# DS1302 commands
READ_TIME = 0x81
WRITE_TIME = 0x80
def write_byte(byte):
for i in range(8):
CLK.value(0)
DAT.value((byte >> i) & 1)
CLK.value(1)
def read_byte():
result = 0
for i in range(8):
CLK.value(0)
if DAT.value():
result |= (1 << i)
CLK.value(1)
return result
def set_time(year, month, day, hour, minute, second):
RST.value(1)
write_byte(WRITE_TIME)
write_byte(second)
write_byte(minute)
write_byte(hour)
write_byte(day)
write_byte(month)
write_byte(year - 2000)
RST.value(0)
def get_time():
RST.value(1)
write_byte(READ_TIME)
second = read_byte()
minute = read_byte()
hour = read_byte()
day = read_byte()
month = read_byte()
year = read_byte() + 2000
RST.value(0)
return year, month, day, hour, minute, second
# Initialize DS1302
RST.value(0)
CLK.value(0)
# Set initial time
set_time(2023, 12, 4, 14, 30, 0)
# Loop to read time
while True:
year, month, day, hour, minute, second = get_time()
print(f"Time: {hour:02}:{minute:02}:{second:02}, Date: {year:04}/{month:02}/{day:02}")
time.sleep(1)set_time() function initializes the DS1302 with the provided date and time by writing data byte-by-byte. The get_time() function reads the current date and time by issuing the appropriate commands and parsing the returned bytes. The main loop continuously fetches and prints the time and date every second.Wrapping Up DS1302
The ESP32 DS1302 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 DS1302 into your ESP32 project and bring your ideas to life!
Explore Alternative Sensors
Looking for alternatives to the DS1302? Check out these similar sensors that might fit your project needs.

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...

DS3231 / AT24C32 Real-Time Clock (RTC)
The DS3231 is a highly accurate I²C real-time clock with an integrated temperature-compensated crystal oscillator, providing precise...

PCF8563 Real-Time Clock (RTC)
The PCF8563 is a low-power real-time clock/calendar with I2C interface, offering timekeeping functions, programmable clock output, alarm and...





