The DS1302 is a Real-Time Clock (RTC) integrated circuit that provides precise timekeeping and calendar functionality. It tracks time in seconds, minutes, hours, as well as day, month, and year information. The DS1302 is designed to operate on very low power and retain data and time information on less than 1µW of power, making it ideal for battery-backed non-volatile timekeeping applications.
Common applications of the DS1302 include:
Pin Number | Name | Description |
---|---|---|
1 | X1 | Input for the 32.768kHz crystal oscillator |
2 | X2 | Output for the 32.768kHz crystal oscillator |
3 | GND | Ground pin |
4 | Vcc | Power supply pin (2.0V to 5.5V) |
5 | SCLK | Serial clock input for communication |
6 | I/O | Data input/output for communication |
7 | CE | Chip enable, active high |
8 | Vbat | Backup battery input for data retention |
To use the DS1302 in a circuit, connect the Vcc pin to a power supply within the specified range and GND to the system ground. The X1 and X2 pins should be connected to a 32.768kHz crystal. The CE, I/O, and SCLK pins are used for communication with a microcontroller, such as an 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 January 1, 2023, 12:00:00
rtc.setDOW(SUNDAY); // Set Day-of-Week to SUNDAY
rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
rtc.setDate(1, 1, 2023); // Set the date to January 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);
}
Q: Can the DS1302 be used in a 5V system? A: Yes, the DS1302 can operate in systems with a supply voltage from 2.0V to 5.5V.
Q: How long will the DS1302 keep time on a battery backup? A: This depends on the battery capacity and the quality of the crystal. With a good quality coin cell and crystal, the DS1302 can keep time for several years.
Q: Is the DS1302 Y2K compliant? A: Yes, the DS1302 has a century bit that allows it to be used well beyond the year 2000.
Q: Does the DS1302 account for leap years? A: Yes, the DS1302 automatically adjusts for months with fewer than 31 days, including corrections for leap year.
For further assistance, consult the DS1302 datasheet or contact technical support.