To get the time from the chip, you must call updateTime(). This fetches the current second, minute, hour, day, date, month, and year from the DS1302 and stores them in the object's public variables.
Time variables available after updateTime():
Example: Printing the time to Serial Monitor
#include <virtuabotixRTC.h> virtuabotixRTC myRTC(2, 3, 4);void setup() Serial.begin(9600);
void loop() // This line updates the time variables from the hardware myRTC.updateTime(); virtuabotixrtc.h arduino library
// Print the date and time Serial.print("Date: "); Serial.print(myRTC.month); Serial.print("/"); Serial.print(myRTC.dayofmonth); Serial.print("/"); Serial.print(myRTC.year); Serial.print(" - Time: "); Serial.print(myRTC.hour); Serial.print(":"); Serial.print(myRTC.minute); Serial.print(":"); Serial.println(myRTC.second);
delay(1000); // Update once per second
Even with a great library, issues arise. Here is how to fix them. To get the time from the chip, you must call updateTime()
The Virtuabotix RTC modules typically communicate via a 3-wire serial interface (I/O, SCLK, CE/RST) for the DS1302, or I2C for the DS1307/DS3231.
Note: The library code usually defaults to the pin connections defined in the constructor. If you are using a standard Virtuabotix breakout board, connect the pins as follows:
The VirtuabotixRTC.h library is not the most modern RTC solution, nor the most feature-rich. However, its direct, transparent control over the DS1302 makes it ideal for:
Understanding its internal bit-banging, BCD conversion, and RAM access unlocks the full potential of the DS1302. While newer chips offer better accuracy and simpler interfaces, the DS1302 and VirtuabotixRTC remain a robust, economical choice for countless embedded timekeeping tasks. Example: Printing the time to Serial Monitor #include
For developers pushing this library further, consider forking it to add trickle charger configuration, 12-hour mode support, or hardware SPI acceleration. The simplicity of the codebase invites modification—a testament to good open-source design.
The VirtuabotixRTC library is an Arduino software library specifically designed to interface with DS1302 Real-Time Clock chips. (Note: Despite the ambiguous name, it is primarily intended for the DS1302, though it is often adapted for DS1307 clones). It was written and released by the Virtuabotix team to abstract away the tedious bit-banging and shift-register logic required to talk to these chips.
Unlike the more modern RTClib by Adafruit (which is excellent for DS3231 and DS1307 over I2C), the Virtuabotix library handles the 3-wire interface unique to the DS1302. This is crucial because the DS1302 uses a different communication method than standard I2C or SPI devices.