Arduino Library - Virtuabotixrtc.h
Guide to the VirtuabotixRTC Library for Arduino
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.
The "Set Time" Sketch
#include <virtuabotixRTC.h>// Define your pins: (CLK, DAT, RST) // Using pins 7, 6, 5 as wired above virtuabotixRTC myRTC(7, 6, 5);
void setup() Serial.begin(9600);
// Set the time and date manually // Syntax: setDS1302Time(seconds, minutes, hours, dayOfMonth, month, dayOfWeek, year) // NOTE: dayOfWeek: Sunday=1, Monday=2 ... Saturday=7
// Example: April 30, 2026, 14:30:00, Wednesday (dayOfWeek = 4) myRTC.setDS1302Time(00, 30, 14, 30, 4, 4, 26); virtuabotixrtc.h arduino library
Serial.println("RTC Time has been set!");
void loop() // Nothing to do here
Crucial Warning: After running this sketch once, comment out the setDS1302Time line and re-upload a "read-only" sketch. Otherwise, you will reset the clock to the old time every time the Arduino boots. Guide to the VirtuabotixRTC Library for Arduino The
Common usage tips
- Ensure the module’s battery is installed so the clock keeps time when power is removed.
- Confirm I2C address (typically 0x68) and wiring (SDA/SCL) for your board.
- Use the library’s low-level read/write only when implementing advanced features (alarms, square-wave output) if supported by the RTC chip.
- If time appears incorrect after upload, re-check year format expectations (some libraries expect two-digit year values).
Example sketch
A minimal example to set and print time:
#include <Wire.h>
#include "VirtuabotixRTC.h"
VirtuabotixRTC myRTC(0x68); // typical I2C address for DS1307
void setup()
Serial.begin(9600);
Wire.begin();
myRTC.setTime(14, 30, 0); // 14:30:00
myRTC.setDate(3, 15, 4, 26); // Tuesday, 15 April 2026 (example format)
void loop()
Time t = myRTC.getTime();
Date d = myRTC.getDate();
Serial.print(d.day); Serial.print('/');
Serial.print(d.month); Serial.print('/');
Serial.print(d.year); Serial.print(' ');
Serial.print(t.hour); Serial.print(':');
Serial.print(t.minute); Serial.print(':');
Serial.println(t.second);
delay(1000);
(Adjust struct names and function signatures to match the library version you installed.) void loop() // Nothing to do here