Virtuabotixrtc.h | Arduino Library
The library creates a VirtuabotixRTC object. The core functions are:
| Function | Syntax Example | Description |
| :--- | :--- | :--- |
| Constructor | VirtuabotixRTC(int clk, int dat, int rst) | Initializes the library with the three Arduino pins connected to the DS1302's CLK, DAT (I/O), and RST (CE) pins. |
| updateTime() | rtc.updateTime(); | Reads the current time from the RTC chip and stores it in the object's internal variables. Must be called before reading time. |
| setDS1302Time() | rtc.setDS1302Time(seconds, minutes, hours, dayOfWeek, date, month, year); | Writes a new time/date to the RTC chip. Typically called once during setup. |
| Accessing Time | rtc.hours, rtc.minutes, rtc.seconds, rtc.dayofmonth, rtc.month, rtc.year, rtc.dayofweek | After updateTime(), these integer variables hold the current time components. |
Example Minimal Code:
#include <virtuabotixRTC.h>// CLK, DAT, RST pins VirtuabotixRTC myRTC(6, 7, 8);
void setup() Serial.begin(9600); // Uncomment to set time (year, month, date, hour, minute, second, dayOfWeek) // myRTC.setDS1302Time(0, 27, 13, 4, 11, 4, 2026); // Example: 2026-04-11 13:27:00 Sat
void loop() myRTC.updateTime(); Serial.print(myRTC.hours); Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(":"); Serial.println(myRTC.seconds); delay(1000);
Cause: The library is not installed correctly.
Fix: Reinstall via Library Manager or check that you have #include <virtuabotixRTC.h> at the top.
This tiny board usually has 5 pins:
Behind these pins lies the DS1302 chip, a 32.768 kHz crystal, and a small battery (usually a CR2032). This battery keeps the clock running when the main Arduino power is off. virtuabotixrtc.h arduino library
Most users ignore the 31 bytes of NV RAM on the DS1302. However, VirtuabotixRTC.h exposes this through writeRAM() and readRAM(). This RAM is separate from the time registers and retains data as long as backup power is supplied.
Use cases:
Important: RAM addresses 0–30 are valid. Address 31 is the trickle charger register, which the library does not natively expose but can be accessed via writeRegister(0x90 | 0x01, value). The trickle charger is essential for supercapacitor-backed RTCs.
#include <virtuobabotixRTC.h>virtuobabotixRTC myRTC(7, 6, 5);
void setup() Serial.begin(9600);
void loop() // This is the most important line. It reads the RTC's current time // into the library's variables. myRTC.updateTime();
// --- Different ways to display time ---
// 1. Raw integers (Best for logic) Serial.print("Raw Data: "); Serial.print(myRTC.hours); // 24-hour format Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(":"); Serial.println(myRTC.seconds);
// 2. Formatted Strings (Best for Serial Monitor) Serial.print("Time (HH:MM:SS): "); Serial.println(myRTC.getTimeStr()); // Returns "14:30:00" The library creates a VirtuabotixRTC object
Serial.print("Date (MM/DD/YYYY): "); Serial.println(myRTC.getDateStr()); // Returns "04/30/2026"
// 3. Combined Serial.print("Full Stamp: "); Serial.print(myRTC.getDateStr()); Serial.print(" "); Serial.println(myRTC.getTimeStr());
// 4. Day of week (as number) Serial.print("Day of week (1-7): "); Serial.println(myRTC.dayofweek);
delay(1000); // Update every second
| Feature | VirtuabotixRTC.h | RTClib (Adafruit) | | :--- | :--- | :--- | | Primary Chip | DS1302 | DS1307, DS3231 | | Interface | 3-wire (Digital pins) | I2C (SDA/SCL) | | Ease of Use | Very simple | More features, steeper curve | | Code Size | Small | Larger | | Precision | Low (±20ppm) | High (DS3231: ±2ppm) | | Best For | Simple clocks, DS1302 modules | Professional logging, DS3231 |
Our Verdict: Use VirtuabotixRTC if you already have a DS1302 module and need a 5-minute setup. Use RTClib for long-term, high-accuracy projects.
In the world of embedded electronics and DIY automation, keeping accurate time is a fundamental requirement. Whether you are building a data logger, an automated garden sprinkler, a programmable oven timer, or a complex home security system, your Arduino needs a way to know what time it is right now.
While the Arduino’s built-in millis() function is excellent for measuring short intervals, it resets every time the power is cycled. For calendar dates and long-term tracking, you need a dedicated Real-Time Clock (RTC) module. Among the most popular and affordable of these modules are the DS1302 and DS1307 chips. void loop()
myRTC
Enter the virtuabotixrtc.h library. Created to simplify the complex communication protocols (SPI and I2C) used by these RTC chips, this library has become a go-to solution for thousands of makers. This article provides a deep dive into everything you need to know about the virtuabotixrtc.h library—from installation and wiring to advanced coding techniques and troubleshooting.
The virtuabotixRTC.h library is a widely-used tool for interfacing Arduino boards with Real-Time Clock (RTC) modules, specifically the DS1302 chip. This library simplifies the process of reading and setting time, allowing projects like digital clocks, data loggers, and automated systems to maintain accurate time even when power is lost. Key Features and Functionality
The library is designed for ease of use, featuring a simple API to interact with the DS1302 module.
Time Tracking: It tracks seconds, minutes, hours, day of the week, day of the month, month, and year.
Synchronous Serial Communication: Uses a three-wire interface (CLK, DAT, RST) to communicate with the Arduino.
Battery Retention: When paired with a backup battery (like a CR2032), the RTC keeps time independently of the Arduino's power state.
Individual Data Access: Includes functions to access specific time elements (e.g., just the year or just the minutes) for customized displays. Hardware Wiring for DS1302
To use the library, you must connect your DS1302 module to the Arduino. A typical wiring setup is as follows: 35.154.37.103https://35.154.37.103 Virtuabotixrtc.h Arduino Library [portable]


