Connecting your RTC module to an Arduino board requires five connections. You can use almost any digital pins on your Arduino for the data lines. RTC Module Pin Arduino Pin (Example) Description 5V or 3.3V Power supply GND Ground reference CLK (SCLK) Digital Pin 6 Serial Clock DAT (I/O) Digital Pin 7 Serial Data RST (CE) Digital Pin 8 Reset / Chip Enable 💻 Code Architecture
This is a rare but reported symptom where the RTC module appears to be counting time several times faster than normal. This typically indicates a hardware issue with the DS1302 module itself, such as a faulty crystal oscillator, rather than a software or library problem. If you experience this with different libraries, try replacing the RTC module.
#include <virtuabotixRTC.h> virtuabotixRTC myRTC(2, 3, 4); const int relayPin = 7;
While the VirtuabotixRTC library excels at its core task, the Arduino ecosystem offers other robust options, particularly . virtuabotixrtc.h arduino library
int lastSecond = 0; void loop() myRTC.updateTime(); if (myRTC.second != lastSecond) lastSecond = myRTC.second; // Print or process time only once per second Serial.println(myRTC.second);
Communicates using just three pins (CLK, DAT, RST).
VirtuabotixRTC myRTC(0x68); // typical I2C address for DS1307 Connecting your RTC module to an Arduino board
This library provides a straightforward interface to control the popular and cost-effective . Developed by Joseph Dattilo of Virtuabotix LLC, with a version as early as 0.4.5 released in 2011, it has become a go-to solution for hobbyists and educators looking to add real-time clocks to their Arduino-based devices.
If you want your Serial Monitor to display 09:05:02 instead of 9:5:2 , you can use the Arduino sprintf() function or a simple helper function like this:
If your DS1302 is running slow, check the voltage on pin 3.3V. Some modules have a diode that drops voltage. Powering the VCC pin with 5V and the backup battery with 3V can cause issues. Ensure the main power matches the chip's spec sheet. This typically indicates a hardware issue with the
// Turn off both LEDs at all other times else digitalWrite(ledPinRed, LOW); digitalWrite(ledPinGreen, LOW);
Uses only three digital pins (SCLK, I/O, and CE/RST) for communication.
This is a very common issue when working with cheap DS1302 modules. Users have reported that their clock runs five times faster than normal. This is almost always due to a faulty or low-quality 32.768kHz quartz crystal on the RTC module. The best solution is to or upgrade to a DS3231 module.
This article provides a comprehensive guide to using the virtuabotixRTC.h library, covering installation, wiring, key functions, and a complete, functional example for your projects. What is the virtuabotixRTC.h Library?
// 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);