To use the library, you must first create an object. The constructor takes two arguments: the SDA pin and the SCL pin. For standard boards, we use A4 and A5 even though they are analog pins (they double as digital I2C).
#include <VirtuabotixRTC.h>
// Pin definition for Uno/Nano // (SDA, SCL) VirtuabotixRTC myRTC(A4, A5);
Try the example sketch: File → Examples → VirtuabotixRTC → SetTimeAndDisplay virtuabotixrtch arduino library
If you need absolute seconds since 1970, you can extend the library:
unsigned long getUnixTime(VirtuabotixRTC &rtc)
rtc.updateTime();
// Use a helper function (requires <TimeLib.h>)
return makeTime(rtc); // This requires conversion logic
Note: For heavy timestamp math, consider switching to RTClib.
#include <Wire.h>
#include <VirtuabotixRTC.h>
// Use the device address the library expects (example: 0x68) and set starting pin if required
VirtuabotixRTC myRTC(0x68); // constructor may vary by library version
void setup()
Serial.begin(9600);
Wire.begin();
// Optionally set time once:
// myRTC.setTime(14, 30, 0); // hh, mm, ss
// myRTC.setDate(9, 4, 2026); // dd, mm, yyyy or yy depending on library version
void loop()
// read time
int hour = myRTC.getHour();
int minute = myRTC.getMinute();
int second = myRTC.getSecond();
int day = myRTC.getDay();
int month = myRTC.getMonth();
int year = myRTC.getYear(); // check if returns full year or two-digit
// print formatted
Serial.print(hour); Serial.print(":");
if(minute < 10) Serial.print("0");
Serial.print(minute); Serial.print(":");
if(second < 10) Serial.print("0");
Serial.print(second);
Serial.print(" ");
Serial.print(month); Serial.print("/");
Serial.print(day); Serial.print("/");
Serial.println(year);
delay(1000);
Notes:
Checking for a specific minute rollover is trivial:
static int lastMinute = -1;
myRTC.updateTime();
if(myRTC.minutes != lastMinute && myRTC.minutes % 15 == 0)
activateSprinkler(); // runs every 15 minutes
lastMinute = myRTC.minutes;
The library uses the standard I2C pins of your Arduino.
| RTC Module Pin | Arduino Uno/Nano | Arduino Mega 2560 | ESP8266 (NodeMCU) | | :------------ | :--------------- | :---------------- | :---------------- | | VCC | 5V | 5V | 3.3V | | GND | GND | GND | GND | | SCL | A5 | 21 | D1 (GPIO5) | | SDA | A4 | 20 | D2 (GPIO4) | To use the library, you must first create an object
Note: The Virtuabotix library automatically uses the Wire library under the hood, so you don't need to define pins unless using software I2C.
Crucial Tip: If your module has separate DS and SCLK pins (common on old DS1302 modules), do not use VirtuabotixRTC. That library is for I2C modules only (DS1307/3231). For DS1302, you need the DS1302 library.