Arduino RTC DS3231 Alarm System

This is a simple script for an Arduino-based alarm clock using the DS3231 Real-Time Clock module. It triggers a buzzer when the current time matches the predefined alarm constants.
 avatar
Xauraa
c_cpp
4 months ago
1.4 kB
7
Indexable
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

// Settings
const int buzzerPin = 8;    // Buzzer connected to digital pin 8
const int alarmHour = 07;   // Set alarm hour (24h format)
const int alarmMinute = 30; // Set alarm minute

void setup() {
      pinMode(buzzerPin, OUTPUT);
        Serial.begin(9600);

          if (!rtc.begin()) {
                Serial.println("Couldn't find RTC");
                    while (1);
          }

            // Set the time if the RTC lost power
              if (rtc.lostPower()) {
                    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
              }
}

void loop() {
      DateTime now = rtc.now();

        // Print current time to Serial Monitor
          Serial.print(now.hour());
            Serial.print(':');
              Serial.println(now.minute());

                // Check if current time matches alarm time
                  if (now.hour() == alarmHour && now.minute() == alarmMinute) {
                        activateAlarm();
                  } else {
                        noTone(buzzerPin); // Keep buzzer silent
                  }

                    delay(1000); // Check every second
}

void activateAlarm() {
      // Simple beeping pattern
        tone(buzzerPin, 1000); // Send 1KHz sound signal
          delay(500);
            noTone(buzzerPin);
              delay(500);
}

}
                  }
                  }
}
              }
          }
}
Editor is loading...
Leave a Comment