Note: You should upload the code once to set the time, then comment out the setDS1302Time line and upload the code again to prevent the Arduino from resetting the time to the compiled moment every time it resets.
Here is a complete, ready-to-run sketch that displays the current date and time in the Arduino Serial Monitor:
: When paired with a backup battery (like a CR2032), the RTC keeps time independently of the Arduino's power state. virtuabotixrtc.h arduino library
delay(500);
delay(30000); // Check every 30 seconds
// Brief delay to ensure RTC communication is stable delay(100);
The DS1302 module has a small silver coin cell battery slot (usually a CR2032). If your clock is losing time when the Arduino is unplugged, check or replace this battery. Note: You should upload the code once to
#include <virtuabotixRTC.h> virtuabotixRTC myRTC(2, 3, 4);
// LCD pin mapping (RS, E, D4, D5, D6, D7) LiquidCrystal lcd(12, 11, 5, 4, 3, 2); If your clock is losing time when the
Real-time tracking is essential for Arduino projects like data loggers, automated feeding systems, and smart clocks. The virtuabotixrtc.h library provides a straightforward interface to control popular, budget-friendly Real-Time Clock (RTC) modules. 🛠️ Supported Hardware