Virtuabotixrtc.h Arduino Library May 2026

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.

To use this library, you will need a DS1302 RTC module. Wire it to your Arduino as follows:

| DS1302 Pin | Arduino Pin | Description | | :--- | :--- | :--- | | VCC | 5V | Power | | GND | GND | Ground | | CE / RST | Digital Pin 2 | Chip Enable / Reset | | I/O / DAT | Digital Pin 3 | Data Input/Output | | SCLK | Digital Pin 4 | Serial Clock |

(Note: You can use any digital pins, just remember to update them in your code).


This example shows you how to initialize the RTC, set the time (once), and print the time to the Serial Monitor.

#include <VirtuabotixRTC.h>

// Initialize the RTC object with your chosen pins: (CLK, DAT, RST) // My wiring: CLK=4, DAT=3, RST=2 VirtuabotixRTC myRTC(4, 3, 2); virtuabotixrtc.h arduino library

void setup() Serial.begin(9600);

// SET THE TIME ONCE, THEN COMMENT THIS LINE OUT OR IT WILL RESET EVERY TIME YOU REBOOT // Format: seconds, minutes, hours, day of week, day of month, month, year // Example: October 24, 2023, 14:30:00, Tuesday (Day of week is 1-7, Mon-Sun) // myRTC.setDS1302Time(00, 30, 14, 2, 24, 10, 23);

void loop() // This allows the library to update the internal time variables myRTC.updateTime();

// Print the time in a formatted way Serial.print("Current Date/Time: "); Serial.print(myRTC.dayofmonth); Serial.print("/"); Serial.print(myRTC.month); Serial.print("/"); Serial.print(myRTC.year); Serial.print(" "); Serial.print(myRTC.hours); Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(":"); Serial.println(myRTC.seconds);

delay(1000); // Update every second


The VirtuabotixRTC.h library is an Arduino library written to simplify communication with low-cost RTC modules, specifically the DS1302 (3-wire interface) and the DS1307 (I2C interface).

While the official Arduino RTC library exists, the Virtuabotix version gained popularity because of its simplicity and its ability to handle the quirky timing protocols of the DS1302. The library handles the low-level bit-banging necessary to read and write time data, allowing you to use simple commands like rtc.getTimeStr() or rtc.setDate().

Cause: The backup battery is dead or missing. Fix: Replace the CR2032 battery. The DS1302 draws microamps; a good battery lasts 2-5 years.

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. If your DS1302 is running slow, check the voltage on pin 3

#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);

The real power of the Virtuabotix library is in data logging. Let's build a temperature logger that only records from 9 AM to 5 PM.

Once wired, you need to create an object representing your RTC. The constructor requires three arguments: the reset pin, the data pin, and the clock pin.

#include <virtuabotixRTC.h>

// Define your pins: (RST, DAT, CLK) int RTC_RST = 2; int RTC_DAT = 3; int RTC_CLK = 4;

// Create the RTC object virtuabotixRTC myRTC(RTC_RST, RTC_DAT, RTC_CLK);

This object (myRTC) is now your gateway to reading and writing time.

コメント