Add Sounds to Your Raspberry Pi Device

Making my IBM Clock Sound off

Steve Harding
5 min readJan 12, 2022

Recently I wrote about restoring an old IBM Wall Clock to function using a Raspberry Pi driver. I have enjoyed restoring the clock, building the driver, programming it with Python, and writing about those activities. The clock now hangs on my wall keeping time perfectly. I am pleased by it, but there is one more thing I need to do. I need to make it into a Cuckoo Clock, of course!

Well, it’s not a classic Cuckoo Clock, because that would entail adding an animatronic bird that emerges from doors someplace below the clock to announce the time. However, since my IBM Clock is from the Thomas J Watson Sr era, I believe it deserves some dignity. No twitching bird for this clock! It was enough to add audio that announces the hour by cuckooing.

Hardware and System Software Selection

From an old, abandoned Pi project, I have an audio card that mounts directly to the pins of a Raspberry Pi. The device I have is Adafruit’s 3 watt stereo bonnet. I attached two of Adafruit’s 8 ohm, 3 inch speakers to complete my audio system. The total cost of the audio card plus two speakers is under $20, which is remarkable in my opinion.

The Speaker Bonnet shown plugged onto the Raspberry Pi pins. Notice screws on right and left terminal blocks. The speaker wires are attached to these blocks.

I should mention that I did this project on a Raspberry Pi 3B rev 1.2 running Raspbian Buster, which includes Python 3.7.3. You should be able to use other Pi hardware and Raspbian versions. Note that you can check your Raspbian version with the command cat /etc/os-release and your hardware model can be seen on the silkscreen on the board or by issuing the command cat /proc/device-tree/model

Adafruit provides excellent support for installing and configuring this hardware. Note that there is some soldering needed to attach the speaker terminal blocks. If you have the equipment, this is easy. The instructions also cover soldering a 2x20 header to the board. The instructions were written years ago, and the cards from Adafruit now ship with the header pre-assembled, so you will not need to solder the header onto the board.

After plugging the bonnet (Adafruit calls the card a bonnet instead of a hat because of its small size) onto the pins of your Pi, you attach the speakers to their terminal blocks. Again, some soldering of wires to the speakers may be needed. With everything hooked up, configuration was easy using the Fast Install instructions. Just for fun I also configured a different Pi using the Detailed Install instructions. Manual configuration is pretty involved and is not for everyone…Let the fast installer do the work for you.

I will not discuss the Adafruit audio hardware any further here. Read the Adafruit documentation linked to earlier for a more complete discussion of what it is and how it works. Other hardware for playing sound using the Raspberry Pi is certainly possible. You could even use the headphone jack if you enable audio output with raspi-config. Because of poor quality sound that was not a good solution for me. The Adafruit solution gives good quality playback at sufficient volume to meet my needs.

System Software to Play Cuckoo Sounds

Playing wav files on the Pi can be done using a command line utility named aplay. This utility is included in Raspbian Buster and is likely also included in earlier versions. You just pass the path-name of the wav file to play and it plays for you. My cuckoo sounds are wav files I found on the web, and my program uses aplay to play them. More about using Python to play the sounds later…

Other sources of audio content are mp3 files on disk and streaming content from the web. These types of playback can be done using a utility named mpg123. This utility is not part of the base Raspbian code so you need to add it. Issue the command sudo apt-get install mpg123 to install it. I am not going to discuss playing internet streams. To play a mp3 file that is stored locally is done how you might expect, just invoke mpg123 and pass the path-name of the mp3 file on the command line.

Program Design and Implementation

With the sound card and speakers added to the Raspberry Pi, and with a cuckoo wav file downloaded, I proceeded to design and implement a Python program to add cuckooing to the clock.

Requirements

  1. At the top of each hour the computer should emit n cuckoo sounds, where n corresponds to the number pointed to by the hour hand of the clock.
  2. The program that makes the sounds happen should be aware of the day of the week and the 24-hour time of day. This is to allow setting a weekend versus weekday schedule for the hours of when the cuckoo is permitted to sound off. For example, on the weekend the cuckoo sounds are only allowed from 8:00 am until 11:00 pm. On weekdays, when someone is working from home, cuckoo sounds are only allowed from 5:00 pm until 11:00 pm.
  3. The system clock of the Raspberry Pi is the basis for time and date. As it is synchronized with network time servers at boot time, it is considered a reliable time source.
  4. A suitable cuckoo wav sound file is needed.

Implementation

A program written in Python is used to monitor the day of the week and time of day. Based on the time and day, the system aplay utility will be invoked n times by the program, passing the cuckoo.wav file so it is played.

The program is started automatically at Raspberry Pi boot time by use of the crontab service. I use the following line in crontab to perform this function:

@reboot cd /home/pi/mycode/clockchimes && nohup /usr/bin/python3 chimer.py > /dev/null 2>&1 &

Note that when reboot occurs, this changes to the directory where my Python code is saved and then uses the nohup (‘no hangup’) service to run chimer.py using Python 3. Stdout and Stderr are both redirected to the /dev/null device. It’s bizarre and probably could be simplified, but it works.

The Python code for this program has been tested using Python 3.7.3 as found in Raspbian Buster. The code is well commented (in my humble opinion) and will not be discussed directly in this article. See my code on Github (chimer.py) and read its comments to learn how it fulfills the requirements.

Thanks for reading!

--

--

Steve Harding

I am a retired IBM software developer. I have a passion for making creative technology projects. I enjoy writing also. Medium is a good outlet for me!