Virtuabotixrtc.h Arduino Library _hot_ -
The pin naming can vary. Some modules use SCLK , I/O , and CE instead of CLK , DAT , and RST . No matter the name, the function is the same.
Here is an annotated example of how the library is typically used:
This example shows you how to initialize the RTC, set the time (once), and print the time to the Serial Monitor.
For other RTC chips, like the DS1307 or DS3231, the de facto standard is Adafruit’s . That library is superior in features and active maintenance, but it cannot be used with the DS1302 without a software I2C emulation layer. Thus, VirtuabotixRTC remains relevant specifically for the DS1302 ecosystem. virtuabotixrtc.h arduino library
// Only print to the Serial Monitor once per second to avoid clutter if (lastReportedHour != myRTC.hours) // A simple conditional Serial.print("The time is: "); Serial.print(displayHour); Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(":"); Serial.println(myRTC.seconds); lastReportedHour = myRTC.hours;
To use the library, you must manually add it to your Arduino IDE.
// Uncomment to set time (year offset: 25 = 2025) // myRTC.setDS1302Time(0, 49, 14, 4, 21, 4, 25); The pin naming can vary
// Define the pins used to connect the RTC module const int rtcClockPin = 2; const int rtcDataPin = 3; const int rtcCsPin = 4;
The library defines the virtuabotixRTC class with several key methods:
Serial.print(myRTC.hours); // Cached values Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(":"); Serial.println(myRTC.seconds); Here is an annotated example of how the
: When paired with a backup battery (like a CR2032), the RTC keeps time independently of the Arduino's power state.
: The library was originally part of the Virtuabotix Versalino ecosystem and is now primarily maintained in community repositories.
The DS1302 uses a 3-wire serial interface, which consists of a Reset/Chip Enable pin (RST/CE), a Serial Data pin (DAT/IO), and a Serial Clock pin (CLK/SCLK).
virtuabotixRTC myRTC(6, 7, 8); const int ledPinRed = 13; const int ledPinGreen = 12;