If you need to jumpstart your Christmas cheer, try this simple Raspberry Pi project to make a Christmas carol player!
Nothing spreads holiday cheer quite like a catchy Christmas song. If your home is in need of a little extra holiday cheer or you are looking for something to complement your holiday decorations, why not flex your making muscles and program your Raspberry Pi to play some Christmas songs?
In this tutorial, we will write a script to play my piano arrangements of Jingle Bells, Silent Night, and Deck the Halls. If you are not looking for ambient music (or you donât like my versions of the songs), I will also cover how to replace my songs with your own Christmas (or other) music.
Time to get festive with Raspberry Pi!
Required Hardware and Files
It's pretty easy to dive into this project. You need:
- A Raspberry Pi (I used a Raspberry Pi 3)
- A wired speaker
As usual, all files are available on my GitHub page. There are three songs to download, as well as the main script. The songs are available in both .wav and .ogg formats. Download the ones that best suit your needs. If you prefer listening to different songs, copy them onto your Pi.
Optionally, you may enjoy reading my Pi Clock Tower tutorial as it involves a similar concept and script. If you already have, you should easily understand most of the code for this project.
Now, letâs get started!
Holiday Cheer Pi Code
Once your songs are safely stored in your Pi, we can work on the code.
The Holiday Cheer Pi Python code.
It is a pretty simple script that can be molded to fit your needs. We must import the time, OS, and random packages to make our songs play reliably and randomly. Otherwise, your Pi will be playing the same songs in the same order for hours, and that can quickly become very boring.
Adjusting Time Intervals
If we skip down to the first if statement, you see that a song is set to play every five minutes. This is the first adjustable point. Simply swap that mod 5 for your preferred interval. For example, âif (minute%10 == 0)â will play a song every ten minutes instead of every five.
Using Rando for Song Selection
Moving to the next if statement, you can see that rando decides what song plays. Defined in Line 10, rando is a random integer between 1 and 3 (both inclusive).
The "3" corresponds to the number of songs in the system. For more songs, replace the 3 with the total number of songs you have after you add your own to the script.
os.system runs omxplayer as a command with the -o parameter (output location) set to local.
Note: When adding your own songs, you must replace âSong1.wavâ,â Song2.wavâ, and âSong3.wavâ with the full name of your music file. If not, omxplayer will run into an error.
Adding More Songs
If you want more than three songs to play, add them in the middle of the if statement like so:
How to add more songs to your project.
Main Loop Runs playSong()
Lastly, the main loop runs playSong() whatever number of times you set per hour (the default is every 5 minutes).
Unlike the Pi Clock tower where the Pi was put to sleep after playing the main method, every song is longer than a minute, so songs will not play twice per trigger.
Time to Spread Holiday Cheer With Your Raspberry Pi!
Once your script is ready along with all of your song files in the same location, whip out your Python IDE and run it.
If you are running the default version I wrote, you should hear an instrumental version of Jingle Bells, Silent Night, or Deck the Halls. If modified, you should be hearing one of your songs instead.
Check out the video below of my completed project.
If you have any questions let me know here or on GitHub. Also, I'd love to hear what songs you choose to add to your build!
In the mood for even MORE Christmas cheer? Check out these other projects: