Alarm clock (DS3231)
Set a DS3231's alarm, catch its INT/SQW pin with an interrupt, and sound a buzzer until a button stops it.
Sensors · Intermediate
In a real product: Medication reminder (Medical): A precise clock wakes the device and sounds the reminder at each dose time.
A DS3231 is the clock to reach for when the time has to stay right: its crystal is temperature-compensated inside the chip, so it drifts about a minute a year. It also has two alarms that the chip checks by itself, so the sketch does not have to poll the time to know when to act.
What you'll learn
- Setting a real-time clock, and knowing when its time was lost (the OSF flag)
- Alarm masks: which fields of the time must match for an alarm to fire
- An open-drain interrupt line, caught with
attachInterruptand avolatileflag - Clearing a hardware flag so the line can fire again
How it works
At power-up the DS3231's oscillator-stop flag is set, so rtc.lostPower() is true and the sketch sets the time to 06:59:50. writeSqwPinMode(DS3231_OFF) sets the INTCN bit, which turns the INT/SQW pin into the alarm interrupt. setAlarm1(..., DS3231_A1_Hour) stores 07:00:00 with the day's mask bit set, so hours, minutes and seconds must match: the alarm fires once a day.
Ten seconds in, the chip sets its A1F flag and pulls INT/SQW low. That falling edge runs onAlarm, which only sets a flag, and loop does the rest: the buzzer warbles until the button is pressed, then clearAlarm(1) clears A1F and the pin goes high again. The interrupt routine stays tiny because the I²C library cannot be used inside one.
Try this
- Change the alarm to
DS3231_A1_Secondand a time with seconds 30: it fires every minute at :30. - Use alarm 2 instead (
setAlarm2, minutes and hours only): it fires at second 00. - Click the DS3231 while it runs and change the temperature:
getTemperature()follows in quarter degrees.
sketch.ino
// A DS3231 alarm clock: alarm 1 pulls INT/SQW low at 07:00:00, an interrupt on pin 2 sees it, and the buzzer
// sounds until the button is pressed.
#include <RTClib.h>
RTC_DS3231 rtc;
const int INT_PIN = 2, BUZZER = 8, BUTTON = 4;
volatile bool alarmed = false;
void onAlarm() { alarmed = true; }
void setup() {
Serial.begin(9600);
pinMode(INT_PIN, INPUT_PULLUP);
pinMode(BUTTON, INPUT_PULLUP);
if (!rtc.begin()) {
Serial.println("No DS3231");
while (true) {}
}
if (rtc.lostPower()) {
Serial.println("Time lost: setting it");
rtc.adjust(DateTime(2026, 10, 18, 6, 59, 50));
}
rtc.disable32K();
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.disableAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF); // INT/SQW becomes the alarm interrupt
// Every day at 07:00:00: hours, minutes and seconds must match.
rtc.setAlarm1(DateTime(2026, 10, 18, 7, 0, 0), DS3231_A1_Hour);
attachInterrupt(digitalPinToInterrupt(INT_PIN), onAlarm, FALLING);
Serial.println("Alarm set for 07:00:00");
}
void loop() {
// Print each time the clock's second changes.
static uint8_t lastSecond = 60;
DateTime now = rtc.now();
if (now.second() != lastSecond) {
lastSecond = now.second();
char text[] = "hh:mm:ss";
Serial.print(now.toString(text));
Serial.print(" ");
Serial.print(rtc.getTemperature(), 2);
Serial.println(" C");
}
if (alarmed) {
tone(BUZZER, (millis() / 250) % 2 ? 2000 : 1500);
if (digitalRead(BUTTON) == LOW) {
noTone(BUZZER);
rtc.clearAlarm(1); // A1F cleared: INT/SQW goes high again
alarmed = false;
Serial.println("Alarm stopped");
}
}
}