Example Code for Arduino-Alarm Trigger
Last revision 2025/12/14
This article offers a step-by-step guide on setting up an alarm trigger using Arduino and the DS3231M RTC module, including hardware and software requirements, wiring instructions, and detailed example code to ensure precise alarm functionality.
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 to the Arduino UNO R3 using jumper wires as per the Wiring Diagram.
Sample Code
/*!
* @file setAlarmInQuery.ino
* @brief Set alarm clock
* @n Experiment phenomenon: set the alarm clock to trigger at a sepcified time
* @n Trigger when the set time of clock is the same with the RTC time
*
* @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"
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 fine");
delay(1000);
}
/*!
*@brief Set the vaule 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 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 eEveryMinute,
*@n eMinutesMatch,
*@n eMinutesHoursMatch,
*@n eMinutesHoursDateMatch,
*@n eMinutesHoursDayMatch, //Alarm2
*@n eUnknownAlarm
*@n }eAlarmTypes;
*@param days Alarm clock Day (day)
*@param hours Alarm clock Hour (hour)
*@param mode: e24hours, eAM, ePM
*@param minutes Alarm clock (minute)
*@param seconds Alarm clock (second)
*/
rtc.setAlarm(eSecondsMatch,/*date,0-30*/27,/*hour,1-12 in 12hours,0-23 in 24hours*/12,eAM,/*minute,0-59*/0,/*second,0-59*/0);
rtc.setAlarm(eMinutesHoursDateMatch,/*date,0-30*/27,/*hour,1-12 in 12hours,0-23 in 24hours*/12,eAM,/*minute,0-59*/0,/*second,0-59*/0);
rtc.setYear(19);//Set year, default in the 21st century,
rtc.setMonth(8);
rtc.setDate(26);
/*!
*@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(11,ePM);//1-12 in 12hours,0-23 in 24hours
rtc.setMinute(59);
rtc.setSecond(50);
rtc.adjust();
}
void loop() {
/*!
*@brief Get current time data
*@return Current time data
*/
rtc.getNowTime();
/*!
*@brief Judge if the alarm clock is triggered
*@return true, triggered; false, not triggered
*/
if (rtc.isAlarm()){ // If the alarm bit is set
Serial.println("Alarm clock is triggered.");
/*!
*@brief Clear trigger flag
*/
rtc.clearAlarm();
}
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 (rtc.lostPower()) {
Serial.println("RTC lost power, please reset the time!");
}
delay(1000);
}
Result
The serial port prints the time and triggers an interrupt at the specified time.

Additional Information
NOTE: If the hour systems of the clock and alarm clock are not the same, the alarm clock will not be triggered. For example: the clock is 24-hour system, and the alarm clock is 12-hour system.
Was this article helpful?
