Overview

The DS1302 is a classic trickle-charge real-time clock (RTC) chip manufactured by Maxim Integrated (Analog Devices). Included in low-cost Elegoo, SunFounder, and Keyes Arduino starter kits, it is packaged as a compact 5-pin breakout module featuring an onboard 32.768 kHz32.768\text{ kHz} tuning fork crystal and a CR2032 coin cell battery holder.

Communicating over a simple 3-wire synchronous serial interface (CE, I/O, SCLK), the DS1302 maintains full BCD timekeeping registers (seconds, minutes, hours, date, month, day-of-week, and year with leap-year compensation up to 2100) along with 31 bytes of battery-backed static RAM.

Quick reference

Operating voltage (VCC2)2.0 V to 5.5 V DC (3.3 V or 5.0 V nominal)
Primary backup voltage (VCC1)2.0 V to 5.5 V DC (CR2032 3V coin cell battery)
Interface3-Wire Serial Interface (CE / RST, I/O / DAT, SCLK / CLK)
Crystal frequency32.768 kHz32.768\text{ kHz}
Backup power draw<0.3 μA< 0.3\ \mu\text{A} at VCC1=2.0VV_{CC1} = 2.0\text{V}
Static RAM31 bytes battery-backed SRAM for user data storage
Timekeeping formatBCD (Seconds, Minutes, 12/24-Hour, Date, Month, Day, Year)
Trickle ChargerSoftware-selectable charging diode & resistor network for rechargeable batteries

Pinout

Breakout module 5-pin 0.1" (2.54 mm) connector header:

text
        ┌───────────────────────────────┐
        │  [DS1302 IC]  [32.768kHz]     │
        │  [CR2032 Coin Cell Holder]    │
        └─┬─────┬─────┬─────┬─────┬─────┘
         VCC   GND   CLK   DAT   RST
          1     2     3     4     5
PinModule LabelNameTypeDescription
1VCCVCC2PowerMain system power supply input (+2.0 V to +5.5 V DC)
2GNDGNDPowerGround reference (0 V)
3CLKSCLKDigital InputSerial Clock input pin
4DATI/ODigital Input / OutputBi-directional Serial Data line
5RSTCEDigital InputActive-High Chip Enable / Reset control line

Specifications

ParameterSymbolMinTypMaxUnitConditions
Main Supply VoltageVCC2V_{CC2}2.03.3 / 5.05.5VSystem operating power
Backup Battery VoltageVCC1V_{CC1}2.03.03.5VCR2032 coin cell input
Active Supply CurrentICC2I_{CC2}0.31.0mASPI serial data transfer active
Backup CurrentICC1I_{CC1}0.31.0µAMain power off, VCC1=3.0VV_{CC1}=3.0\text{V}
Clock Transfer SpeedfSCLKf_{SCLK}02.0MHzAt VCC2=5.0VV_{CC2} = 5.0\text{V}
Internal SRAMRAMRAM31BytesBattery-backed non-volatile RAM

3-Wire Serial Timing Protocol

  • Chip Enable (CE / RST): Drive CE High before initiating serial transfers. Drive CE Low to end a transaction.
  • Clock (SCLK): Input data bits on I/O are sampled on the rising edge of SCLK; output data bits on I/O change on the falling edge of SCLK.
  • Data (I/O): First byte sent is the Command Byte (R/WˉR/\bar{W} bit, Register Address, and Magic Bit 7 High).
text
 Command Byte Read:  [ 1 | A6 | A5 | A4 | A3 | A2 | A1 | R/W=1 ]
 Command Byte Write: [ 1 | A6 | A5 | A4 | A3 | A2 | A1 | R/W=0 ]

Wiring

DS1302 PinArduino UnoESP32Notes
VCC5V / 3.3V3.3VPower rail
GNDGNDGNDSystem ground
CLKDigital D6GPIO 18SCLK clock line
DATDigital D7GPIO 19I/O bi-directional data line
RSTDigital D8GPIO 5CE active-high chip enable

Example (Arduino RtcByMakuna Library)

cpp
#include <ThreeWire.h>  
#include <RtcDS1302.h>

// Pins: DAT (IO), CLK (SCLK), RST (CE)
ThreeWire myWire(7, 6, 8); 
RtcDS1302<ThreeWire> Rtc(myWire);

void setup() {
  Serial.begin(115200);
  Rtc.Begin();

  if (!Rtc.IsDateTimeValid()) {
    Serial.println("RTC lost confidence in the DateTime!");
    // Set compilation time
    RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
    Rtc.SetDateTime(compiled);
  }

  if (Rtc.GetIsWriteProtected()) {
    Rtc.SetIsWriteProtected(false);
  }

  if (!Rtc.GetIsRunning()) {
    Rtc.SetIsRunning(true);
  }
}

void loop() {
  RtcDateTime now = Rtc.GetDateTime();

  char datestring[20];
  snprintf_P(datestring, 
             countof(datestring),
             PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
             now.Month(), now.Day(), now.Year(),
             now.Hour(), now.Minute(), now.Second());

  Serial.println(datestring);
  delay(1000);
}

Common mistakes

  • Enabling trickle charger with primary CR2032 lithium batteries: The DS1302 includes a software-selectable trickle charger circuit. Do not enable the trickle charger when using standard non-rechargeable CR2032 batteries, as charging non-rechargeable lithium cells causes coin cell swelling and explosion hazards.
  • Confusing 3-wire serial with SPI or I2CI^2C: The DS1302 uses a custom 3-wire protocol with active-high CE. Standard hardware SPI libraries or I2CI^2C scanners cannot communicate with the DS1302.

Notes

  • DS1302 vs DS1307 vs DS3231: DS1302 uses a 3-wire serial interface; DS1307 uses I2CI^2C; DS3231 includes an internal TCXO crystal for ±2 ppm\pm 2\text{ ppm} precision.

Assets & downloads