Automatically handles seconds, minutes, hours, day of the week, day of the month, month, and year. Hardware Wiring: Connecting DS1302 to Arduino
// UNCOMMENT THE NEXT LINE TO SET TIME, THEN COMMENT IT BACK AND RE-UPLOAD // myRTC.setDS1302Time(00, 30, 14, 4, 25, 10, 2023);
| Method | Purpose | |--------|---------| | setDS1302Time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) | Writes all time registers (expects BCD values or standard ints, internally converted). | | updateTime() | Reads all time registers from the DS1302 into library variables. | | getSeconds() , getMinutes() , ... | Return cached time values (must call updateTime() first). | | setDOW(byte DOW) | Directly set day of week register. | | setTime(byte hour, byte minute, byte second) | Set time without date. | | setDate(byte month, byte day, byte year) | Set date without time. | | writeRAM(byte address, byte value) | Write to one of 31 bytes of battery-backed RAM (address 0-30). | | readRAM(byte address) | Read from that RAM location. | virtuabotixrtc.h arduino library
To use the library in your Arduino sketch, follow these steps: Open the Arduino IDE.
Because the virtuabotixRTC library is a community-developed tool, it may not always appear in the official Arduino IDE Library Manager. You can install it manually using these steps: Automatically handles seconds, minutes, hours, day of the
void setup() Serial.begin(9600);
The DS1302 can store time in 12-hour format with AM/PM flags. Use myRTC.hourmode which returns 12-hour value, and myRTC.ampm (1 = PM, 0 = AM). You set the mode during setDS1302Time() . | | getSeconds() , getMinutes() ,
The Virtuabotix RTC library simplifies the process of using the DS1307 chip with Arduino, allowing users to focus on developing their projects rather than dealing with low-level communication protocols.
void loop() myRTC.updateTime(); int tempReading = analogRead(A0); float voltage = tempReading * (5.0 / 1023.0); float temperatureC = (voltage - 0.5) * 100;
#include <virtuabotixRTC.h> virtuabotixRTC myRTC(2, 3, 4); const int relayPin = 7;
Here is an example of how to create an instance of the VirtuabotixRTC class: