Virtuabotixrtc.h Arduino Library
dataFile = SD.open("datalog.txt", FILE_WRITE); if (dataFile) dataFile.print(myRTC.month); dataFile.print("/"); dataFile.print(myRTC.dayofmonth); dataFile.print(" "); dataFile.print(myRTC.hour); dataFile.print(":"); dataFile.print(myRTC.minute); dataFile.print(" - Temp: "); dataFile.print(temperatureC); dataFile.println(" C"); dataFile.close();
Once updateTime() is called, the library provides direct access to time components via public members of the object: myRTC.seconds myRTC.minutes myRTC.hours myRTC.dayofmonth myRTC.month myRTC.year myRTC.dayofweek Example Implementation
In conjunction with a CR2032 coin cell battery, it ensures the RTC maintains time even when the Arduino is powered off. virtuabotixrtc.h arduino library
The library was originally in the public domain and is now primarily maintained on platforms like GitHub (chrisfryer78/ArduinoRTClibrary) . It is not always available in the built-in Arduino Library Manager, so manual installation via is often required.
// SET THE TIME ONCE, THEN COMMENT THIS LINE OUT OR IT WILL RESET EVERY TIME YOU REBOOT // Format: seconds, minutes, hours, day of week, day of month, month, year // Example: October 24, 2023, 14:30:00, Tuesday (Day of week is 1-7, Mon-Sun) // myRTC.setDS1302Time(00, 30, 14, 2, 24, 10, 23); dataFile = SD
// Read RAM byte at address 0 byte config = myRTC.readRAM(0);
When the DS1302 loses all power, bit 7 of the seconds register (CH) is set, stopping the oscillator. The library does automatically clear this flag on initialization. If your RTC seems frozen, manually clear it: // SET THE TIME ONCE, THEN COMMENT THIS
virtuabotixRTC.h library is a popular, user-friendly Arduino library designed specifically to interface with the DS1302 Real-Time Clock (RTC) module. It simplifies the process of setting, updating, and reading time data (seconds, minutes, hours, day, month, year) from the module using only three pins.
Note: You should upload the code once to set the time, then comment out the setDS1302Time line and upload the code again to prevent the Arduino from resetting the time to the compiled moment every time it resets.