CircuitPython supports sending I2S audio signals using the builtin audiobusio
module, making it simple to use the I2S BFF with your QT Py. It's easy to use CircuitPython to play different types of audio using I2S, including tones and WAV files.
This page uses a QT Py RP2040 as the microcontroller board. These demos will work with any QT Py version, as long as it has the audiobusio
module builtin to CircuitPython for that board. Visit circuitpython.org/downloads and search for the QT Py you wish to use. Click on it, and you'll find a list of built-in modules. The list for the QT Py RP2040 is indicated by the magenta rectangle. audiobusio
is highlighted.
QT Py to BFF
Connect a QT Py RP2040 to the I2S BFF by soldering on a pair of pin headers to one, and a pair of socket headers to the other.
In this setup, the pin headers are on the QT Py on the top of the image, and the socket headers are on the I2S BFF on the bottom of the image. They should be plugged in with the backs of the boards facing each other. The I2S BFF has on the back, a USB label with an arrow, to indicate which end of the board should be aligned with the USB connector on the QT Py.
I2S BFF to Speaker
Connect a speaker to your I2S BFF as shown below.
Plug the PicoBlade-compatible connector on your speaker into the PicoBlade-compatible socket on your I2S BFF.
The socket is keyed along with the connector to ensure avoiding plugging it in backwards.
Speaker to Connector Cable (Optional)
If your speaker comes with bare wires, you'll want to pick up this cable, and solder it to your speaker.
Solder the cable to the speaker by connecting the wires on the cable to the same color wires on the speaker.
Tone Playback
Click the Download Project Bundle button below to download the code.py file in a zip file. Extract the contents of the zip, and copy the code.py file to your CIRCUITPY drive.
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """ CircuitPython I2S Tone playback example. """ import time import array import math import audiocore import board import audiobusio TONE_VOLUME = 0.1 # Increase this to increase the volume of the tone. FREQUENCY = 440 # Set this to the Hz of the tone you want to generate. audio = audiobusio.I2SOut(board.A2, board.A1, board.A0) length = 8000 // FREQUENCY sine_wave = array.array("h", [0] * length) for i in range(length): sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * TONE_VOLUME * (2 ** 15 - 1)) sine_wave_sample = audiocore.RawSample(sine_wave) while True: audio.play(sine_wave_sample, loop=True) time.sleep(1) audio.stop() time.sleep(1)
Once successfully copied, you'll begin hearing a one second 440Hz tone, every other second!
You can edit the following variables in the example code to increase the tone volume, and change the frequency in Hz of the tone being played.
TONE_VOLUME = 0.1 # Increase this to increase the volume of the tone. FREQUENCY = 440 # Set this to the Hz of the tone you want to generate.
WAV Playback
Click the Download Project Bundle button below to download the code.py file and the booploop.wav file in a zip file. Extract the contents of the zip, and copy the code.py and booploop.wav files to your CIRCUITPY drive.
Your CIRCUITPY drive contents should resemble the following.
# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """ CircuitPython I2S WAV file playback. """ import audiocore import board import audiobusio LOOP = False # Update to True loop WAV playback. False plays once. audio = audiobusio.I2SOut(board.A2, board.A1, board.A0) with open("chikken.wav", "rb") as wave_file: wav = audiocore.WaveFile(wave_file) print("Playing wav file!") audio.play(wav, loop=LOOP) while audio.playing: pass print("Done!")
Once successfully copied, you'll hear the WAV file play once.
You can update the LOOP
variable in this example to True
if you want the WAV playback to loop.
LOOP = False # Update to True loop WAV playback. False plays once.
Text editor powered by tinymce.