Project tutorial
Arduino Alarm

Arduino Alarm

With the use of a DS1307 RTC Module, you can accurately set alarms and read the time on an LCD Display.

  • 547 views
  • 2 comments
  • 5 respects

Components and supplies

Ardgen mega
Arduino Mega 2560 & Genuino Mega 2560
×1
LCD Display
×1
DS1307 RTC Module
×1
Active Buzzer Module
×1
Push Button Module
×1
Breadboard
×1
Jumper Wire
×23

Apps and online services

About this project

Youtube tutorial:

Notes:

When downloading the code remember to download the code twice the first time with this line not commented out and the second time with it commented out.

 //clock.setDateTime(__DATE__, __TIME__);

Notes for Downloading Code:

When downloading the code make a new sketch and add 2 new tabs and paste each sketch of the code into these tabs.

Code

Arduino AlarmArduino
#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
DS3231 clock;
RTCDateTime dt;

int buzzer = 2; //Alarm
int button = 4; //Snooze
int button_read;
bool alarm_set;
bool alarm_beep;
String alarm_hour;
String alarm_min;
String alarm_AM_PM;
int time_hour;
String time_AM_PM;
int i; //Frequency of Alarm

void setup() {

  lcd.begin(16, 2);

  Serial.begin(9600);

  pinMode(buzzer, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  clock.begin();
    //clock.setDateTime(__DATE__, __TIME__);

  lcd.setCursor(6, 1);
  lcd.print(" Set Alarm");

  Serial.print("Enter Hour of Alarm");
  Serial.println();

  alarm_set =  false;
  alarm_beep = true;
}

void loop() {
  dt = clock.getDateTime();

  if (alarm_hour == 0) {
    if (Serial.available() > 0) {
      alarm_hour = Serial.readString();
      Serial.println("Alarm Hour Set: " + alarm_hour);
      lcd.clear();
      Serial.print("Enter Minute of Alarm");
      Serial.println();
      Serial.end();
      Serial.begin(9600);
    }
  }
  if (alarm_min == 0) {
    if (Serial.available() > 0) {
      alarm_min = Serial.readString();
      Serial.println("Alarm Minute Set: " + alarm_min);
      lcd.clear();
      Serial.print("Enter Ante Meridiem of Alarm");
      Serial.println();
      Serial.end();
      Serial.begin(9600);
    }
  }
  if (alarm_AM_PM == 0) {
    if (Serial.available() > 0) {
      alarm_AM_PM = Serial.readString();
      Serial.println("Alarm Ante Meridiem Set: " + alarm_AM_PM);
      lcd.clear();
      Serial.print("Alarm Set!");
      Serial.println();
      Serial.end();
      Serial.begin(9600);
      alarm_set = true;
    }
  }

  Time_Display();

  Alarm_Display();

  if ((alarm_beep == true) && (alarm_set == true) && (alarm_min.toInt() == dt.minute) && (alarm_hour.toInt() == time_hour) && (time_AM_PM == alarm_AM_PM)) {
    for (i = 0; i < 80; i++)
    {
      digitalWrite(buzzer, HIGH);
      delay(1);
      digitalWrite(buzzer, LOW);
      delay(1);
      Time_Display();
    }
    for (i = 0; i < 100; i++)
    {
      digitalWrite(buzzer, HIGH);
      delay(2);
      digitalWrite(buzzer, LOW);
      delay(2);
      Time_Display();
    }
    button_read = digitalRead(button);
    if (button_read == LOW) {
      alarm_beep = false;
      Time_Display();
    }
  }
}
Alarm DisplayArduino
void Alarm_Display()  {
  
  Time_Display();
  
  lcd.setCursor(0, 1);
  lcd.print("Alarm:");

  if (alarm_set == true) {
    if (alarm_hour.toInt() > 9) {
      lcd.setCursor(6, 1);
      lcd.print(alarm_hour.toInt());
    }
    else {
      lcd.setCursor(7, 1);
      lcd.print(alarm_hour.toInt());
    }

    lcd.setCursor(8, 1);
    lcd.print(":");

    if (alarm_min.toInt() > 9) {
      lcd.setCursor(9, 1);
      lcd.print(alarm_min.toInt());
    }
    else {
      lcd.setCursor(9, 1);
      lcd.print("0");
      lcd.setCursor(10, 1);
      lcd.print(alarm_min.toInt());
    }

    lcd.setCursor(12, 1);
    lcd.print(alarm_AM_PM);

    lcd.setCursor(11, 1);
    lcd.print(" ");
    lcd.setCursor(14, 1);
    lcd.print("    ");
  }
  Time_Display();
}
Time DisplayArduino
void Time_Display()  {
  lcd.setCursor(0, 0);
  lcd.print("Time:");

  if (dt.hour > 12) {
    time_hour = (dt.hour - 12);
    if (time_hour > 9) {
      lcd.setCursor(6, 0);
      lcd.print(time_hour);
    }
    else {
      lcd.setCursor(7, 0);
      lcd.print(time_hour);
    }

    lcd.setCursor(8, 0);
    lcd.print(":");

    if (dt.minute > 9) {
      lcd.setCursor(9, 0);
      lcd.print(dt.minute);
    }
    else {
      lcd.setCursor(9, 0);
      lcd.print("0");
      lcd.setCursor(10, 0);
      lcd.print(dt.minute);
    }
    if (dt.hour != 12) {
      lcd.setCursor(12, 0);
      lcd.print("PM\n");
      time_AM_PM = "PM\n";
    }
    else {
      lcd.setCursor(12, 0);
      lcd.print("AM\n");
      time_AM_PM = "AM\n";
    }
  }
  else {
    time_hour = dt.hour;
    if (dt.hour > 9) {
      lcd.setCursor(6, 0);
      lcd.print(dt.hour);
    }
    else {
      lcd.setCursor(7, 0);
      lcd.print(dt.hour);
    }
    lcd.setCursor(8, 0);
    lcd.print(":");
    if (dt.minute > 9) {
      lcd.setCursor(9, 0);
      lcd.print(dt.minute);
    }
    else {
      lcd.setCursor(9, 0);
      lcd.print("0");
      lcd.setCursor(10, 0);
      lcd.print(dt.minute);
    }
    if (dt.hour != 12) {
      lcd.setCursor(12, 0);
      lcd.print("AM\n");
      time_AM_PM = "AM\n";
    }
    else {
      lcd.setCursor(12, 0);
      lcd.print("PM\n");
      time_AM_PM = "PM\n";
    }
  }
  lcd.setCursor(11, 0);
  lcd.print(" ");
  lcd.setCursor(14, 0);
  lcd.print(" ");
}

Schematics

Arduino Alarm Schematic
Arduino alarm q57xoadchd

Comments

Similar projects you might like

Complete Digital Clock Including Alarm and Motion Sensor

Project tutorial by LAGSILVA

  • 24,915 views
  • 30 comments
  • 85 respects

DIY Arduino Fire Alarm System At Home

Project tutorial by Team MrModder

  • 22,352 views
  • 1 comment
  • 25 respects

Alarm System Security Put to the Test

Project tutorial by danionescu

  • 17,105 views
  • 5 comments
  • 27 respects

Backpack Alarm

Project tutorial by Tomi Chen

  • 14,198 views
  • 13 comments
  • 41 respects

Burglar Alarm

Project tutorial by Salman Faris

  • 10,668 views
  • 2 comments
  • 15 respects

A Very Compact Alarm with Card Reader and Real-Time Clock

Project tutorial by Simone Adobs

  • 9,767 views
  • 3 comments
  • 34 respects
Add projectSign up / Login