Project43.Ds1302 Rtc Module ============================ Description ------------ The DS1320 RTC is a compact real time battery backed clock module capable of accurately keeping the current time and date. It includes a 24 hour clock consisting of hours, minutes & seconds and a calendar which stores the current year in 2 digits, month and date. The calendar is also capable of correcting for months with different amounts of days and leap years. Additionally there is a day of the week counter to keep track of the current date. Specification -------------- 2.0V to 5.5V Full Operation TTL-Compatible (VCC = 5V) Connect -------- .. image:: /img/connect/43_bb.png .. note:: Go to Library manager and install the Rtc by Makuna library. .. image:: img/phenomenon/43-2.png Code ----- .. code-block:: c #include #include ThreeWire myWire(4,5,2); // IO, SCLK, CE RtcDS1302 Rtc(myWire); void setup () { Serial.begin(9600); Serial.print("compiled: "); Serial.print(__DATE__); Serial.println(__TIME__); Rtc.Begin(); RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); printDateTime(compiled); Serial.println(); if (!Rtc.IsDateTimeValid()) { // Common Causes: // 1) first time you ran and the device wasn't running yet // 2) the battery on the device is low or even missing Serial.println("RTC lost confidence in the DateTime!"); Rtc.SetDateTime(compiled); } if (Rtc.GetIsWriteProtected()) { Serial.println("RTC was write protected, enabling writing now"); Rtc.SetIsWriteProtected(false); } if (!Rtc.GetIsRunning()) { Serial.println("RTC was not actively running, starting now"); Rtc.SetIsRunning(true); } RtcDateTime now = Rtc.GetDateTime(); if (now < compiled) { Serial.println("RTC is older than compile time! (Updating DateTime)"); Rtc.SetDateTime(compiled); } else if (now > compiled) { Serial.println("RTC is newer than compile time. (this is expected)"); } else if (now == compiled) { Serial.println("RTC is the same as compile time! (not expected but all is fine)"); } } void loop () { RtcDateTime now = Rtc.GetDateTime(); printDateTime(now); Serial.println(); if (!now.IsValid()) { // Common Causes: // 1) the battery on the device is low or even missing and the power line was disconnected Serial.println("RTC lost confidence in the DateTime!"); } delay(5000); // five seconds } #define countof(a) (sizeof(a) / sizeof(a[0])) void printDateTime(const RtcDateTime& dt) { char datestring[20]; snprintf_P(datestring, countof(datestring), PSTR("%02u/%02u/%04u %02u:%02u:%02u"), dt.Month(), dt.Day(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second() ); Serial.print(datestring); } Phenomenon ----------- .. image:: /img/phenomenon/43.jpg