The VirtuabotixRTC library is a popular, lightweight Arduino library designed to interface with Real Time Clock (RTC) modules, specifically the DS1302, DS1307, and DS3231 chips. While similar to other RTC libraries, VirtuabotixRTC is favored for its simplicity and its streamlined method for setting the time and date inside the setup() loop.
updateTime()):myRTC.seconds (0-59)myRTC.minutes (0-59)myRTC.hours (0-23)myRTC.dayofmonth (1-31)myRTC.month (1-12)myRTC.year (0-99 – note: 22 = 2022)myRTC.dayofweek (1-7)This is the most distinct feature of this library. To set the time, you pass the current date and time parameters directly to the setDS1302Time function inside setup().
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. virtuabotixrtc.h arduino library
void setup()
Serial.begin(9600);
// Format: seconds, minutes, hours, day of week, day of month, month, year
// Example: Setting time to Oct 25, 2023, Wednesday, 14:30:00
// Day of week: 1=Sunday, 2=Monday, ... 7=Saturday
// UNCOMMENT THE NEXT LINE TO SET TIME, THEN COMMENT IT BACK AND RE-UPLOAD
// myRTC.setDS1302Time(00, 30, 14, 4, 25, 10, 2023);
rtc.updateTime().One of the most subtle aspects of VirtuabotixRTC.h is its handling of BCD vs. binary. The DS1302 stores time natively in BCD: for example, 42 seconds is stored as 0x42 (binary 01000010), not 0x2A (binary 00101010).
The library provides two pathways:
setDS1302Time(0, 12, 15, 6, 18, 5, 25) — these accept decimal integers and internally convert to BCD using bit shifts: ((x/10) << 4) | (x%10).Potential bug trap: If you manually manipulate registers without conversion, or if you mix methods, you may write invalid BCD values (e.g., 0x1A for seconds, where A is invalid). The library does not validate register writes beyond basic bounds.
Leap year handling: The library does not contain automatic leap year calculation for day-of-week tracking. It relies on the user to correctly maintain day-of-week or update it externally. For applications needing self-correcting calendars, this is a limitation. Guide to the VirtuabotixRTC Library for Arduino The
Once myRTC.updateTime() is called, you can access the following public variables:
myRTC.secondsmyRTC.minutesmyRTC.hours (Usually in 24-hour format depending on chip settings)myRTC.dayofweek (Returns integer: 1 for Sunday, 2 for Monday, etc.)myRTC.dayofmonthmyRTC.monthmyRTC.year