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.
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 |
#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);
}
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.