Example Code for Arduino-Alarm Trigger Interrupt
Last revision 2025/12/14
This blog post provides a step-by-step guide on setting up an alarm trigger interrupt on Arduino using the DS3231M RTC module. It covers hardware and software preparation, wiring diagrams, and sample code to implement precise alarm functionalities, making it ideal for DIY electronics enthusiasts looking to enhance their Arduino projects.
Hardware Preparation
- DFR0216-2 DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B x 1
- DFR0641 DS3231M MEMS Precise RTC Module x 1
- FIT0916-FF DuPont Wires x5
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Download the DFRobot_DS3231M library: DFRobot DS3231M library
- About how to install the library?
Wiring Diagram

Other Preparation Work
- Install the DS3231M Library in Arduino IDE.
- Connect the DS3231M module's INT pin to the Arduino UNO R3's interrupt pin D2.
- Connect other pins as per the Wiring Diagram.
Sample Code
/*!
* @file setAlarmInterrupt.ino
* @brief Set alarm, and use interrput pin to trigger it
* @n Experiment phenomenon: set the alarm clock to trigger at a specified time
* @n connect SQW pin with DIGITALPIN2
* @n print information on serial port after the alarm clock is triggered.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [LuoYufeng]([email protected])
* @version V0.1
* @date 2019-08-19
* @url https://github.com/DFRobot/DFRobot_DS3231M
*/
#include "DFRobot_DS3231M.h"
volatile int8_t alarmFlag = 0;
DFRobot_DS3231M rtc;
void setup(void)
{
Serial.begin(9600);
/*Wait for the chip to be initialized completely, and then exit*/
while(rtc.begin() != true){
Serial.println("failed to init chip, please check if the chip connection is correct. ");
delay(1000);
}
/*!
*@brief Set the value of pin sqw
*@param mode eDS3231M_OFF = 0x01 // Not output square wave, enter interrupt mode
*@n eDS3231M_SquareWave_1Hz = 0x00 // 1Hz square wave
*@n eDS3231M_SquareWave_1kHz = 0x08 // 1kHz square wave
*@n eDS3231M_SquareWave_4kHz = 0x10 // 4kHz square wave
*@n eDS3231M_SquareWave_8kHz = 0x18 // 8kHz square wave
*/
rtc.writeSqwPinMode(eDS3231M_OFF);
/*!
*@brief enable Alarm1 interrupt
*/
rtc.enAbleAlarm1Int();
/*!
*@brief disable Alarm1 interrupt
*/
//rtc.disAbleAlarm1Int();
/*!
*@brief enable Alarm2 interrupt
*/
rtc.enAbleAlarm2Int();
/*!
*@brief disable Alarm2 interrupt
*/
//rtc.disAbleAlarm2Int();
/*!
*@brief Set alarm clock
*@param alarmType Alarm clock working mode typedef enum{
*@n eEverySecond,
*@n eSecondsMatch,
*@n eSecondsMinutesMatch,
*@n eSecondsMinutesHoursMatch,
*@n eSecondsMinutesHoursDateMatch,
*@n eSecondsMinutesHoursDayMatch, //Alarm1
*@n
*@n eEveryMinute,
*@n eMinutesMatch,
*@n eMinutesHoursMatch,
*@n eMinutesHoursDateMatch,
*@n eMinutesHoursDayMatch, //Alarm2
*@n eUnknownAlarm
*@n }eAlarmTypes;
*@param days Alarm clock (day)
*@param hours Alarm clock (hour)
*@param mode: e24hours, eAM, ePM
*@param minutes Alarm clock (minute)
*@param seconds Alarm clock (second)
*/
//Alarm1
rtc.setAlarm(eSecondsMatch,/*date,0-30*/30,/*hour,1-12 in 12hours,0-23 in 24hours*/15,e24hours,/*minute,0-59*/12,/*second,0-59*/35);
//Alarm2
rtc.setAlarm(eMinutesHoursDayMatch,/*date,0-30*/30,/*hour,1-12 in 12hours,0-23 in 24hours*/15,e24hours,
/*minute,0-59*/13,/*second,0-59, this argument doesn't work in Alarm2*/42);
/*!
*@brief Judge if it is power-down
*@return if return true, power-down, time needs to reset; false, work well
*/
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
/*!
*@brief Adjust the current time
*/
rtc.setYear(19);//Set year, default in the 21st century.
rtc.setMonth(9);
rtc.setDate(30);
/*!
*@brief Set the hours and 12hours or 24hours
*@param hour:1-12 in 12hours,0-23 in 24hours
*@param mode:e24hours, eAM, ePM
*/
rtc.setHour(15,e24hours);
rtc.setMinute(12);
rtc.setSecond(30);
rtc.adjust();
}
attachInterrupt(0, interrupt, FALLING);
}
void loop() {
/*!
*@brief Judge if the alarm clock is triggered
*@return true, triggered; false, not triggered
*/
rtc.getNowTime();
Serial.print(rtc.year(), DEC);
Serial.print('/');
Serial.print(rtc.month(), DEC);
Serial.print('/');
Serial.print(rtc.day(), DEC);
Serial.print(" (");
Serial.print(rtc.getDayOfTheWeek());
Serial.print(") ");
Serial.print(rtc.hour(), DEC);
Serial.print(':');
Serial.print(rtc.minute(), DEC);
Serial.print(':');
Serial.print(rtc.second(), DEC);
Serial.print(' ');
/*if rtc works in 24hours mode,this function doesn't print anything*/
Serial.print(rtc.getAMorPM());
Serial.println();
if(alarmFlag == 1){
alarmFlag = 0;
Serial.println("Alarm clock is triggered.");
delay(1000);
rtc.clearAlarm();
}
else
delay(1000);
if (rtc.lostPower()) {
Serial.println("RTC lost power, please reset the time!");
}
}
void interrupt(){
alarmFlag = 1;
}
Result
The serial port prints the time and triggers the alarm to interrupt at regular intervals.

Additional Information
NOTE:In this sample, the pin INT of the sensor should be connected to the corresponding interrupt pin of the mainboard(Here selected the interrput pin D2 of UNO board).
AVR Series Interrupt Pin and Interrupt Number
| 328 Mainboards: Uno, Nano, Mini... | Mega2560 | 32u4 Mainboards: Leonardo... | |
|---|---|---|---|
| Interrupt Pin | D2, D3 | D2, D3, D21, D20, D19, D18 | D3, D2, D0, D1, D7 |
| Interrupt Number | 0, 1 | 0, 1, 2, 3, 4, 5 | 0, 1, 2, 3 ,4 |
Was this article helpful?
