In this tutorial you will learn how to access the real-time clock (RTC) on an Arduino UNO R4 WiFi board. The RTC is embedded in the UNO R4 WiFi's microcontroller (RA4M1).
The goals of this project are:
The RTC on the UNO R4 WiFi can be accessed using the RTC library that is included in the Renesas core. This library allows you to set/get the time as well as using alarms to trigger interrupts.
The UNO R4 WiFi features a VRTC pin, that is used to keep the onboard RTC running, even when the boards power supply is is cut off. In order to use this, apply a voltage in the range of 1.6 - 3.6 V to the VRTC pin.
There are many practical examples using an RTC, and the examples provided in this page will help you get started with it.
RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE)
RTC.setTime(startTime)
To set the starting time for the RTC, you can create an
RTCTime
object. Here you can specify the day, month, year, hour, minute, second, and specify day of week as well as daylight saving mode.Then to set the time, use the
setTime()
method.Example:
1#include "RTC.h"2
3void setup() {4 Serial.begin(9600);5
6 RTC.begin();7 8 RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);9
10 RTC.setTime(startTime);11}12
13void loop(){14}
RTC.getTime(currentTime)
To retrieve the time, we need to create a
RTCTime
object, and use the getTime()
method to retrieve the current time.This example sets & gets the time and stores it in an
RTCTime
object called currentTime
.1#include "RTC.h"2
3void setup() {4 Serial.begin(9600);5
6 RTC.begin();7 8 RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);9
10 RTC.setTime(startTime);11}12
13void loop(){14RTCTime currentTime;15
16// Get current time from RTC17RTC.getTime(currentTime);18}
The above examples show how to set & get the time and store it in an object. This data can be retrieved by a series of methods:
getDayOfMonth()
getMonth()
getYear()
getHour()
getMinutes()
getSeconds()
The example below prints out the date and time from the
currentTime
object.1#include "RTC.h"2
3void setup() {4 Serial.begin(9600);5
6 RTC.begin();7 8 RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);9
10 RTC.setTime(startTime);11}12
13void loop() {14 RTCTime currentTime;15
16 // Get current time from RTC17 RTC.getTime(currentTime);18
19 // Print out date (DD/MM//YYYY)20 Serial.print(currentTime.getDayOfMonth());21 Serial.print("/");22 Serial.print(Month2int(currentTime.getMonth()));23 Serial.print("/");24 Serial.print(currentTime.getYear());25 Serial.print(" - ");26
27 // Print time (HH/MM/SS)28 Serial.print(currentTime.getHour());29 Serial.print(":");30 Serial.print(currentTime.getMinutes());31 Serial.print(":");32 Serial.println(currentTime.getSeconds());33
34 delay(1000);35}
currentTime.getUnixTime()
To retrieve the Unix timestamp, use the
getUnixTime()
method.1#include "RTC.h"2
3void setup() {4 Serial.begin(9600);5
6 RTC.begin();7 8 RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);9
10 RTC.setTime(startTime);11}12
13void loop() {14 RTCTime currentTime;15
16 // Get current time from RTC17 RTC.getTime(currentTime);18 19 //Unix timestamp20 Serial.print("Unix timestamp: ");21 Serial.println(currentTime.getUnixTime());22
23 delay(1000);24}
A periodic interrupt allows you to set a recurring callback.
To use this, you will need to initialize the periodic callback, using the
setPeriodicCallback()
method:RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)
You will also need to create a function that will be called:
void periodicCallback() { code to be executed }
Note the IRQ has a very fast execution time. Placing a lot of code is not a good practice, so in the example below we are only switching a single flag,
.irqFlag
The example below blinks a light every 2 seconds:
1#include "RTC.h"2
3volatile bool irqFlag = false;4volatile bool ledState = false;5
6const int led = LED_BUILTIN;7
8void setup() {9 pinMode(led, OUTPUT);10
11 Serial.begin(9600);12
13 // Initialize the RTC14 RTC.begin();15
16 // RTC.setTime() must be called for RTC.setPeriodicCallback to work, but it doesn't matter17 // what date and time it's set to18 RTCTime mytime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);19 RTC.setTime(mytime);20
21 if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_2_SEC)) {22 Serial.println("ERROR: periodic callback not set");23 }24}25
26void loop(){27 if(irqFlag){28 Serial.println("Timed CallBack");29 ledState = !ledState;30 digitalWrite(LED_BUILTIN, ledState);31 irqFlag = false;32 }33}34
35void periodicCallback()36{37 irqFlag = true;38}
The period can be specified using the following enumerations:
ONCE_EVERY_2_SEC
ONCE_EVERY_1_SEC
N2_TIMES_EVERY_SEC
N4_TIMES_EVERY_SEC
N8_TIMES_EVERY_SEC
N16_TIMES_EVERY_SEC
N32_TIMES_EVERY_SEC
N64_TIMES_EVERY_SEC
N128_TIMES_EVERY_SEC
N256_TIMES_EVERY_SEC
RTC.setAlarmCallback(alarm_cbk, alarmtime, am)
1#include "RTC.h"2
3void setup() {4 Serial.begin(9600);5
6 RTC.begin();7
8 RTCTime alarmtime;9 alarmtime.setSecond(35);10
11 AlarmMatch am;12 am.addMatchSecond();13
14 if (!RTC.setAlarmCallback(alarm_cbk, alarmtime, am)) {15 Serial.println("ERROR: alarm callback not set");16 }17}18
19void alarm_cbk() {20 Serial.println("ALARM INTERRUPT");21}
To retrieve and store the current time, we can make a request to an NTP server,
pool.ntp.org
. This will retrieve the UNIX time stamp and store it in an RTC
object. 1
Please also note that you will need to create a new tab called
arduino_secrets.h
. This is used to store your credentials. In this file, you will need to add:1#define SECRET_SSID "" //network name2#define SECRET_PASS "" //network password
This tutorial shows how to use the RTC on the UNO R4 WiFi, such as setting a start time, setting an alarm, or obtaining time in calendar or unix format.
Read more about this board in the Arduino UNO R4 WiFi documentation.