Simple test

Ensure your device works with this simple test.

examples/pcm51xx_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5"""
 6Simple test for the PCM5122 I2S DAC.
 7"""
 8
 9import board
10import busio
11
12import adafruit_pcm51xx
13
14# Initialize I2C
15i2c = board.I2C()
16
17# Initialize PCM5122
18print("Initializing PCM5122...")
19pcm = adafruit_pcm51xx.PCM51XX(i2c)
20print("Found PCM5122!")
21
22# Set volume to -10dB on both channels
23print("\nSetting volume to 0dB")
24pcm.volume_db = (0.0, 0.0)
25left_db, right_db = pcm.volume_db
26print(f"Volume set to: L={left_db}dB, R={right_db}dB")
27
28# Unmute the DAC
29print("\nUnmuting DAC")
30pcm.mute = False
31print(f"Muted: {pcm.mute}")

Full test

Test all of the functionality for the I2S DAC

examples/pcm51xx_fulltest.py
  1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
  2#
  3# SPDX-License-Identifier: MIT
  4
  5"""
  6PCM5122 CircuitPython Test Example
  7Basic test example for the Adafruit PCM51xx CircuitPython library
  8"""
  9
 10import time
 11
 12import board
 13import busio
 14
 15import adafruit_pcm51xx
 16
 17print("Adafruit PCM51xx Test")
 18
 19# Initialize I2C
 20i2c = board.I2C()
 21
 22# Initialize PCM51xx (I2C mode only in CircuitPython)
 23try:
 24    pcm = adafruit_pcm51xx.PCM51XX(i2c)
 25    print("PCM51xx initialized successfully!")
 26except Exception as e:
 27    print(f"Could not find PCM51xx, check wiring! Error: {e}")
 28    while True:
 29        time.sleep(0.01)
 30
 31# Set I2S format to I2S
 32print("Setting I2S format")
 33pcm.i2s_format = adafruit_pcm51xx.I2S_FORMAT_I2S
 34
 35# Read and display current format
 36format_val = pcm.i2s_format
 37print("Current I2S format:", end=" ")
 38if format_val == adafruit_pcm51xx.I2S_FORMAT_I2S:
 39    print("I2S")
 40elif format_val == adafruit_pcm51xx.I2S_FORMAT_TDM:
 41    print("TDM/DSP")
 42elif format_val == adafruit_pcm51xx.I2S_FORMAT_RTJ:
 43    print("Right Justified")
 44elif format_val == adafruit_pcm51xx.I2S_FORMAT_LTJ:
 45    print("Left Justified")
 46else:
 47    print("Unknown")
 48
 49# Set I2S word length to 16-bit
 50print("Setting I2S word length")
 51pcm.i2s_size = adafruit_pcm51xx.I2S_SIZE_16BIT
 52
 53# Read and display current word length
 54size_val = pcm.i2s_size
 55print("Current I2S word length:", end=" ")
 56if size_val == adafruit_pcm51xx.I2S_SIZE_16BIT:
 57    print("16 bits")
 58elif size_val == adafruit_pcm51xx.I2S_SIZE_20BIT:
 59    print("20 bits")
 60elif size_val == adafruit_pcm51xx.I2S_SIZE_24BIT:
 61    print("24 bits")
 62elif size_val == adafruit_pcm51xx.I2S_SIZE_32BIT:
 63    print("32 bits")
 64else:
 65    print("Unknown")
 66
 67# Set error detection bits
 68print("Configuring error detection")
 69try:
 70    pcm.ignore_fs_detect = True
 71    pcm.ignore_bck_detect = True
 72    pcm.ignore_sck_detect = True
 73    pcm.ignore_clock_halt = True
 74    pcm.ignore_clock_missing = True
 75    pcm.disable_clock_autoset = False
 76    pcm.ignore_pll_unlock = True
 77    print("Error detection configured successfully")
 78except Exception as e:
 79    print(f"Error detection failed to configure: {e}")
 80
 81# Enable PLL
 82print("Enabling PLL")
 83pcm.pll_enabled = True
 84
 85# Check PLL status
 86pll_enabled = pcm.pll_enabled
 87print(f"PLL enabled: {'Yes' if pll_enabled else 'No'}")
 88
 89# Set PLL reference to BCK
 90print("Setting PLL reference")
 91pcm.pll_reference = adafruit_pcm51xx.PLL_REF_BCK
 92
 93# Read and display current PLL reference
 94pll_ref = pcm.pll_reference
 95print("Current PLL reference:", end=" ")
 96if pll_ref == adafruit_pcm51xx.PLL_REF_SCK:
 97    print("SCK")
 98elif pll_ref == adafruit_pcm51xx.PLL_REF_BCK:
 99    print("BCK")
