Example Code for Arduino - RTC
Last revision 2026/02/05
Function Description
The DMX512 controller is equipped with an RTC clock chip, which can maintain the system clock running in offline state. The backup power supply can save the clock for about 12 hours in case of power failure.
You can set and read the date and time of the RTC clock, including year, month, day, hour, minute, second, and alarm settings.
The addition of clock function will greatly facilitate various timing controls, such as timed switch lights, color or pattern changes of lights in different time periods, and so on.
Example: Setting and Reading Clock
If you haven't downloaded and imported the DMX512 library yet, please download it from this link and import it: https://github.com/DFRobot/DFRobot_DMX512
In the following example, we will demonstrate how to set and read the RTC, and print it in the serial monitor. If you only need to read the clock the second time, you can delete the part of the code in "Setup" that sets the clock.
#include "DFRobot_Peripheral.h"
DFRobot_RTC rtc;
void setup() {
Serial.begin(115200);
while(rtc.begin() != 0){
Serial.println("init error");
delay(1000);
}
Serial.println("init succeed");
rtc.stopClock(); //Stop the timer.
rtc.setYear(23); //Set the year.
rtc.setMonth(rtc.eNov); //Set the month.
rtc.setDay(16); //Set the day.
rtc.setWeekDay(rtc.eThu); //Set the week.
rtc.setHour(9); //Set the hour.
rtc.setMinute(58); //Set the minute.
rtc.setSecond(30); //Set the second.
rtc.startClock(); //Start timing.
}
void loop(){
sTime_t time;
time = rtc.getTime();
Serial.print("year:20");
Serial.println(time.year);
Serial.print("month:");
Serial.println(time.month);
Serial.print("day:");
Serial.println(time.day);
Serial.print("weekday:");
Serial.println(time.week);
Serial.print("hour:");
Serial.println(time.hour);
Serial.print("minute:");
Serial.println(time.minute);
Serial.print("second:");
Serial.println(time.second);
Serial.println("--------------------------------------------------------");
delay(100);
}
Was this article helpful?
