April 24, 2025

Complete Guide to Building a Arduino Clock and Alarm System

One of the most popular and practical projects in the realm of Arduino is the clock and alarm system. It’s a versatile project that not only displays the current time and date but also includes an alarm feature, making it a functional device for daily use. Whether you’re a beginner looking to get started with microcontroller programming or an enthusiast aiming to refine your skills, this project is both educational and rewarding.

In this comprehensive guide, I will walk you through the steps to create your own clock and alarm system using an Arduino board, a Real-Time Clock (RTC) module, an LCD display, and buttons. With this system, you’ll gain hands-on experience in programming, circuit design, and interfacing various electronic components.

Required Materials

To build this project, you will need the following materials:

  • Arduino UNO board
  • 16×2 LCD display
  • DS1307 RTC module
  • Push buttons (Set/Mode, Increment, Decrement)
  • Buzzer
  • Jumper wires
  • Breadboard

Daigram

COD

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

// LCD pins
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// RTC setup
RTC_DS1307 rtc;

// Button pins
const int setButton = 8;   // Set/Mode button
const int incButton = 9;   // Increment button
const int decButton = 10;  // Decrement button
const int buzzerPin = 11;  // Buzzer pin

// Variables
int mode = 0; // 0: Normal, 1: Set time, 2: Set date, 3: Set alarm, 4: Set AM/PM
int hour = 12, minute = 0, second = 0, day = 1, month = 1, year = 2024;
int alarmHour = 7, alarmMinute = 0;
bool isPM = false;         // AM/PM toggle
bool alarmEnabled = false; // Alarm status

void setup() {
  pinMode(setButton, INPUT_PULLUP);
  pinMode(incButton, INPUT_PULLUP);
  pinMode(decButton, INPUT_PULLUP);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(buzzerPin, LOW);

  lcd.begin(16, 2);
  lcd.print("Initializing...");
  delay(2000);
  lcd.clear();

  if (!rtc.begin()) {
    lcd.print("RTC not found!");
    while (1);
  }

  if (!rtc.isrunning()) {
    rtc.adjust(DateTime(2024, 12, 6, 12, 30, 0)); // Default time
  }
}

void loop() {
  static unsigned long pressTime = 0;
  static bool buttonPressed = false;

  // Handle Set/Mode button
  if (digitalRead(setButton) == LOW) {
    if (!buttonPressed) {
      pressTime = millis();
      buttonPressed = true;
    } else if (millis() - pressTime > 3000) { // Long press
      mode = 1; // Enter Set Time mode
      buttonPressed = false;
    }
  } else if (buttonPressed && millis() - pressTime < 3000) { // Short press
    mode = (mode + 1) % 5; // Cycle through modes
    buttonPressed = false;
  }

  // Execute modes
  if (mode == 0) {
    displayTimeAndDate();
    checkAlarm();
  } else if (mode == 1) {
    setTime();
  } else if (mode == 2) {
    setDate();
  } else if (mode == 3) {
    setAlarm();
  } else if (mode == 4) {
    setAMPM();
  }
}

void displayTimeAndDate() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(formatTwoDigits(now.hour()));
  lcd.print(":");
  lcd.print(formatTwoDigits(now.minute()));
  lcd.print(isPM ? " PM" : " AM");

  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(formatTwoDigits(now.day()));
  lcd.print("/");
  lcd.print(formatTwoDigits(now.month()));
  lcd.print("/");
  lcd.print(now.year());
  delay(1000);
}

void setTime() {
  lcd.setCursor(0, 0);
  lcd.print("Set Time:");
  lcd.setCursor(0, 1);
  lcd.print(formatTwoDigits(hour));
  lcd.print(":");
  lcd.print(formatTwoDigits(minute));
  lcd.print(isPM ? " PM" : " AM");

  if (digitalRead(incButton) == LOW) {
    hour = (hour % 12) + 1; // Increment hour (1-12)
    delay(300);
  } else if (digitalRead(decButton) == LOW) {
    hour = (hour - 1 + 12) % 12; // Decrement hour
    delay(300);
  } else if (digitalRead(setButton) == LOW) {
    rtc.adjust(DateTime(year, month, day, hour + (isPM ? 12 : 0), minute, second));
    mode = 2; // Move to Set Date
    delay(300);
  }
}

void setDate() {
  lcd.setCursor(0, 0);
  lcd.print("Set Date:");
  lcd.setCursor(0, 1);
  lcd.print(formatTwoDigits(day));
  lcd.print("/");
  lcd.print(formatTwoDigits(month));
  lcd.print("/");
  lcd.print(year);

  if (digitalRead(incButton) == LOW) {
    day = (day % 31) + 1; // Increment day
    delay(300);
  } else if (digitalRead(decButton) == LOW) {
    day = (day - 1 + 31) % 31; // Decrement day
    delay(300);
  } else if (digitalRead(setButton) == LOW) {
    mode = 3; // Move to Set Alarm
    delay(300);
  }
}

