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

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

Image of DS1302 RTCDesign with DS1302 RTC in Cirkit Designer

Introduction

The DS1302 Real-Time Clock (RTC) is a widely used electronic component that provides accurate timekeeping and date information in a binary-coded decimal (BCD) format. It includes a built-in clock/calendar and 31 bytes of static RAM. The DS1302 communicates with microcontrollers via a simple serial interface and is commonly used in embedded systems, clocks, data loggers, and other devices that require timekeeping functionality.

Explore Projects Built with DS1302 RTC

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 DS1302 RTC 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 DS1302 RTC 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 and Use Cases

Technical Specifications

Key Technical Details

Pin Configuration and Descriptions

Pin Number Name Description
1 X1 Input for the 32.768 kHz crystal oscillator
2 X2 Output for the 32.768 kHz crystal oscillator
3 GND Ground pin
4 Vcc Power supply pin (2.0V to 5.5V)
5 SCLK Serial Clock Input
6 I/O Serial Data Input/Output
7 CE Chip Enable Input
8 Vbat Backup Battery Input

Usage Instructions

How to Use the DS1302 in a Circuit

  1. Power Supply: Connect the Vcc pin to a 2.0V to 5.5V power source and the GND pin to the ground.
  2. Crystal Oscillator: Attach a 32.768 kHz crystal oscillator to pins X1 and X2.
  3. Microcontroller Interface: Connect the SCLK, I/O, and CE pins to the corresponding digital I/O pins on your microcontroller.
  4. Backup Battery: To maintain timekeeping when the main power is off, connect a 3V coin cell battery to the Vbat pin.

Important Considerations and Best Practices

Example Code for Arduino UNO

#include <DS1302.h>

// Initialize the DS1302
// CE pin -> Arduino Digital 2, I/O pin -> Arduino Digital 3, SCLK pin -> Arduino Digital 4
DS1302 rtc(2, 3, 4);

void setup() {
  Serial.begin(9600);
  rtc.halt(false);  // Enable the clock
  rtc.writeProtect(false);  // Disable write protection

  // Set the time to 12:00:00 (24hr format), date to 2023-04-01
  rtc.setDOW(SATURDAY);        // Set Day-of-Week to Saturday
  rtc.setTime(12, 0, 0);       // Set the time to 12:00:00 (24hr format)
  rtc.setDate(1, 4, 2023);     // Set the date to April 1, 2023
}

void loop() {
  // Print the current date and time to the Serial Monitor
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
  Serial.println(rtc.getTimeStr());

  // Wait one second before repeating
  delay(1000);
}

Troubleshooting and FAQs

Common Issues Users Might Face

Solutions and Tips for Troubleshooting

FAQs

Q: Can the DS1302 be used in a 12-hour format? A: Yes, the DS1302 can be configured to use a 12-hour format with an AM/PM indicator.

Q: How long will the backup battery last? A: The backup battery life depends on the battery capacity and the quality of the battery. Typically, a 3V coin cell battery can last several years.

Q: Is the DS1302 Y2K compliant? A: Yes, the DS1302 has leap year compensation and is valid up to the year 2100.