Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

How to Use RTC DS1302: Examples, Pinouts, and Specs

Image of RTC DS1302Design with RTC DS1302 in Cirkit Designer

Introduction

The DS1302 is a real-time clock (RTC) chip designed to keep track of the current time and date, including seconds, minutes, hours, day, date, month, and year. It features a serial interface for communication with microcontrollers and includes a battery backup, allowing it to maintain accurate timekeeping even during power outages. The DS1302 is widely used in applications requiring precise timekeeping, such as data loggers, clocks, alarms, and embedded systems.

Explore Projects Built with RTC DS1302

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Image of Arduino UNO with DS1307 RTC Controlled LED Lighting System: A project utilizing RTC DS1302 in a practical application
Arduino UNO with DS1307 RTC Controlled LED Lighting System

This circuit features an Arduino UNO connected to a DS1307 Real Time Clock (RTC) module for timekeeping and a red LED with a series resistor for indication purposes. The Arduino communicates with the RTC via I2C (using A4 and A5 pins for SDA and SCL, respectively), and controls the LED connected to digital pin D8 through a 330-ohm resistor. The embedded code sets the RTC time, checks the current time, and turns the LED on or off based on the specified time condition (between 11:00 AM and 11:43 AM).

Open Project in Cirkit Designer
Image of Arduino UNO Controlled Relay with DS3231 RTC: A project utilizing RTC DS1302 in a practical application
Arduino UNO Controlled Relay with DS3231 RTC

This circuit features an Arduino UNO microcontroller connected to a DS3231 Real Time Clock (RTC) module and a 12V single-channel relay. The Arduino provides power to both the RTC and the relay, and it communicates with the RTC via I2C using the SDA and SCL lines connected to A4 and A5 respectively. The relay is controlled by the Arduino through a digital output on pin D13, allowing the Arduino to switch external loads on and off based on time events managed by the RTC.

Open Project in Cirkit Designer

Common Applications

Technical Specifications

The DS1302 is a low-power RTC chip with the following key specifications:

Parameter Value
Operating Voltage 2.0V to 5.5V
Backup Battery Voltage 2.0V to 3.5V
Operating Current 300 µA (typical at 5V)
Timekeeping Current 1 µA (typical at 2.0V)
Communication Interface Serial (3-wire)
Clock Accuracy ±2 minutes per month (at 25°C)
Operating Temperature -40°C to +85°C
Maximum Clock Frequency 2 MHz

Pin Configuration and Descriptions

The DS1302 has an 8-pin DIP/SOIC package. Below is the pinout and description:

Pin Name Description
1 VCC1 Primary power supply (2.0V to 5.5V).
2 X1 Oscillator input. Connect to a 32.768 kHz crystal.
3 X2 Oscillator output. Connect to a 32.768 kHz crystal.
4 GND Ground.
5 RST Reset pin. Used to enable communication with the chip. Active high.
6 I/O Serial data input/output.
7 SCLK Serial clock input.
8 VCC2 Backup battery input (2.0V to 3.5V).

Usage Instructions

How to Use the DS1302 in a Circuit

  1. Power Supply: Connect the primary power supply to the VCC1 pin and the ground to the GND pin. For backup power, connect a 3V coin cell battery to the VCC2 pin.
  2. Crystal Oscillator: Attach a 32.768 kHz crystal between the X1 and X2 pins. No external capacitors are required.
  3. Microcontroller Interface: Connect the RST, I/O, and SCLK pins to the corresponding GPIO pins of your microcontroller.
  4. Pull-Up Resistor: Use a pull-up resistor (typically 10kΩ) on the I/O line to ensure proper communication.

Important Considerations

Example Code for Arduino UNO

Below is an example of how to interface the DS1302 with an Arduino UNO to read and set the time:

#include <DS1302.h> // Include the DS1302 library

// Define the DS1302 pins connected to the Arduino
#define RST_PIN 7   // Reset pin connected to Arduino pin 7
#define IO_PIN 6    // I/O pin connected to Arduino pin 6
#define SCLK_PIN 5  // Serial clock pin connected to Arduino pin 5

// Create an instance of the DS1302 class
DS1302 rtc(RST_PIN, IO_PIN, SCLK_PIN);

void setup() {
  Serial.begin(9600); // Initialize serial communication
  rtc.halt(false);    // Start the RTC
  rtc.writeProtect(false); // Disable write protection

  // Set the date and time (Year, Month, Day, Hour, Minute, Second)
  rtc.setDateTime(2023, 10, 15, 14, 30, 0); // Example: 15th Oct 2023, 14:30:00
}

void loop() {
  // Read the current date and time
  DS1302::DateTime now = rtc.getDateTime();

  // Print the date and time to the Serial Monitor
  Serial.print("Date: ");
  Serial.print(now.year); Serial.print("-");
  Serial.print(now.month); Serial.print("-");
  Serial.println(now.day);

  Serial.print("Time: ");
  Serial.print(now.hour); Serial.print(":");
  Serial.print(now.minute); Serial.print(":");
  Serial.println(now.second);

  delay(1000); // Wait for 1 second before updating
}

Notes:

Troubleshooting and FAQs

Common Issues and Solutions

  1. RTC Not Keeping Time

    • Ensure the backup battery is properly connected to the VCC2 pin.
    • Verify that the crystal oscillator is correctly connected to the X1 and X2 pins.
  2. Incorrect Time or Date

    • Check the initialization code to ensure the correct time and date are set.
    • Verify that the microcontroller is communicating with the DS1302 correctly.
  3. No Communication with Microcontroller

    • Ensure the RST, I/O, and SCLK pins are properly connected to the microcontroller.
    • Check for loose connections or damaged wires.
    • Use a pull-up resistor on the I/O line if communication issues persist.
  4. RTC Stops Working After Power Loss

    • Verify that the backup battery voltage is within the specified range (2.0V to 3.5V).
    • Replace the backup battery if it is depleted.

FAQs

Q: Can the DS1302 operate without a backup battery?
A: Yes, but it will lose the current time and date when the primary power supply is disconnected.

Q: What type of crystal oscillator should I use?
A: Use a 32.768 kHz crystal oscillator with a load capacitance of 6pF to 12.5pF.

Q: How accurate is the DS1302?
A: The DS1302 has an accuracy of ±2 minutes per month at 25°C. For higher accuracy, consider temperature-compensated RTCs.

Q: Can I use the DS1302 with 3.3V microcontrollers?
A: Yes, the DS1302 operates within a voltage range of 2.0V to 5.5V, making it compatible with 3.3V systems.

By following this documentation, you can effectively integrate the DS1302 RTC into your projects for reliable timekeeping.