Virtuabotixrtc.h Arduino Library __hot__ File
// Print the date and time Serial.print("Date: "); Serial.print(myRTC.month); Serial.print("/"); Serial.print(myRTC.dayofmonth); Serial.print("/"); Serial.print(myRTC.year); Serial.print(" - Time: "); Serial.print(myRTC.hour); Serial.print(":"); Serial.print(myRTC.minute); Serial.print(":"); Serial.println(myRTC.second);
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. virtuabotixrtc.h arduino library
void loop() // Nothing here. Reset the Arduino after this. // Print the date and time Serial
Important: The DS1302 requires a backup battery (usually a CR2032) to keep time when the Arduino is unplugged. Ensure the battery is installed and has a charge above 2.5V. 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. Writing to the RTC every loop wears out the memory
Even with a great library, issues arise. Here is how to fix them. Error 1: 'virtuabotixRTC' does not name a type Cause: The library is not installed correctly. Fix: Reinstall via Library Manager or check that you have #include <virtuabotixRTC.h> at the top. Error 2: Time resets to 2000/01/01 every power cycle Cause: The backup battery is dead or missing. Fix: Replace the CR2032 battery. The DS1302 draws microamps; a good battery lasts 2-5 years. Error 3: Garbage time values (e.g., hour = 255) 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. Error 4: The time drifts fast or slow Cause: The DS1302 uses an external 32.768kHz crystal. Cheap crystals have tolerance. Fix: This is a hardware issue, not software. Use a DS3231 (which is much more accurate) if you need precision. You cannot fix drift in code unless you manually adjust every day. Comparing VirtuabotixRTC to Other RTC Libraries | Feature | VirtuabotixRTC | RTClib (Adafruit) | DS1302RTC (by JChristensen) | | :--- | :--- | :--- | :--- | | Chip Support | DS1302 (3-wire) | DS1307, DS3231, PCF8523 (I2C) | DS1302 only | | Ease of Use | Easy | Very Easy | Moderate | | Memory Usage | Low (~1.5KB) | Medium (~2.5KB) | Low | | Active Development | No (Stable, legacy) | Yes | No | | Best For | Legacy DS1302 modules | Modern projects needing accuracy | Minimalist DS1302 users |
delay(60000); // Log every minute