It's easy to use the PCM5122 in I2C mode with CircuitPython, and the Adafruit_CircuitPython_PCM51xx module. This module allows you to easily write Python code to configure this I2S DAC.
If you want to get up and running quickly without I2C, you can use this DAC in hardware mode as shown on the CircuitPython - Hardware Mode page.
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 SCL to DAC SCL (yellow wire)
- Board SDA to DAC SDA (blue wire)
- Board D9 to DAC BCK (green wire)
- Board D10 to DAC WSEL (white wire)
- Board D11 to DAC DIN (orange wire)
- Board 3.3V to DAC MOD2 (pink wire)
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_PCM51xx 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 folder and file:
- adafruit_bus_device/
- adafruit_pcm51xx.mpy
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Sine tone playback test for the PCM5122 I2S DAC.
"""
import array
import math
import time
import audiobusio
import audiocore
import board
import busio
import adafruit_pcm51xx
# Initialize I2C
i2c = board.I2C()
# Initialize PCM5122
print("Initializing PCM5122...")
pcm = adafruit_pcm51xx.PCM51XX(i2c)
print("Found PCM5122!")
# Set volume to -5dB on both channels
print("\nSetting volume to -5dB")
pcm.volume_db = (-5.0, -5.0)
left_db, right_db = pcm.volume_db
print(f"Volume set to: L={left_db}dB, R={right_db}dB")
# Unmute the DAC
print("\nUnmuting DAC")
pcm.mute = False
print(f"Muted: {pcm.mute}")
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.