void setAlarm() {
  lcd.setCursor(0, 0);
  lcd.print("Set Alarm:");
  lcd.setCursor(0, 1);
  lcd.print(formatTwoDigits(alarmHour));
  lcd.print(":");
  lcd.print(formatTwoDigits(alarmMinute));
  lcd.print(alarmEnabled ? " ON" : " OFF");

  if (digitalRead(incButton) == LOW) {
    alarmHour = (alarmHour % 12) + 1; // Increment alarm hour
    delay(300);
  } else if (digitalRead(decButton) == LOW) {
    alarmMinute = (alarmMinute + 1) % 60; // Increment alarm minute
    delay(300);
  } else if (digitalRead(setButton) == LOW) {
    alarmEnabled = !alarmEnabled; // Toggle alarm
    mode = 4; // Move to Set AM/PM
    delay(300);
  }
}

void setAMPM() {
  lcd.setCursor(0, 0);
  lcd.print("Set AM/PM:");
  lcd.setCursor(0, 1);
  lcd.print(isPM ? "PM" : "AM");

  if (digitalRead(incButton) == LOW || digitalRead(decButton) == LOW) {
    isPM = !isPM; // Toggle AM/PM
    delay(300);
  } else if (digitalRead(setButton) == LOW) {
    mode = 0; // Return to Normal Mode
    delay(300);
  }
}

void checkAlarm() {
  DateTime now = rtc.now();
  int currentHour = now.hour() % 12;
  bool currentPM = now.hour() >= 12;

  if (alarmEnabled && currentHour == alarmHour && now.minute() == alarmMinute && currentPM == isPM) {
    digitalWrite(buzzerPin, HIGH);
    delay(5000); // Alarm duration
    digitalWrite(buzzerPin, LOW);
  }
}

String formatTwoDigits(int number) {
  return number < 10 ? "0" + String(number) : String(number);
}

Library Installation

Installing the required libraries in the Arduino IDE is essential. For this project, you need to install the following libraries:

  • Wire.h: For I2C communication.
  • RTClib.h: To communicate with the RTC module.
  • LiquidCrystal.h: For controlling the LCD display.

To install these libraries, go to Sketch > Include Library > Manage Libraries in the Arduino IDE, search for them, and install.

Code Analysis

Below is the full code with explanations of each section:

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

// LCD pins
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// RTC setup
RTC_DS1307 rtc;

// Button pins
const int setButton = 8;   // Set/Mode button
const int incButton = 9;   // Increment button
const int decButton = 10;  // Decrement button
const int buzzerPin = 11;  // Buzzer pin

// Variables
int mode = 0; // 0: Normal, 1: Set time, 2: Set date, 3: Set alarm, 4: Set AM/PM
int hour = 12, minute = 0, second = 0, day = 1, month = 1, year = 2024;
int alarmHour = 7, alarmMinute = 0;
bool isPM = false;         // AM/PM toggle
bool alarmEnabled = false; // Alarm status

Explanation of the Main Parts of the Code

  • RTC_DS1307 rtc;: Connects the RTC module to the Arduino.
  • LiquidCrystal lcd(rs, en, d4, d5, d6, d7);: Initializes the LCD display.
  • mode variable tracks different modes (setting time, setting date, setting alarm, etc.).
  • The buttons are configured as INPUT_PULLUP to read input easily.

displayTimeAndDate() Function

This function reads the current time and date from the RTC module and displays it on the LCD.

void displayTimeAndDate() {
  DateTime now = rtc.now();
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(formatTwoDigits(now.hour()));
  lcd.print(":");
  lcd.print(formatTwoDigits(now.minute()));
  lcd.print(isPM ? " PM" : " AM");

  lcd.setCursor(0, 1);
  lcd.print("Date: ");
  lcd.print(formatTwoDigits(now.day()));
  lcd.print("/");
  lcd.print(formatTwoDigits(now.month()));
  lcd.print("/");
  lcd.print(now.year());
  delay(1000);
}

setTime(), setDate(), setAlarm(), setAMPM() Functions

These functions help the user set different parameters like time, date, and alarm. Each function operates in a specific mode and takes user input.

checkAlarm() Function

This function compares the current time with the alarm time and triggers the buzzer if they match.

How to Use the Clock and Alarm System Project

