alarm-clock-using-python

Alarm Clock Using Python

In this article, we are going to learn how to make an Alarm Clock using a simple python script.

Introduction:

As the title suggests, our task here is to write a python script that creates an alarm clock. For this task, I will be using the DateTime module in Python to create an alarm clock and the sound library in Python to play the alarm sound.

The DateTime module comes preinstalled in the Python programming language so you can easily import it in your program. The playsound library can be easily installed by using a pip command.

> pip install playsound.

I hope you will be able to install it in your systems, now let’s see how to write a program to create an alarm with Python.

Project Prerequisites:

  • The DateTime module is a built-in module in the Python programming language, so we don’t need to install it separately.
  • You should have prior knowledge about the DateTime module. To know about the DateTime refer to this link – https://docs.python.org/3/library/datetime.html
  • We should install the playsound module as it isn’t a built-in module. We’ll use the cmd to install the playsound module.
  • To know about the playsound module refer to this link – https://pypi.org/project/playsound/
  • You also need a sound for the alarm. So we have to download it before a save it in the same folder as the program.

Getting Started:

  • First, we’ll create a folder for this project named “Alarm_Clock”.

After, create a file named “alarmclock.py”

See also  Tic Tac Toe Using HTML, CSS & JavaScript.

Code Implementation:

As we are using the DateTime and playsounds modules for this project we’ll first import them using the import keyword.

from datetime import datetime  

from playsound import playsound

from datetime import datetime   
from playsound import playsound

After that, we’ll take input the time at which we want to set the alarm in the format

HH:MM:SS:(AM/PM).

alarm_time = input(“Enter the time of alarm to be set:HH:MM:SS:(AM/PM)\n”)

alarm_time = input("Enter the time of alarm to be set:HH:MM:SS:(AM/PM)\n")

Then we’ll make different variables for hours, minutes, seconds, and period i.e. AM/PM.

We use the upper() method so that even if the user enters a lowercase it will be converted to an uppercase.

alarm_hour = alarm_time[0:2]

alarm_minute = alarm_time[3:5]

alarm_seconds = alarm_time[6:8]

alarm_period = alarm_time[9:11].upper()

alarm_hour = alarm_time[0:2]
alarm_minute = alarm_time[3:5]
alarm_seconds = alarm_time[6:8]
alarm_period = alarm_time[9:11].upper()

We print a prompt to know that the alarm is being set.

print(“Setting up alarm..”)

print("Setting up alarm..")

After that, we will use a while loop to continuously check the time with the alarm time that we have entered.

If the times match, then we first print “Wake Up!” and then play the sound that we have stored in the same folder as the program using the playsound method.

while True:

    now = datetime.now()

    current_hour = now.strftime(“%I”)

    current_minute = now.strftime(“%M”)

    current_seconds = now.strftime(“%S”)

    current_period = now.strftime(“%p”)

    if(alarm_period == current_period):

        if(alarm_hour == current_hour):

            if(alarm_minute == current_minute):

                if(alarm_seconds == current_seconds):

                    print(“Wake Up!”)

See also  Learn python for Beginners - Quick start

                    playsound(‘audio.mp3’)

                    break

while True:
    now = datetime.now()
    current_hour = now.strftime("%I")
    current_minute = now.strftime("%M")
    current_seconds = now.strftime("%S")
    current_period = now.strftime("%p")
    if(alarm_period == current_period):
        if(alarm_hour == current_hour):
            if(alarm_minute == current_minute):
                if(alarm_seconds == current_seconds):
                    print("Wake Up!")
                    playsound('audio.mp3')
                    break

That’s it for the code.

We have now completed the code implementation part.

Now, let’s check the complete source code.

Source Code:

Here is the complete source of the project.

from datetime import datetime   
from playsound import playsound
alarm_time = input("Enter the time of alarm to be set:HH:MM:SS:(AM/PM)\n")
alarm_hour = alarm_time[0:2]
alarm_minute = alarm_time[3:5]
alarm_seconds = alarm_time[6:8]
alarm_period = alarm_time[9:11].upper()
print("Setting up alarm..")
while True:
    now = datetime.now()
    current_hour = now.strftime("%I")
    current_minute = now.strftime("%M")
    current_seconds = now.strftime("%S")
    current_period = now.strftime("%p")
    if(alarm_period == current_period):
        if(alarm_hour == current_hour):
            if(alarm_minute == current_minute):
                if(alarm_seconds == current_seconds):
                    print("Wake Up!")
                    playsound('audio.mp3')
                    break

Output: 

First, we have to enter the time in the given format.

After that, a prompt will be displayed saying “Setting up alarm..”

When the time is right you will hear the sound that you set in the folder.

See also  Bank Management System in Python using Tkinter

That’s it for the project…. If you have come to this far congratulation you now know how to build an Alarm Clock using python.

Important Note:

Note that you can still improve this code by adding some more functionalities to it.

Keep trying. All the Best : )

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.