The RTC-DS1302 is a real-time clock (RTC) module that provides accurate timekeeping capabilities. It is equipped with an integrated circuit (IC) DS1302 which communicates via a simple serial interface. The module includes a small coin cell battery that maintains the time when the main power is off. Common applications include clocks, data loggers, timers, and other devices that require an accurate time reference.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (2.0V to 5.5V) |
2 | GND | Ground |
3 | CLK | Clock input for serial interface |
4 | DAT | Data input/output for serial interface |
5 | RST | Reset input for serial interface |
6 | NC | Not connected |
7 | NC | Not connected |
8 | BAT | Battery input for backup |
#include <DS1302.h>
// Initialize the DS1302
// RST pin, DAT pin, CLK pin
DS1302 rtc(7, 6, 5);
void setup() {
// Set the clock to run-mode, and disable the write protection
rtc.halt(false);
rtc.writeProtect(false);
// Setup Serial connection
Serial.begin(9600);
// The following lines can be uncommented to set the date and time
// rtc.setDOW(FRIDAY); // Set Day-of-Week to FRIDAY
// rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format)
// rtc.setDate(6, 8, 2010); // Set the date to August 6th, 2010
}
void loop() {
// Get the current time and date from the RTC
Serial.print(rtc.getDOWStr());
Serial.print(" ");
// Print the time
Serial.print(rtc.getTimeStr());
Serial.print(" -- ");
// Print the date
Serial.println(rtc.getDateStr());
// Wait one second before repeating :)
delay (1000);
}
Q: Can the RTC-DS1302 be used in low-power applications? A: Yes, the RTC-DS1302 has a low-power consumption mode and can be used in battery-powered applications.
Q: How do I set the time on the RTC-DS1302?
A: You can set the time by using the rtc.setTime()
function in your code, as shown in the example above.
Q: What is the lifespan of the battery? A: The lifespan of the battery depends on the quality and type of battery used, but typically it can last for several years.
Q: Does the RTC-DS1302 account for leap years? A: Yes, the DS1302 chip has an automatic leap year compensation valid up to the year 2100.
Q: Can I use the RTC-DS1302 with other microcontrollers besides Arduino? A: Yes, the RTC-DS1302 can be interfaced with any microcontroller that supports a serial interface. Adjustments to the code may be necessary for different platforms.