Arduino M0 RTC Example

RTC Example

This example shows you how to use the RTCint library and to print on the Serial monitor the date and the current time.
RTCint library gives you opportunity to use the internal RTC of ATSAMD21G18A, the microcontroller mounted on M0 and M0 PRO boards.
This library uses RTC in clock calendar mode with alarm.
Time can be represented in 12h or 24h mode.
You need the RTCint library to use this sketch. It is contained into the new IDE 1.7.7 and Arduino Studio 0.0.4. Download it now!! ! Click here.

Hardware Required

  • Arduino M0/M0 Pro



Connect your board to PC with a USB cable and upload the sketch.

In the picture is shown the circuit realized using an Arduino M0 Pro.


This sketch prints on Serial monitor the date and the current time using the methods of the RTCint library.
It is possible to choose how to represent the time between 12h or 24h mode, inserting into "rtc.begin()"the parameters TIME_H12 or TIME_H24.
Time is get from method "rtc.getTime()" .
This method retrieves the hours, the minutes and the seconds from RTC and fills an internal structure called local_time that it is accessed to read the current time.
Instead the date is get from method "rtc.getDate()" .
This method fills an internal structure called local_date, accessible in the RTCInt class, with day, month and year read from RTC. For a correct representation of year is added an appropriate offset to this field.
The informations so stored are printed on Serial monitor and updated each second.
First uploading the sketch, set the date and the current time, inserting the corresponding values into respective methods "rtc.set()".

The complete code and its detailed description are shown down.

This sketch gives a simple demonstration of how to use RTCint library.
The code sets date and time using internal structure and then prints them on serial monitor .
In this example the Time is represented in 24 hour mode

#include <RTCInt.h>// include the RTCint library

RTCInt rtc;             //create an RTCInt type object
void setup() 
  SerialUSB.begin(9600);//initialize the Serial communication
  rtc.begin(TIME_H24);  //init RTC in 24 hour mode
  //time settings
  CHour(15,0);  //setting hour
  rtc.setMinute(43);   //setting minute
  rtc.setSecond(0);    //setting second
  rtc.setDay(13);      //setting day
  rtc.setMonth(8);     //setting month
  rtc.setYear(15);     //setting year
void loop() 
 rtc.getDate();       //getting date in local structure (local_date)
 rtc.getTime();       //getting time in local structure(local_time)
 //printing date in format YYYY/MM/DD
 SerialUSB.print(rtc.local_date.year+2000); //year
 SerialUSB.print(rtc.local_date.month);     //month
 SerialUSB.print(rtc.local_date.day);       //day
 SerialUSB.print(' ');
 //printing time
 SerialUSB.print(rtc.local_time.hour);      //hour
 SerialUSB.print(rtc.local_time.minute);    //minute
 SerialUSB.println(rtc.local_time.second);  //second
 delay(1000);          //wait 1 second