Arduino M0 Pro 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 M0PRO 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 into 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.


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 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.

When you use the "native USB"port you must change "Serial" with "SerialUSB".

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() 
  Serial.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
 Serial.print(rtc.local_date.year+2000); //year
 Serial.print(rtc.local_date.month);     //month
 Serial.print(rtc.local_date.day);       //day
 Serial.print(' ');
 //printing time
 Serial.print(rtc.local_time.hour);      //hour
 Serial.print(rtc.local_time.minute);    //minute
 Serial.println(rtc.local_time.second);  //second
 delay(1000);       //wait 1 second