Creating a clock and alarm system with Arduino can be incredibly useful and educational. Once you have completed the hardware assembly and uploaded the code, it’s time to understand how to use and navigate the system effectively. Here’s a step-by-step guide on how to interact with the system and make the most of its features:

Initial Setup

  1. Power On: Connect the Arduino board to a power source (e.g., a USB cable or external adapter). The LCD should display “Initializing…” for a few seconds before switching to the normal display mode.
  2. Date and Time Adjustment: The system starts in Normal Mode, displaying the current time and date from the RTC module. If this is your first time using the project, make sure the RTC module has the correct date and time set (as done in the setup() function of the code).

Navigating the Modes

The system operates in different modes, which can be navigated using the Set/Mode button. Here’s how to cycle through the modes:

  • Normal Mode (mode = 0): Displays the current time and date on the LCD.
  • Set Time Mode (mode = 1): Allows you to adjust the current time.
  • Set Date Mode (mode = 2): Lets you modify the current date.
  • Set Alarm Mode (mode = 3): Enables you to set or modify the alarm time.
  • Set AM/PM Mode (mode = 4): Allows you to toggle between AM and PM for time settings.

How to Change Modes

  • Short Press: Press the Set/Mode button briefly to cycle through each mode in order. Each press will change the mode to the next one (0 → 1 → 2 → 3 → 4 → 0 → …).
  • Long Press: Hold the Set/Mode button for more than 3 seconds to enter Set Time Mode directly from any mode.

Setting the Time

  1. Enter Set Time Mode: The LCD will display “Set Time:” and show the current hour and minute.
  2. Increment/Decrement Time: Use the Increment button to increase the hour and the Decrement button to decrease it. The hour will loop between 1 and 12.
  3. Set Time: Press the Set/Mode button to confirm the time. The system will move to Set Date Mode for the next step.

Setting the Date

  1. Enter Set Date Mode: The LCD will display “Set Date:” and show the current day, month, and year.
  2. Increment/Decrement Date: Use the Increment button to increase the day and the Decrement button to decrease it. The day will loop between 1 and 31.
  3. Set Date: Press the Set/Mode button to save the date and move to Set Alarm Mode.

Setting the Alarm

  1. Enter Set Alarm Mode: The LCD will display “Set Alarm:” along with the current alarm hour and minute. It will also indicate whether the alarm is ON or OFF.
  2. Increment/Decrement Alarm Time: Use the Increment button to increase the alarm hour and the Decrement button to increase the alarm minute.
  3. Toggle Alarm: Press the Set/Mode button to enable or disable the alarm. When the alarm is set to ON, the system will activate the buzzer at the designated alarm time.
  4. Move to AM/PM Setting: After setting the alarm, press the Set/Mode button to move to Set AM/PM Mode.

Setting AM/PM

  1. Enter Set AM/PM Mode: The LCD will display “Set AM/PM:” and show whether the time is currently set to AM or PM.
  2. Toggle AM/PM: Use the Increment or Decrement button to switch between AM and PM.
  3. Confirm and Exit: Press the Set/Mode button to return to Normal Mode.

Normal Operation

Once set up, the system will display the current time and date on the LCD. The alarm will activate based on the set time and AM/PM. When the current time matches the alarm time, the buzzer will turn on and sound for 5 seconds, after which it will turn off.

Troubleshooting Tips

  • RTC Not Found: If the LCD displays “RTC not found!” during initialization, make sure the RTC module is properly connected and powered.
  • Incorrect Time/Date: If the time or date is not correct, manually adjust it using the steps described above.
  • Alarm Not Triggering: Double-check the alarm time and ensure it is set to ON. Also, make sure the current time matches the alarm conditions (e.g., AM/PM).

Final Thoughts

This clock and alarm system project is a great way to explore the use of Arduino, an RTC module, and LCD display. By following this guide, you can set up and use the system effectively for daily scheduling and notifications. This project can be expanded upon in the future to include features such as multiple alarms, different alarm sounds, or automatic daylight saving time adjustments.

How to Use the Project

This clock and alarm system helps users stay on schedule and get reminded of important events. It is suitable for use in schools, offices, or at home.

Benefits

  • Self-Sustaining: Once set, the system runs automatically until changed.
  • Smart Features: Includes AM/PM toggle, alarm settings, etc.
  • Easy Installation: Simple to assemble and inexpensive.

Conclusion

I hope this guide to building a clock and alarm system using Arduino has been helpful. It’s a fun project that will enhance your learning and practical skills.

About The Author

1 thought on “Complete Guide to Building an Arduino Clock and Alarm System

Leave a Reply

Your email address will not be published. Required fields are marked *