DS1302 RTC with Arduino

Article thumbnail

Published

arduino rtc

Using the DS1302 real-time clock (RTC) with Arduino

Date/time and RAM examples are listed in the DS130X Common Reference. This page covers DS1302-specific features.

DS1302 Features

The DS1302:

Pin NumberPin NameFunction
1VCC25V power supply
2X1Connection to 32.768 kHz quartz crystal
3X2
4GNDGround
5CEChip Enable
6I/OSerial Data
7SCLKSerial Clock
8VCC1Connection to 3V battery

Schematic (Typical Operating Circuit)

DS1302 schematic

Note that the backup supply is a 3V coin cell. This supply will allow the DS1302 to keep track of time when it is turned off.

The DS1302 keeps track of time by reading ticks from an oscillator. This is why a 32.768 kHz oscillator (Y1) is attached to pins 1 and 2.

Library

This project uses my DS130X library. Download the library in Zip format

Using the TCR (Trickle Charge Register)

Trickle charging means to charge a battery at the same rate as its discharge rate, so it remains at its current level. The DS1302 charges its battery by outputting voltage from its VCC1 pin.

The example code is at File > Examples > DS130X > DS1302 > Other > SetTCR. It does not output anything to the serial monitor.

important:

If you have a non-rechargeable battery (or if you don’t know if it’s rechargeable), take it out before running this example. Attempting to charge a non-rechargeable battery can cause it to catch fire or explode.

You can use a multimeter to measure the voltage between VCC1 and GND to make sure this function works.

/*
   SetTCR.ino
   Sketch to demonstrate using the DS1302's trickle charge feature.
   Created August 30, 2020

   DS1302 connections:
   CLK to 2
   DAT to 3
   CE  to 4
*/

#include <DS130X.h>

DS1302 rtc(3, 2, 4); // DAT, CLK, CE

void setup() {
  rtc.setWriteProtect(false); // Disable write protect
  rtc.writeRaw(TCR_SET, TCR_1D_2R); // Set TCR to 1 diode and 2kR
}

void loop() {} // No loop

DS1302 TCR Output Voltage/Current

The DS1302 outputs the same amount of voltage present on VCC2, the main power supply. In our circuit, the DS1302’s main power supply (the Arduino’s 5V pin) is 5V, so the TCR will also output 5V.

The DS1302’s TCR has internal resistors and diodes. Selecting how many of these are activated will change the output current.

You can select any combination of resistors and diodes from below:

The formula for calculating the output current from the diodes/resistors is:

Imax=5DR1I_{max} = \frac{5-D}{R_1}

For example, the output current of having 1 diode and the 2k resistor selected would be:

Imax=50.72000=0.00215A2.2mAI_{max} = \frac{5-0.7}{2000} = 0.00215 \mathrm{A} \approx 2.2 \mathrm{mA}

Configuring the TCR

The DS1302 powers up with the TCR disabled. To enable the TCR, use writeRaw (not write) to write to the register TCR_SET. For the data to write, use one of these six constants:

For example, to set the TCR to have 1 diode and a 2k resistor to have a 2.2mA current (like described above), use:

rtc.writeRaw(TCR_SET, TCR_1D_2R);

note:

To get the current TCR setting, read from the TCR_GET register. readRaw is preferred since it returns the raw BCD data, like how writeRaw writes raw data.

In the code, The Arduino will disable the RTC’s write protect, then write to the TCR_SET register with data TCR_1D_2R (1 diode, 2k resistor).