Virtuabotixrtc.h Arduino Library
After completing the connections, upload the following code to your Arduino.
lcd.clear(); lcd.setCursor(0, 0); lcd.print("Initializing...");
: If you manually manipulate registers without conversion, or if you mix methods, you may write invalid BCD values (e.g., 0x1A for seconds, where A is invalid). The library does not validate register writes beyond basic bounds.
Before writing code, you must connect your RTC module to the Arduino. While the DS1302 uses a serial interface, it is not standard I2C or SPI. Fortunately, the library allows you to use almost any digital pins on your Arduino. Typical Pin Mapping DS1302 Pin Description Example Arduino Pin Power Supply (usually 3.3V or 5V) 5V or 3.3V GND CLK / SCLK Serial Clock Digital Pin 6 DAT / IO Serial Data Digital Pin 7 RST / CE Reset / Chip Enable Digital Pin 8 Installation Guide virtuabotixrtc.h arduino library
To use the library, you must first install it (often manually via a ZIP from GitHub ) and then include it in your sketch. Problem with code for Arduino using an RTC - Programming
void setup() Serial.begin(9600); // Uncomment once to set time (Year offset from 2000: 25 = 2025) // myRTC.setDS1302Time(0, 30, 14, 3, 15, 4, 25);
: Uses a three-wire interface (CLK, DAT, RST) to communicate with the Arduino. After completing the connections, upload the following code
. This is usually called once in setup() to calibrate the clock. updateTime()
that defines which Arduino pins are used for the CLK, DAT, and RST connections. setDS1302Time(sec, min, hr, dayW, dayM, mon, yr)
Setting and fetching time requires just a few intuitive function calls. Before writing code, you must connect your RTC
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.
// Create an RTC object: virtuabotixRTC myRTC(CLK_PIN, DAT_PIN, RST_PIN); // Using pins 6, 7, and 8 as per the common wiring example. virtuabotixRTC myRTC(6, 7, 8);