The Mini Ultra Pro comes with a built-in Real Time Clock (RTC) peripheral on the ATSAMD21G18A-AU microcontroller. It supports full calendar capability with alarm function. By having the RTC built-in, you can avoid from using an external RTC chip like DS1307 and PCF8563. This save you from needing to spare couple of pins for the interface with the RTC chip, an external crystal and some passive components.
The Arduino team has written a RTC library specifically for the Arduino Zero which Mini Ultra Pro is compatible with. You can download the latest revision of the library from the Arduino IDE through “Sketch >> Include Library >> Manage Libraries”. Search for “RTCZero”.
Install the latest revision of the library and perform any update if your installed version is not up to date.
The installed RTCZero library comes with several examples to get you started on using the RTC. Open the SimpleRTC example and connect your Mini Ultra Pro to the PC using the USB connection.
The example sketch uses the default Serial (used on other non-native USB Serial port like Arduino Uno, Mega2560) and if we were to upload the sketch without modifying them, we won’t be able to see any debug messages on the serial monitor when the USB on the Mini Ultra Pro is used. We need to make some small changes to the code in the SimpleRTC to use the SerialUSB instead.
Simple add the following line of code in the sketch to replace the use of Serial with SerialUSB instead.
#include <RTCZero.h> /* Create an rtc object */ RTCZero rtc; #define Serial SerialUSB // ***** Add this line of code *****
On top of that, we would need to wait for the USB connection to be up before sending any debug messages. Add the following line of code.
void setup() { // Wait until USB connection is up while(!SerialUSB); // ***** Add this line of code ***** Serial.begin(9600);
Upload the modified sketch to the Mini Ultra Pro. Open the serial monitor at 9600 bps baud rate once the sketch is uploaded.
One of the most important feature of the RTC is the capability to set alarm. This allow the scheduling of a specific process or task when a predefined time and date that has been set earlier is reached. The RTCZero library has included an example of using the alarm feature. Open up the example SimpleRTCAlarm and make the similar changes to the code like in the previous SimpleRTC example.
/* Simple RTC Alarm for Arduino Zero and MKR1000 Demonstrates how to set an RTC alarm for the Arduino Zero and MKR1000 This example code is in the public domain http://arduino.cc/en/Tutorial/SimpleRTCAlarm created by Arturo Guadalupi <a.guadalupi@arduino.cc> 25 Sept 2015 modified 21 Oct 2015 */ #include <RTCZero.h> /* Create an rtc object */ RTCZero rtc; #define Serial SerialUSB // ***** Add this line of code ***** /* Change these values to set the current initial time */ const byte seconds = 0; const byte minutes = 0; const byte hours = 16; /* Change these values to set the current initial date */ const byte day = 25; const byte month = 9; const byte year = 15; void setup() { while(!SerialUSB); // ***** Add this line of code ***** Serial.begin(9600); rtc.begin(); // initialize RTC 24H format rtc.setTime(hours, minutes, seconds); rtc.setDate(day, month, year); rtc.setAlarmTime(16, 0, 10); rtc.enableAlarm(rtc.MATCH_HHMMSS); rtc.attachInterrupt(alarmMatch); } void loop() { } void alarmMatch() { Serial.println("Alarm Match!"); }
In this example, the alarm is set 10 seconds after start up. Once the alarm time is reached, you should be able to see the “Alarm Match!” message on the serial monitor.