Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Real-Time Clock

real_time_clock.ino
/* Project: Smart Home Component: Arduino Ek R4 Built-in Real-Time Clock Sketch Description: This sketch has functions to set and get time using the Real-Time Clock on the board. This will also use a global NTP (Network Time Protocol) server (pool.ntp.org) if the board is connected to the Internet to get and set the accurate current date and time. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // This is included in the UNO R4 board package. #include "RTC.h" // Install the library "NTPClient" by Fabrice Weinberg from the IDE Library Manager. #include <NTPClient.h> #include <WiFiUdp.h> // PIN DEFINITIONS // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS WiFiUDP udp; // A UDP instance to let us send and receive packets over UDP NTPClient timeClient(udp); // LOCAL FUNCTIONS void setRTCTime(){ RTC.begin(); // Retrieve the time set on the board using an RTCTime object. RTCTime board_time; RTC.getTime(board_time); // If the board is just starting up (the default year for the RTC on the UNO R4 is 2000) // we should update the time. If WiFi is connected update to NTP time else to a manual random time. if (!RTC.isRunning()) { // This means the RTC is waking up "as new". if (board_time.getYear() == 2000){ // Get and set the board to NTP time if connected to WiFi. if(is_wifi_connected){ setNTPTime(); }else{ // A fallback time object, for setting a random time if no WiFi. // 01-Jan-2025, 00:00:00, Saturday RTCTime manual_time(1, Month::JANUARY, 2025, 00, 00, 00, DayOfWeek::SATURDAY, SaveLight::SAVING_TIME_INACTIVE); RTC.setTime(manual_time); } }else if(board_time.getYear() == 2025 && Month2int(board_time.getMonth()) == 1){ // If already set to manual time // Get and set the board to NTP time if connected to WiFi. if(is_wifi_connected){ setNTPTime(); } // else leave it at manual time. } } String date_time = getRTCTime(); if(OPERATING_MODE == 'D'){ Serial.println(date_time); } printToLCDByRow(1, date_time, true); if(OPERATING_MODE == 'D'){ Serial.println("The Real-Time Clock is ready!"); } } String getRTCTime() { RTCTime board_time; RTC.getTime(board_time); if(OPERATING_MODE == 'D'){ Serial.print("Current time: "); /* DATE */ Serial.print(board_time.getDayOfMonth()); Serial.print("/"); Serial.print(Month2int(board_time.getMonth())); Serial.print("/"); Serial.print(board_time.getYear()); Serial.print(" - "); /* HOURS:MINUTES:SECONDS */ Serial.print(board_time.getHour()); Serial.print(":"); Serial.print(board_time.getMinutes()); Serial.print(":"); Serial.println(board_time.getSeconds()); } String date_time = (String)board_time.getDayOfMonth() + "/" + (String)Month2int(board_time.getMonth()) + "/" + (String)board_time.getYear() + " " + (String)board_time.getHour() + ":" + (String)board_time.getMinutes() + ":" + (String)board_time.getSeconds(); return date_time; } void setNTPTime(){ Serial.println("\nStarting connection to NTP server..."); timeClient.begin(); timeClient.update(); // Get the current date and time from an NTP server and convert // it to UTC +5.5 by passing the time zone offset in hours. // You may change the time zone offset to your local one. auto timeZoneOffsetHours = 5.5; auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600); RTCTime timeToSet = RTCTime(unixTime); RTC.setTime(timeToSet); }