CircuitPython

Currently CircuitPython is best for opening and using sound files. MakeCode does not handle files and Arduino is a steep mountain full of deep chasms. 

Are you new to using CircuitPython? No worries, there is a full getting started guide here.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. You can learn about Mu and its installation in this tutorial.

Simple Sound Triggering

For this example we will use a Circuit Playground Express board as it has two buttons onboard. 

The code waits for either Button A or Button B to be pressed and plays one of two wav files, 01.wav or 02.wav.

The sound will come out on the speaker on the Circuit Playground Express board. If you'd like to hook up headphones or powered speakers, make the following connections:

Download: file
from adafruit_circuitplayground.express import cpx
 
while True:
    if cpx.button_a:
        cpx.play_file("01.wav")
    if cpx.button_b:
        cpx.play_file("02.wav")

Copy this file to the CIRCUITPY drive as code.py.

Download these two wav files below and place them on the CIRCUITPY drive in the main (root) directory.

Here is what it sounds like with two demonstration voice files:

For other Express boards besides the Circuit Playground Express, here is a more generalized code script. This is shown using the Metro M0 Express although the code still runs on the Circuit Playground Express by uncommenting two lines for pin definitions. Use the save WAV files above for testing.

The analog output A0 goes to the jack of headphones or amplified speakers.

Download: file
# Generalized CircuitPython audio play file

import time
import board
import audioio
from digitalio import DigitalInOut, Direction, Pull

# D1 = board.BUTTON_A  # uncomment these if using
# D2 = board.BUTTON_B  #  a Circuit Playground Express

# Button A
button_a = DigitalInOut(D1)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
# Button B
button_b = DigitalInOut(D2)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN

# Audio playback object and helper to play a full file
aout = audioio.AudioOut(board.A0)

# Play a wave file 
def play_file(wavfile):
    print("Playing", wavfile)
    with open(wavfile, "rb") as f:
        wav = audioio.WaveFile(f)
        aout.play(wav)
        while aout.playing:
            pass

while True:
    if button_a.value:
        play_file("01.wav")
    if button_b.value:
        play_file("02.wav")
    time.sleep(0.01)

Expanding the Number of Words You Can Trigger

At this point, you can consider triggering sounds a number of different ways. The Metro M0 Express has 13 digital pins and 6 analog pins which could be used as wired above.

Capacitive touch is a very popular method of getting multiple "button" triggers and making sounds. On the Circuit Playground Express, capacitive touch is available on all data pads except A0.

The Adafruit tutorial FruitBox Sequencer: Musically Delicious Step Pattern Generator uses capacitive touch on the Circuit Playground Express to trigger different musical sounds. You can use the same exact CircuitPython code but replace the musical wav files with your word recordings. The words could be "Peach", "Apple", "Orange", "Mango", "Lemon", "Lime", and "Tangerine". Tapping a finger on the fruit triggers the corresponding wav file containing what that fruit is named. Great demonstration for what an inexpensive microcontroller can do!

This guide was first published on Dec 19, 2018. It was last updated on Dec 19, 2018. This page (CircuitPython) was last updated on Dec 05, 2019.