Mein Versuchsaufbau besteht aus:
- 1 x Arduino Nano
- 1 x RTC Real Time Clock-Modul DS1302 von AZ-Delivery
- Kabel
Verwendete Software:
- Arduino IDE 1.8.16
- Bibliothek „RTC by Makuna“ by C. Miller
Anschlussplan:
Folgender Quellcode wurde verwendet:
#include <ThreeWire.h> #include <RtcDS1302.h> ThreeWire myWire(3, 4, 2); // DAT/IO, CLK/SCLK, RST/CE RtcDS1302<ThreeWire> Rtc(myWire); void setup () { Rtc.Begin(); Serial.begin(115200); //Uncomment to write current PC time to the RTC RtcDateTime cdt = RtcDateTime(__DATE__, __TIME__); //Uncomment to enter manualy date and time to the RTC //RtcDateTime cdt = RtcDateTime("Jan 14 2015", "09:55:20"); Rtc.SetDateTime(cdt); } void loop () { RtcDateTime pdt = Rtc.GetDateTime(); printDateTime(pdt); Serial.println(); delay(500); } void printDateTime(const RtcDateTime& dt) { //Day of the week Serial.print("Day of the week: "); if (dt.DayOfWeek() == 1) { Serial.println("Monday"); } else if (dt.DayOfWeek() == 2) { Serial.println("Tuesday"); } else if (dt.DayOfWeek() == 3) { Serial.println("Wednesday"); } else if (dt.DayOfWeek() == 4) { Serial.println("Thursday"); } else if (dt.DayOfWeek() == 5) { Serial.println("Friday"); } else if (dt.DayOfWeek() == 6) { Serial.println("Saturday"); } else if (dt.DayOfWeek() == 7) { Serial.println("Sunday"); } // Current Date Serial.print("Current Date: "); if (dt.Day() < 10) { Serial.print("0"); Serial.print(dt.Day()); } else { Serial.print(dt.Day()); } //one tab Serial.print("/"); if (dt.Month() < 10) { Serial.print("0"); Serial.print(dt.Month()); } else { Serial.print(dt.Month()); } Serial.print("/"); Serial.println(dt.Year()); //Current Time Serial.print("Current Time: "); if (dt.Hour() < 10) { Serial.print("0"); Serial.print(dt.Hour()); } else { Serial.print(dt.Hour()); } Serial.print(":"); if (dt.Minute() < 10) { Serial.print("0"); Serial.print(dt.Minute()); } else { Serial.print(dt.Minute()); } //one tab Serial.print(":"); if (dt.Second() < 10) { Serial.print("0"); Serial.print(dt.Second()); Serial.println(); } else { Serial.print(dt.Second()); Serial.println(); } }
Ergebnis:
Modul funktioniert wie erwartet. Bei mir liegen PC-Uhrzeit und Modul 7 Sekunden auseinander.