Music and Sound in CircuitPython

CircuitPython has similar capabilities to MakeCode for playing tones. CircuitPython, though, doesn't understand music so you would have to provide your own understanding of which musical notes correspond to which sound frequencies.

You can look up which musical notes correspond to which frequencies on a number of websites including this chart on Wikipedia.

Simple Tones

Adafruit provides the adafruit_circuitplayground library to provide high level access to the various parts of the board in CircuitPython.  The functions CircuitPython provides for using the speaker:

  • cpx.play_tone(frequency, duration)
  • cpx.start_tone(frequency)
  • cpx.stop_tone()
  • cpx.play_file("laugh.wav")

Most often for tones, it is known how long you might want to play the tone, for a second or so or perhaps until you manually turn it off (tones that play a long time are annoying unless you are doing it on purpose like for an alarm).

The two tone program using buttons A and B for two versions of the musical note A is:

Download: file
from adafruit_circuitplayground.express import cpx

while True:
    if cpx.button_a:
        cpx.play_tone(220, 0.5)
    elif cpx.button_b:
        cpx.play_tone(440, 0.5)

In this case we play the frequencies 220 Hz and 440Hz each for 1/2 second.

Continuous Tones

For an alarm, say your soil is too dry and you need to water or your family member tripped your alarm. We'll use a capacitive touch on Circuit Playground Express pad A1 to trigger an alarm. The Circuit Playground Express slide switch will silence and reset the alarm:

Download: file
    from adafruit_circuitplayground.express import cpx

cpx.adjust_touch_threshold(100)  # pretty sensitive

while True:
    if cpx.switch and cpx.touch_A1:
        cpx.start_tone(2000)
    if not cpx.switch:
        cpx.stop_tone()
  

Wave Files

The strength of CircuitPython lies with its ability to play sound files in the wave or WAV format. Not 100% of wave files on the Internet are compatible though. The parameters for CircuitPython playable files are:

  • A sample rate less than or equal to 22,050 hertz
  • 16-bit
  • mono / single channel

Adafruit has a guide to help you with your audio files here.

Using wave files for your project is wonderful! You have nearly unlimited sounds to sample and play on whatever command you set.

Here is a simple program that will play one wave file when button A is pressed and another if button B is pressed.

Here's the two files we're going to play:

Click to download, and save them onto your CIRCUITPY drive, alongside your code.py program file. 

Download: file
from adafruit_circuitplayground.express import cpx

while True:
    if cpx.button_a:
        cpx.play_file("rimshot.wav")
    if cpx.button_b:
        cpx.play_file("laugh.wav")

It is that easy!

If you want to try out some projects which use wave files, here are some good ones in the Adafruit Learning System:

This guide was first published on Jul 24, 2018. It was last updated on Jul 24, 2018. This page (Music and Sound in CircuitPython) was last updated on Dec 05, 2019.