100elif pll_ref == adafruit_pcm51xx.PLL_REF_GPIO:
101    print("GPIO")
102else:
103    print("Unknown")
104
105# Set DAC clock source to PLL
106print("Setting DAC source")
107pcm.dac_source = adafruit_pcm51xx.DAC_CLK_PLL
108
109# Read and display current DAC source
110dac_source = pcm.dac_source
111print("Current DAC source:", end=" ")
112if dac_source == adafruit_pcm51xx.DAC_CLK_MASTER:
113    print("Master clock (auto-select)")
114elif dac_source == adafruit_pcm51xx.DAC_CLK_PLL:
115    print("PLL clock")
116elif dac_source == adafruit_pcm51xx.DAC_CLK_SCK:
117    print("SCK clock")
118elif dac_source == adafruit_pcm51xx.DAC_CLK_BCK:
119    print("BCK clock")
120else:
121    print("Unknown")
122
123# Test auto mute (default turn off)
124print("Setting auto mute")
125pcm.auto_mute = False
126
127# Read and display current auto mute status
128auto_mute_enabled = pcm.auto_mute
129print(f"Auto mute: {'Enabled' if auto_mute_enabled else 'Disabled'}")
130
131# Test mute (default do not mute)
132print("Setting mute")
133pcm.mute = False
134
135# Read and display current mute status
136mute_enabled = pcm.mute
137print(f"Mute: {'Enabled' if mute_enabled else 'Disabled'}")
138
139# Check DSP boot status and power state
140print(f"DSP boot done: {'Yes' if pcm.dsp_boot else 'No'}")
141
142power_state = pcm.power_state
143print("Power state:", end=" ")
144if power_state == adafruit_pcm51xx.POWER_POWERDOWN:
145    print("Powerdown")
146elif power_state == adafruit_pcm51xx.POWER_WAIT_CP_VALID:
147    print("Wait for CP voltage valid")
148elif power_state in {adafruit_pcm51xx.POWER_CALIBRATION_1, adafruit_pcm51xx.POWER_CALIBRATION_2}:
149    print("Calibration")
150elif power_state == adafruit_pcm51xx.POWER_VOLUME_RAMP_UP:
151    print("Volume ramp up")
152elif power_state == adafruit_pcm51xx.POWER_RUN_PLAYING:
153    print("Run (Playing)")
154elif power_state == adafruit_pcm51xx.POWER_LINE_SHORT:
155    print("Line output short / Low impedance")
156elif power_state == adafruit_pcm51xx.POWER_VOLUME_RAMP_DOWN:
157    print("Volume ramp down")
158elif power_state == adafruit_pcm51xx.POWER_STANDBY:
159    print("Standby")
160else:
161    print("Unknown")
162
163# Check PLL lock status
164pll_locked = pcm.pll_locked
165print(f"PLL locked: {'Yes' if pll_locked else 'No'}")
166
167# Set volume to -6dB on both channels
168print("Setting volume")
169pcm.volume_db = (-6.0, -6.0)
170
171# Read and display current volume
172left_vol, right_vol = pcm.volume_db
173print(f"Current volume - Left: {left_vol:.1f}dB, Right: {right_vol:.1f}dB")
174
175# Test GPIO pins (CircuitPython bonus!)
176print("\n--- GPIO Pin Test ---")
177# Test pin 1 as output
178print("Testing pin1 as output")
179pcm.pin1.switch_to_output(True)
180print(f"Pin1 value: {pcm.pin1.value}")
181pcm.pin1.value = False
182print(f"Pin1 value after setting low: {pcm.pin1.value}")
183
184# Test pin 2 as input
185print("Testing pin2 as input")
186pcm.pin2.switch_to_input()
187print(f"Pin2 value: {pcm.pin2.value}")
188
189print("\nTest complete!")

Sine tone playback

Playback a sine tone

examples/pcm51xx_sinetone.py
 1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4
 5"""
 6Sine tone playback test for the PCM5122 I2S DAC.
 7"""
 8
 9import array
10import math
11import time
12
13import audiobusio
14import audiocore
15import board
16import busio
17
18import adafruit_pcm51xx
19
20# Initialize I2C
21i2c = board.I2C()
22
23# Initialize PCM5122
24print("Initializing PCM5122...")
25pcm = adafruit_pcm51xx.PCM51XX(i2c)
26print("Found PCM5122!")
27
28# Set volume to -5dB on both channels
29print("\nSetting volume to -5dB")
30pcm.volume_db = (-5.0, -5.0)
31left_db, right_db = pcm.volume_db
32print(f"Volume set to: L={left_db}dB, R={right_db}dB")
33
34# Unmute the DAC
35print("\nUnmuting DAC")
36pcm.mute = False
37print(f"Muted: {pcm.mute}")
38
39audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
40
41tone_volume = 0.5  # Increase this to increase the volume of the tone.
42frequency = 440  # Set this to the Hz of the tone you want to generate.
43length = 8000 // frequency
44sine_wave = array.array("h", [0] * length)
45for i in range(length):
46    sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2**15 - 1))
47sine_wave_sample = audiocore.RawSample(sine_wave)
48
49while True:
50    audio.play(sine_wave_sample, loop=True)
51    time.sleep(1)
52    audio.stop()
53    time.sleep(1)