It's easy to use the Audio BFF with CircuitPython, the builtin audiobusio module and the Adafruit_CircuitPython_SD module. These modules allow you to easily write Python code that lets you read from an attached SD card and play audio.
micro SD Card Prep
There are five .wav audio files available for download below in a .zip file. After downloading the .zip file, extract the contents:
- adabot.wav
- hello.wav
- interesting.wav
- uhoh.wav
- whhaatt.wav
Then, drag and drop the five .wav files onto a micro SD card that you'll use with the Audio BFF.Â
CircuitPython Microcontroller Wiring
Plug an Audio BFF into your QT Py or Xiao form factor board exactly as shown below. Here's an example of connecting a QT Py RP2040 to the BFF.
Connect the QT Py RP2040 with plug headers into the Audio BFF with socket headers. They should be plugged in with the backs of the boards facing each other.
For more information on soldering socket headers, check out this Learn Guide.
Then, plug in a speaker to the speaker input. Finally, insert the micro SD card with the audio files that you downloaded above into the micro SD card slot.
CircuitPython Usage
To use with CircuitPython, you need to first install the adafruit_sdcard library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders and files:
- /adafruit_bus_device
- adafruit_sdcard.mpy
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2023 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Demo audio player that plays random wav files from internal storage or
# SD card. Default pinout matches the Audio BFF for QT Py S2, S3 and RP2040
import os
import random
import audiocore
import board
import audiobusio
import audiomixer
import adafruit_sdcard
import storage
import digitalio
card_cs = digitalio.DigitalInOut(board.A0)
card_cs.direction = digitalio.Direction.INPUT
card_cs.pull = digitalio.Pull.UP
sdcard = None
DATA = board.A1
LRCLK = board.A2
BCLK = board.A3
audio = audiobusio.I2SOut(BCLK, LRCLK, DATA)
mixer = None
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
wave_files = []
for filename in sorted(os.listdir("/")):
filename = filename.lower()
if filename.endswith(".wav") and not filename.startswith("."):
wave_files.append(filename)
def open_audio():
n = random.choice(wave_files)
print("playing", n)
f = open(n, "rb")
w = audiocore.WaveFile(f)
return f, w
wavefile = 0
while True:
if not sdcard:
try:
sdcard = adafruit_sdcard.SDCard(board.SPI(), card_cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
print("Mounted SD card")
wave_files = ["/"+file for file in os.listdir('/') if file.endswith('.wav')]
wave_files += ["/sd/"+file for file in os.listdir('/sd') if file.endswith('.wav')]
print(wave_files)
except OSError:
pass
if not button.value:
if mixer and mixer.voice[0].playing:
print("stopping")
mixer.voice[0].stop()
if wavefile:
wavefile.close()
else:
wavefile, wave = open_audio()
mixer = audiomixer.Mixer(voice_count=1,
sample_rate=wave.sample_rate,
channel_count=wave.channel_count,
bits_per_sample=wave.bits_per_sample,
samples_signed=True)
mixer.voice[0].level = 0.1
audio.play(mixer)
mixer.voice[0].play(wave)
while not button.value:
pass
In the code, the SD card is mounted and the available .wav files are added to an array and printed to the Serial Console. Then, when you press the BOOT button, a random audio file will play from the array. If you press the BOOT button while a file is playing, the file will stop.
If you encounter any errors, such as RuntimeError: Mount point directory missing or an OSError, make sure that you have created an /sd directory on your CIRCUITPY drive.
Page last edited January 22, 2025
Text editor powered by tinymce.