!exclusive! — Virtuabotixrtc.h Arduino Library
// Pin connections: (SCLK, I/O, CE) VirtuabotixRTC myRTC(6, 7, 8);
| | Arduino Pin | | :--- | :--- | | VCC | 5V | | GND | GND | | CLK (Serial Clock) | 6 (or any digital pin) | | DAT (I/O / Data) | 7 (or any digital pin) | | RST (Reset / CE) | 8 (or any digital pin) |
An Arduino does not keep track of time (hours, minutes, seconds) accurately once powered off. A Real Time Clock module, powered by a small coin cell battery, keeps ticking even when the Arduino is turned off. This library allows the Arduino to:
// SET THE TIME ON THE RTC (RUN ONCE) // Set to: 0 seconds, 30 minutes, 14 hours, Sunday (1), 7th day, April (4), 2024 // myRTC.setDS1302Time(0, 30, 14, 1, 7, 4, 2024); // Comment this line after you set the time. virtuabotixrtc.h arduino library
Here is a complete, ready-to-run sketch that displays the current date and time in the Arduino Serial Monitor:
It includes a dedicated function, setDS1302Time() , which takes seven parameters to calibrate the clock.
Understanding its internal bit-banging, BCD conversion, and RAM access unlocks the full potential of the DS1302. While newer chips offer better accuracy and simpler interfaces, the DS1302 and VirtuabotixRTC remain a robust, economical choice for countless embedded timekeeping tasks. // Pin connections: (SCLK, I/O, CE) VirtuabotixRTC myRTC(6,
For example, to print the current hour to the Serial Monitor, you would write:
: You can set the initial time (seconds, minutes, hours, day of the week, day of the month, month, year) using a single function call like myRTC.setDS1302Time(...) Time Retrieval : After calling myRTC.updateTime()
This is almost always an installation problem. The Arduino IDE cannot find the library files. Re-download the library ZIP file and re-add it using the Sketch → Include Library → Add .ZIP Library menu path. Here is a complete, ready-to-run sketch that displays
void loop() myRTC.updateTime(); if (myRTC.minutes == 0) // Every hour at :00 Serial.print(myRTC.hours); Serial.print(":"); Serial.print(myRTC.minutes); Serial.print(" - Temperature: 22.3 C"); delay(60000); // Avoid multiple logs
: