How to Build an Alarm Clock Using Python

How to Build an Alarm Clock Using Python

Welcome to the Python Projects for Beginners Series πŸ™‹πŸ»β€β™‚οΈ

I'm Sai Ashish and today, we are going to build an alarm clock using Python which plays your favorite song when the time is up 🎼

How cool is that? Tick, tick, tick. Let's start building πŸš€

Through This Project, You're Going to Learn About:

1. The Playsound Module
2. The Datetime Module
3. Basics of Python: Conditional Statements, Iterative Statements & Slicing a string 
4. Implementation of Your Very Own Alarm Clock

You can access the basics of python used in this project here. Don't forget to smash the star button if it provided value to you πŸ’―

Step 1: Modules Required:

1. What is the Playsound module?

For this project, we would be using the Playsound module. Playsound is a pure Python, cross-platform, single function module with no dependencies for playing sounds. In other words, you can play a sound with just a single line of code. The Playsound module lets us play our favorite song with just one line of code!

Playsound is not available by default in the Python Standard Library and has to be installed. To do this, we will use pip, the standard package manager for Python. All you've to do is, go to the and terminal and run:

pip install playsound

You can access the official documentation of Playsound Module from here

2. What is the Datetime Module?

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.

Example: Import the datetime module and display the current date:

import datetime

x =

When we run the above code snippet, we obtain the output as:

2021-01-21 00:01:15.165663

The date contains year, month, day, hour, minute, second, and microsecond.

You can access the official documentation of the Datetime module from here to know more about it.

Source: W3Schools

Step 2: Time to Code!

The Insightful Coder: Time to Code

The first step is to import the required modules into our environment. To do that we type:

#importing the required modules
from datetime import datetime   
from playsound import playsound

Next, we need to ask the user for the time he wants to set the alarm.

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

At this stage, we have obtained a point of time in the format of HH:MM: SS AM/PM. To compare this format of time with the system time in our computer, we need to extract the hours, minutes, seconds, and period from the above string. We achieve this using string slicing.

String slicing helps you obtain a part of a word/sentence. Let's assume the example:

s = "The Insightful Coder"

Taking the first index as 0, slicing prints the characters from position 4 to position 11 (not included).


Mini- Challenge🧠

Do you think you can now obtain hours, minutes and seconds from the string HH:MM:SS? Try here.

Answer Reveal πŸ”“

#obtaining the hour

#obtaining the minute

#obtaining the second

#obtaining the period
alarm_period = alarm_time[9:11]

Kudos to all the insightful coders who dared to try πŸ₯³

Next, let's inform the user his alarm was set.

print("Alarm Set!")

All we have to do now is compare and find when the current time matches the alarm time set by the user. To check every instant of time, we will make use of the While loop.

while True:

Inside this loop, we would first acquire the current time and store it inside a variable.

now =

The current time obtained is not in the string format. To obtain a string from a datetime object we make use of the strftime() method. Obtaining the hour, minute and seconds of current time:

current_hour = now.strftime("%I")
current_minute = now.strftime("%M")
current_seconds = now.strftime("%S")
current_period = now.strftime("%p")

%I, %M, %S etc. are format codes. The strftime() method takes one or more format codes as an argument and returns a formatted string based on it. Read more about it here.

We have both the strings now. Let's compare it:

if(alarm_period==current_period and alarm_hour==current_hour and alarm_minute==current_minute and alarm_seconds==current_seconds):

Once, the time is up, we print a message saying, "Wake up!" and play a song. Let's see it in action.

print("Wake Up!")

Finally, we break out of the loop using the break statement.


The Final Source Code Could Be Seen Below:

Source code of building an alarm clock using python

That's it, folks! You now have built your very own alarm clock using just 18 lines of code πŸ‘©πŸ»β€πŸ’» You can download the source code to this project here. Do hit the twinkle star, if this article provided value to you πŸ”₯

And while you're at it, consider giving this blog the maximum love you can and I promise to give you such value bombs every week πŸ’£ Until then, take care πŸ™‹πŸ»β€β™‚οΈ

Bonus Insights by The Insightful Coder :

  • Interested in Building Your Own Artificial Intelligence Projects using Python?: Check out the Python AI Series🧠

  • Wanna Discover Some Valuable Tech-Hacks πŸ› ?: Check out the Tech-Hacks for Everybody Series😎

  • Wanna Interact With Me πŸ™‹πŸ»β€β™‚οΈ? Connect With Me on Your Favourite Platform Here 🀝🏻

  • Find and Download All My Project Source Codes at My Github Repository 🎁

Did you find this article valuable?

Support Sai Ashish Konchada by becoming a sponsor. Any amount is appreciated!