CircuitPython
It's easy to use the PCM510x I2S DAC with CircuitPython and the builtin audiobusio module. This module allows you to easily write Python code that lets you play audio.
CircuitPython Microcontroller Wiring
First wire up the DAC to your board exactly as follows. The following is the DAC wired to a Feather RP2040:
- Board 3.3V to DAC VIN (red wire)
- Board GND to DAC GND (black wire)
- Board D9 to DAC BCK (yellow wire)
- Board D10 to DAC WSEL (green wire)
- Board D11 to DAC DIN (blue wire)
- DAC 3.5mm output to line-level speaker
CircuitPython 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: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython I2S Tone playback example.
Plays a tone for one second on, one
second off, in a loop.
"""
import time
import array
import math
import audiocore
import board
import audiobusio
audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
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.
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.
WAV Playback
Click the Download Project Bundle button below to download the code.py file and the StreetChicken.wav file in a zip file. Extract the contents of the zip, and copy the code.py and StreetChicken.wav files to your CIRCUITPY drive.
Your CIRCUITPY drive contents should resemble the following.
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython I2S WAV file playback.
Plays a WAV file once.
"""
import audiocore
import board
import audiobusio
audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
with open("StreetChicken.wav", "rb") as wave_file:
wav = audiocore.WaveFile(wave_file)
print("Playing wav file!")
audio.play(wav)
while audio.playing:
pass
print("Done!")
Once successfully copied, you'll hear the Street Chicken WAV file play once.
Page last edited March 06, 2025
Text editor powered by tinymce.