Learn how you can create your very own clock tower for your living room with a Raspberry Pi and a speaker!

When I was a kid, I remember playing outside until the clock tower played a song that reminded me to go home before it got dark. Though I did not know it at the time, the Westminster Chimes are the most popular tune choice for clock towers around the globe and even some school bells in Asia.

Not everyone has a clock tower nearby, but with a Raspberry Pi and a speaker, you can recreate the song in your living room! Best of all, it actually works its magic every hour

Pi Code for the Clock

All of the files are available on my GitHub page. If you would like to type out the small Python script yourself, you do not need to download it. Also, make sure you have Raspbian downloaded on your Raspberry Pi.

Remember to download the sound files or your clock tower will not have a bell. I have included both .wav and .ogg files of both sound files on GitHub but will be using the .wav files below.

Once you have downloaded the files, drop them on the Desktop of your Pi. Here is the full script. 

pi clock tower code

As you can see, the only imports we need are time and os. Time allows us to get the real time and os lets us play the sound files. 

hour minute for clock tower

Specifically from time, we will be using time.localtime() to find the current hour and minute. We assign these two values to variables in the chime method for easy access.

The only method in the script is chime. It plays a tune every hour, with a certain number of bell tolls depending on the hour. It only runs from 7 am to 8 pm to avoid disrupting healthy sleep schedules, but those hours can be modified to suit your needs. 

setting when it doesn't work

If it is between 7 am and 8 pm, the script checks if the minute is equal to zero. Once it's done that, it converts the 24-hour time format from time.localtime() to a twelve hour format.

image6.png

Once these conditions are met, your Pi can begin tolling.

image5.png

Here, we use os.system to run omxplayer as a command with the -o parameter (output location) set to local. 

Though it is not required, I had some issues when my Pi decided to play the sound files through the HDMI monitor it was hooked up to instead of the speaker I had bought for this purpose. o- local ensures audio goes through the aux port, while -o hdmi does the same but for HDMI. –vol is by default 0, so I set it to 900 after trial and error. 

The main tune is played once, and then the bell toll file is played a number of times corresponding to the hour. 

main loop for chime

Lastly, the main loop runs chime until the minute is zero. When the minute is zero, the tune plays, the bell tolls, and the main loop is put to sleep a full minute to make sure the tune plays only once per hour. That last line is necessary because chime takes about 45 seconds to run from start to finish. Without it, the tune would play twice per hour instead of only once.

Once you have everything ready, simply open it up with Thonny or whatever Python IDE you use and run the script. Feel free to comment out the “if(minute == 0):” if you want to test the bell.

Adjustments You Can Make to Your Clock

There are a couple of improvements you, the reader, can make to this project. For example, you might consider creating another method to play a tune every 15 minutes as well as every hour. Feel free to repurpose chime or write your own method from scratch. Another potential improvement is making this script part of a larger project.

If you have any questions, feel free to comment below or create an issue on Github.

Eduardo Pecina
Polyglot Programming Pianist :P       Twitter @RealMitomon    GitHub: mitomon 

Categories