Cause: Wiring issue or poor connection on CLK, DAT, or RST lines. Fix: Check all jumper wires. Use shorter wires. Add 10k pull-up resistors on DAT and CLK if the wires are long.
When you first power a new DS1302 chip, the time is random. You must set it. Important: You should only run the setting code once and then comment it out. Writing to the RTC every loop wears out the memory.
Syntax:
setDS1302Time(second, minute, hour, dayofweek, dayofmonth, month, year)
Example: Setting to a specific date and time
#include <virtuabotixRTC.h> virtuabotixRTC myRTC(2, 3, 4);void setup() Serial.begin(9600);
// Set the time to: 15:30:00 on Friday, May 4th, 2026 // Parameters: sec, min, hour, dayOfWeek, dayOfMonth, month, year // Note: dayOfWeek: 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri, 7=Sat myRTC.setDS1302Time(0, 30, 15, 6, 4, 5, 2026);
// Verify the setting myRTC.updateTime(); Serial.println("Time has been set!");
void loop() // Nothing here. Reset the Arduino after this.
Pro Tip: Instead of hardcoding, you can set the time based on your computer's clock at compilation using __TIME__ and __DATE__ macros, but note this requires parsing strings—a more advanced technique.
Because the DS1302 uses a 3-wire interface with no differential signaling, long wires (>30 cm) can cause bit errors. Use shielded wires or keep leads short.
First, you must include the library and create an instance of the RTC object. You must define which Arduino pins are connected to the RTC module.
#include <virtuabotixRTC.h>
// Define connections: CLK, DAT, RST
// Change these pin numbers based on your wiring
#define CLK 6
#define DAT 7
#define RST 8
// Create the myRTC object
virtuabotixRTC myRTC(RST, DAT, CLK);
Example (conceptual):
#include <Wire.h>
#include <VirtuabotixRTC.h>
VirtuabotixRTC rtc(0x68); // DS1307 I2C address
void setup()
Wire.begin();
// Set time once (uncomment and adjust values then upload)
// rtc.setDS1307Time(30, 59, 23, 7, 24, 3, 2026); // sec,min,hour,day,date,month,year
void loop()
// rtc.getTime() or equivalent returns struct with fields: second, minute, hour, day, date, month, year
// use fields to display or compute
If your DS1302 is running slow, check the voltage on pin 3.3V. Some modules have a diode that drops voltage. Powering the VCC pin with 5V and the backup battery with 3V can cause issues. Ensure the main power matches the chip's spec sheet.
In the loop(), you must call updateTime() before reading the variables. This pulls the latest data from the hardware chip into the software variables.
void loop()
// This function updates the variables inside the object with current RTC data
myRTC.updateTime();
// Print the time to the Serial Monitor
Serial.print("Current Date/Time: ");
Serial.print(myRTC.year); // Year
Serial.print("/");
Serial.print(myRTC.month); // Month
Serial.print("/");
Serial.print(myRTC.dayofmonth); // Day
Serial.print(" ");
Serial.print(myRTC.hours); // Hours
Serial.print(":");
Serial.print(myRTC.minutes); // Minutes
Serial.print(":");
Serial.println(myRTC.seconds); // Seconds
delay(1000); // Wait 1 second before reading again
The VirtuabotixRTC.h library is a lightweight Arduino library that provides a simple interface for interacting with DS1307 and compatible I²C real-time clock (RTC) modules. It abstracts low-level I²C communication and offers straightforward functions to read and set date/time and perform basic RTC operations.