Arduino M0 RTC Alarm Polled Mode Example

RTC Alarm in polled mode Example

This example shows you how to configure an alarm in polled mode using the RTCint library.
RTCint library gives you opportunity to use the internal RTC of ATSAMD21G18A, the microcontroller mounted on M0 and M0 PRO boards.

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
  • 1 green led
  • 1 red led
  • 2 resistor of 220O
  • 1 Buzzer
  • wires
  • 1 breadboard



Connect the anode of the red LED (usually the longer pin) in series to a resistence of 220 Ohm and to digital pin 12, then connect the cathode of led to GND of board.
Also connect, in the same way, the green led but taking care to connect the anode to pin 13.
Finally connect the positive pole of buzzer to pin 11 of board and the negative to the Ground.

Now you can upload the sketch using an USB cable.

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


This sketch turns on the green led. When the alarm occurs the green led goes off, the red led blinks , the buzzer plays and it prints on Serial monitor:"Alarm match!".
This example uses the methods of the RTCint library.
The alarm is enable with:"rtc.enableAlarm(SEC,ALARM_POLLED,NULL)".
It is possible to choose the alarm match inserting these parameters into parentheses:
-OFF alarm disabled
-SEC alarm match on seconds
-MMSS alarm match on minutes and seconds
-HHMMSS alarm match on hours, minute and seconds
-DDHHMMSS alarm match on day, hours, minutes and seconds
-MMDDHHMMSS alarm match on month, day hours, minutes and seconds
-YYMMDDHHMMSS alarm match on year, month, day, hours, minutes and seconds.
Also it is possible to indicate alarm mode inserting into parentheses: "ALARM_POLLED,NULL" for alarm polled or "ALARM_INTERRUPT,my_function" for interrupt.
In this example is used the polled mode and the Alarm is checked with the method:"rtc.alarmMatch()".
This method reads ALARM0 flag on RTC and return the current value.
If this flag is set then alarm match occurred, else there is no match.
First uploading the sketch, set the date, the current time and the alarm time.

The complete code and its detailed description are shown down.

* This sketch demonstrates how configure alarm in polled mode.
  In this mode you have to check continuously ALARM0 flag in the main program code.
/*RTC Alarm in polled mode*/
#include <RTCInt.h> //include RTCint library

RTCInt rtc;         //create an RTCInt type object
int buzzer=11;      //connect the buzzer to digital pin 11
int red=12;         //connect red led to digital pin 12
int green=13;       //connect green led to digital pin 13
void setup() 
  SerialUSB.begin(9600);       //serial communication initializing
  pinMode(green,OUTPUT);       //define green as output
  pinMode(red,OUTPUT);         //define red as output
  pinMode(buzzer,OUTPUT);      //define buzzer as output
  digitalWrite(green,LOW);     //initialize the green to LOW level
  digitalWrite(red,LOW);       //initialize the green to LOW level
  rtc.begin(TIME_H24);         //RTC initializing with 24 hour representation mode
  rtc.setTime(17,0,5,0);       //setting time (hour minute and second)
  rtc.setDate(13,8,15);        //setting date
  rtc.enableAlarm(SEC,ALARM_POLLED,NULL); //enabling alarm in polled mode and match on second
  rtc.local_time.hour=17;      //setting hour alarm
  rtc.local_time.minute=5;     //setting minute alarm
  rtc.local_time.second=10;    //setting second to match
  rtc.setAlarm();              //write second in alarm register
void loop()
   noTone(buzzer);                    //disable the buzzer
   digitalWrite(green,HIGH);          //turn on green led
   delay(500);                        //wait 500 millisecond
  if(rtc.alarmMatch())                //when match occurs red led on pin 12 blinks ten times
    SerialUSB.println("Alarm match!");//print on Serial monitor 
    for(int i=0; i < 10; i++)
    { digitalWrite(green,LOW);        //turn off green led
      tone(buzzer,200);               //play buzzer
      digitalWrite(red,HIGH);         //turn on red green
      delay(500);                     //wait 500 millisecond
      digitalWrite(red,LOW);          //turn off red green
      delay(500);                     //wait 500 millisecond  
    RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag