Building the Raspberry Pest

4 minute read

Many years ago, I purchased an Annoy-a-Tron from This little gadget could be surreptitiously mounted somewhere in the office, chirping randomly like a cricket. The intended side effect was driving your co-workers crazy. Over time, I started to enjoy having it in the office, especially in the early mornings or late nights working as a Sys Admin. This little cricket was my only company at times, and I eventually started thinking of it as a pet (or I was slowly going mad).

Sadly, it finally died and I debated whether to replace it. After some thought, I figured this would be a perfect candidate for one of the many Raspberry Pi’s hanging around my office doing various on-line tasks. I should be able to connect a piezo speaker to one of the GPIO pins and use a script make it to chirp randomly. This is where things get a little interesting. I found many examples for connecting a piezo buzzer via GPIO to play sounds from the Raspberry Pi, but most used custom Java or C++ code. I decided to draft this post documenting all the steps I followed, allowing anyone with basic experience to make this simple project. In the end, it was my goal to run this on one of my pre-existing Raspberry Pi servers using Raspbian Lite (with no GUI and the minimum additional packages).

During my research, the best resource I found on-line was this post from Adafruit which documents the steps necessary to output audio using the GPIO pins. That said, the document assumes you will be connecting full sized stereo speakers, so it was much more complex than needed for my purposes. After further digging, I worked out the steps below to enable mono audio out using a piezo buzzer connected to GPIO pin 18 on the Raspberry Pi.

  1. Firstly, login to the Raspberry Pi and use the Configuration Tool (sudo raspi-config) to force audio out via the 3.5mm jack.

    • Select 7 Advanced Options
    • Select A4 Audio
    • Select 1 Force 3.5mm (‘headphone’) jack
    • Select Finish
  2. Next, update /boot/config.txt to enable mono PWM audio on GPIO18 at boot. To do this, run the command sudo nano /boot/config.txt and add the following line to the file.


  3. To exit the editor and save the changes, press Ctrl + X , then Y. These changes will take affect after the next boot.

  4. Lastly, to play sounds from the command line (we’re running Raspbian Lite with no GUI), you will need to install AlsaPlayer and utilities. Run the command sudo apt-get install alsa-utils to install the necessary packages.

Once these changes are completed, the Raspberry Pi is now ready to play sound from GPIO pin 18. I had originally thought of running a Python script to generate the cricket sound manually, but found it much simpler (and more flexible) to play a wav file from the command-line. This way, if I got tired of the cricket, I could switch to a different sound clip in the future.

Of course, to play sounds from the Pi, I needed to connect a piezo buzzer between GPIO pin 18 and ground. Luckily, I had one in my parts bin similar to this model from Adafruit. I connected the buzzer using a 2-pin female Dupont crimp-on connector similar to these found on eBay. For this project, I’m using a Raspberry Pi Model B, so the speaker is physically connected to pins 12 (gpio18) and 14 (Gnd). In the photo you can see the Dupont connector on the GPIO pins, although the speaker is hidden.

Raspberry Pi B with Oak case

The case shown is based on a design I found at Instructables which allows the speaker to be mounted inconspicuously underneath the top panel. I have versions of this case available made from Honey Oak with all the necessary hardware for mounting most versions of the Raspberry Pi (except the Zero). Cost is $20, shipping included. Contact me using the Contact page if interested.

Once I had the hardware done and tested, I built a simple bash script which randomly plays a sound clip using aplayer. To do this, the script checks if $RANDOM (the bash function which provides a random number from 0 to 32768) is less than or equal (-le) to 8192. This works out to around a 1 in 4 chance of playing the sound. The odds can be adjusted by raising or lowering the number specified. For example, if lowered to 4096, this reduces the chance to 1 in 8 of playing the sound. Lastly, the script resets the volume on each run to ensure the sound is played at the preferred level.


dirname=`dirname "$0"`

# Manually set audio out on GPIO pin 18 (not needed if using dtoverlay in /boot/config.txt
# raspi-gpio set 18 a5

# Set volume to 75%
amixer -M set PCM 75% > /dev/null

# Play sound randomly
if [ $RANDOM -le 8192 ]; then
	aplay $dirname/cricket.wav

To execute the script regularly, I placed it and the sound clip in the same folder in my home directory (/home/pi/cricket). I then added an entry in my crontab file so it will be run once per minute. This can be done by running crontab -e and adding the entry shown below.

# m h  dom mon dow   command
* * * * * /home/pi/cricket/

I’m happy to have my little cricket back in the office chirping away on a regular basis. As always, I hope you found this post useful, or at least interesting. To contact me or provide feedback on this post, please use the Contact page, or message me on Twitter.