void setup() Serial.begin(9600);
This sketch initializes the clock, sets an initial baseline time, and outputs the updated time to the Serial Monitor every second.
The library allows you to perform basic time-keeping tasks with minimal code: Time Setting
Use VirtuabotixRTC if you already have a DS1302 module and need a 5-minute setup. Use RTClib for long-term, high-accuracy projects.
Because VirtuabotixRTC.h bit-bangs the protocol, its timing is critical. The DS1302 expects SCLK periods as fast as 2.5 MHz, but bit-banging on an Arduino Uno (16 MHz) typically yields a few hundred kHz. This is sufficient for occasional reads/writes but too slow for high-frequency polling.
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.
#include <virtuabotixRTC.h> #include <SD.h>
// --- Different ways to display time ---
The library defines the virtuabotixRTC class with several key methods: