It's easy to use the PCM5122 in hardware mode with CircuitPython and the the builtin audiobusio module. This module allows you to easily write Python code to play audio.
If you want to use this DAC with its I2C driver, you can reference the CircuitPython - I2C Mode page.
No additional libraries are required for this example.
CircuitPython Microcontroller Wiring
First wire up the I2S DAC to your board exactly as follows. The following is the DAC wired to a Feather RP2040 with the headphone output:
- Board 3.3V to DAC VIN (red wire)
- Board GND to DAC GND (black wire)
- Board D9 to DAC BCK (green wire)
- Board D10 to DAC WSEL (white wire)
- Board D11 to DAC DIN (orange wire)
Example Code
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: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Sine tone playback test for the PCM5122 I2S DAC in hardware mode.
"""
import array
import math
import time
import audiobusio
import audiocore
import board
audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
tone_volume = 0.5 # 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 the code starts running, you'll begin hearing a one second 440Hz tone, every other second.
Page last edited September 26, 2025
Text editor powered by tinymce.