Skip to content

Basic Alarm Clock

Abstract

Build a functional alarm clock application that lets users set a specific time and receive an audio alert when that time arrives. This project demonstrates working with time modules, user input validation, and system audio playbook.

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE
  • Basic understanding of Python syntax
  • Knowledge of datetime module
  • Familiarity with loops and conditional statements

Getting Started

Create a new project

  1. Create a new project folder and name it basicAlarmClockbasicAlarmClock.
  2. Create a new file and name it basicalarmclock.pybasicalarmclock.py.
  3. Open the project folder in your favorite text editor or IDE.
  4. Copy the code below and paste it into your basicalarmclock.pybasicalarmclock.py file.

Write the code

  1. Add the following code to your basicalarmclock.pybasicalarmclock.py file.
⚙️ Basic Alarm Clock
Basic Alarm Clock
# Basic Alarm Clock
 
# Importing the libraries
import datetime
import time
import winsound
 
# Defining the alarm function
def alarm(Timing):
    altime = str(datetime.datetime.now().strptime(Timing,"%I:%M %p"))
    altime = altime[11:-3]
    print(altime)
    Horeal = altime[:2]
    Horeal = int(Horeal)
    Mireal = altime[3:5]
    Mireal = int(Mireal)
    print(f"Done, alarm is set for {Timing}")
    while True:
        if Horeal  == datetime.datetime.now().hour:
            if Mireal == datetime.datetime.now().minute:
                print("Alarm is running")
                winsound.PlaySound('abc',winsound.SND_LOOP)
            
            elif Mireal<datetime.datetime.now().minute:
                break
            
# Taking user input
print("Set a time to alarm(HH:MM AM/PM)")
Alarm_Time = input("Enter the time of alarm to be set: ")
alarm(Alarm_Time)
 
Basic Alarm Clock
# Basic Alarm Clock
 
# Importing the libraries
import datetime
import time
import winsound
 
# Defining the alarm function
def alarm(Timing):
    altime = str(datetime.datetime.now().strptime(Timing,"%I:%M %p"))
    altime = altime[11:-3]
    print(altime)
    Horeal = altime[:2]
    Horeal = int(Horeal)
    Mireal = altime[3:5]
    Mireal = int(Mireal)
    print(f"Done, alarm is set for {Timing}")
    while True:
        if Horeal  == datetime.datetime.now().hour:
            if Mireal == datetime.datetime.now().minute:
                print("Alarm is running")
                winsound.PlaySound('abc',winsound.SND_LOOP)
            
            elif Mireal<datetime.datetime.now().minute:
                break
            
# Taking user input
print("Set a time to alarm(HH:MM AM/PM)")
Alarm_Time = input("Enter the time of alarm to be set: ")
alarm(Alarm_Time)
 
  1. Save the file.
  2. Open the terminal and navigate to the project folder.
  3. Run the following command to run the application.
command
C:\Users\username\Documents\basicAlarmClock> python basicalarmclock.py
Enter the time of alarm to be set: HH.MM.SS AM/PM
Enter the time: 02:30:00 PM
Alarm is set for 02:30:00 PM
Waiting for alarm time...
*Alarm sound plays*
Wake Up! Wake Up!
command
C:\Users\username\Documents\basicAlarmClock> python basicalarmclock.py
Enter the time of alarm to be set: HH.MM.SS AM/PM
Enter the time: 02:30:00 PM
Alarm is set for 02:30:00 PM
Waiting for alarm time...
*Alarm sound plays*
Wake Up! Wake Up!

Explanation

  1. The import datetimeimport datetime statement imports the datetime module for working with dates and times.
  2. The import timeimport time statement imports the time module for adding delays and sleep functionality.
  3. The import winsoundimport winsound statement imports the Windows sound module for playing audio alerts.
  4. The def alarm(Timing):def alarm(Timing): function defines the main alarm functionality that takes the alarm time as a parameter.
  5. The while True:while True: loop continuously checks the current time against the alarm time.
  6. The datetime.datetime.now().strftime("%H:%M:%S")datetime.datetime.now().strftime("%H:%M:%S") gets the current time in HH:MM
    format.
  7. The if Now == Timing:if Now == Timing: condition checks if the current time matches the alarm time.
  8. The winsound.PlaySound()winsound.PlaySound() function plays the alarm sound when the time matches.
  9. The input()input() function prompts the user to enter the desired alarm time.
  10. The program uses 24-hour time format for setting alarms.

Next Steps

Congratulations! You have successfully created a Basic Alarm Clock in Python. Experiment with the code and see if you can modify the application. Here are a few suggestions:

  • Add multiple alarm support
  • Implement snooze functionality
  • Create GUI interface with Tkinter
  • Add custom sound file selection
  • Include alarm name/description feature
  • Add recurring alarm options (daily, weekly)
  • Implement alarm history and logs
  • Add volume control for alarms
  • Create different alarm tones

Conclusion

In this project, you learned how to create a Basic Alarm Clock in Python. You also learned how to work with time modules, user input validation, and system audio playback. You can find the source code on GitHub

Features

  • 12-Hour Time Format: Accepts AM/PM time input
  • Real-Time Monitoring: Continuously checks current time
  • Audio Alerts: Plays sound when alarm triggers
  • Automatic Termination: Stops after alarm time passes
  • Simple Interface: Command-line based interaction

Next Steps

Enhancements

  • Add multiple alarm support
  • Implement snooze functionality
  • Create GUI interface with Tkinter
  • Add custom sound file selection
  • Include alarm name/description feature
  • Add recurring alarm options (daily, weekly)
  • Implement alarm history and logs

Learning Extensions

  • Study threading for non-blocking alarms
  • Explore cross-platform audio libraries
  • Learn about system notifications
  • Practice with configuration files for settings
  • Understand process scheduling and background tasks

Educational Value

This project teaches:

  • Time Manipulation: Working with datetime objects and formatting
  • Input Validation: Parsing and validating time formats
  • System Integration: Using OS-specific sound capabilities
  • Loop Control: Implementing monitoring loops with break conditions
  • String Processing: Extracting and converting time components
  • Real-Time Programming: Creating responsive time-based applications

Perfect for understanding how time-based applications work and practicing system-level programming concepts.

Was this page helpful?

Let us know how we did