Write a Review (requires login)
The DS1302 is a VERY old clock module and it's probably better to use a DS1307 because you can't set this clock from your system clock. You have to enter the date/time manually by entering values from your system clock display just beore you upload your Arduino Sketch.
Here is the code I wrote. You will have to download and manually install the library.
// library from
// https://github.com/hickey/arduino/tree/master/Libraries/DateTime/DateTime
#include
MyRealTimeClock myRTC(6, 7, 8); // Assign Digital Pins
int DayOfWeek;
void setup() {
Serial.begin(9600);
// Read values from system clock screen display.
// Add a few seconds to account for upload time.
// (sec, min, hr, day of week (1-7), day, mon, day, year)
// Display for U.S. date format: month/day/year
myRTC.setDS1302Time(30, 3, 16, 2 , 10, 5, 2021);
}
void loop() {
myRTC.updateTime();
Serial.print("Current Date / Time: ");
DayOfWeek=(myRTC.dayofweek);
Serial.print(DayOfWeek); Serial.print(' ');
switch(DayOfWeek) {
case 1: Serial.print("Sun "); break;
case 2: Serial.print("Mon "); break;
case 3: Serial.print("Tue "); break;
case 4: Serial.print("Wed "); break;
case 5: Serial.print("Thu "); break;
case 6: Serial.print("Fri "); break;
case 7: Serial.print("Sat "); break;
}
Serial.print(myRTC.month); Serial.print("/");
Serial.print(myRTC.dayofmonth); Serial.print("/");
Serial.print(myRTC.year); Serial.print(" ");
Serial.print(myRTC.hours); Serial.print(":");
Serial.print(myRTC.minutes); Serial.print(":");
Serial.println(myRTC.seconds); delay( 5000);
}
It is nice that it comes with a new CR2032 battery. The spec. sheet has a link to useful, well commented Arduino code. This is an older edition of the various real-time clocks available. The unit I connected to my Arduino appears to have fallen behind by six seconds over a three-day period, but so what.