Simple test

Ensure your device works with this simple test. Plays a 440hz sine wave at 16-bit, 44.1khz through the line output.

examples/tlv320aic3204_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2026 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import array
 7import math
 8import time
 9
10import audiobusio
11import audiocore
12import board
13
14from relic_tlv320aic3204 import TLV320AIC3204
15
16# Initialize codec
17codec = TLV320AIC3204(
18    i2c=board.STEMMA_I2C(), mclk=board.GP17, rst=board.GP16
19)  # defaults to 16 bit, 44.1khz sample rate
20audio = audiobusio.I2SOut(board.GP18, board.GP19, board.GP20)
21
22# Setup DAC Output
23codec.dac_volume = 0.0  # dB
24codec.dac_enabled = True
25codec.dac_muted = False
26
27# Line Output
28codec.dac_to_line_output = True
29codec.line_output_enabled = True
30codec.line_output_muted = False
31
32# Headphone Output
33# codec.dac_to_headphone_output = True
34# codec.headphone_output_enabled = True
35# codec.headphone_output_muted = False
36
37# Generate one period of sine wave
38length = codec.sample_rate // 440
39sine_wave = array.array("H", [0] * length)
40for i in range(length):
41    sine_wave[i] = min(
42        max(int(math.sin(math.pi * 2 * i / length) * (2**15) + 2**15), -65536), 65535
43    )
44sine_wave = audiocore.RawSample(sine_wave, sample_rate=codec.sample_rate)
45
46while True:
47    audio.play(sine_wave, loop=True)
48    time.sleep(0.5)
49    audio.stop()
50    time.sleep(0.5)

Analog Passthrough

Utilize the input mixer to pass the analog signal at IN1L and IN1R through to either the line output or headphone output. Also demonstrates how to use the dedicated input 1 passthrough to headphones.

examples/tlv320aic3204_passthrough.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2026 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import board
 7
 8import relic_tlv320aic3204
 9
10# Initialize codec
11codec = relic_tlv320aic3204.TLV320AIC3204(
12    i2c=board.STEMMA_I2C(),
13    rst=board.GP16,
14)  # defaults to 16 bit, 44.1khz sample rate
15
16# Direct Passthrough of INPUT1 to Headphones
17# codec.input1_to_headphone_output = True
18# codec.input1_to_headphone_output_volume = 0.0  # dB
19
20# Enable mic input and connect to desired source
21codec.connect_input(relic_tlv320aic3204.INPUT_1)
22codec.input_gain = 0.0  # dB
23
24# Setup Passthrough Input Mixer
25codec.input_passthrough_enabled = True
26codec.input_passthrough_volume = 0.0  # dB
27
28# Line Output
29codec.input_to_line_output = True
30codec.line_output_enabled = True
31codec.line_output_muted = False
32
33# Headphone Output
34# codec.input_to_headphone_output = True
35# codec.headphone_output_enabled = True
36# codec.headphone_output_muted = False

ADC Input

With the help of the PIO I2S library, read I2S data from the single-ended line level input on IN1L.

examples/tlv320aic3204_input.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2026 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import board
 7import pio_i2s
 8import ulab.numpy as np
 9
10import relic_tlv320aic3204
11
12# Initialize codec
13codec = relic_tlv320aic3204.TLV320AIC3204(
14    i2c=board.STEMMA_I2C(),
15    mclk=board.GP17,
16    rst=board.GP16,
17)  # defaults to 16 bit, 44.1khz sample rate
18i2s = pio_i2s.I2S(
19    bit_clock=board.GP18,
20    word_select=board.GP19,
21    data_in=board.GP21,
22    sample_rate=codec.sample_rate,
23    channel_count=1,
24)
25
26# Connect IN1L to Left MICPGA and IN1R to Right MICPGA
27codec.connect_input(relic_tlv320aic3204.INPUT_1, relic_tlv320aic3204.IMPEDANCE_20K)
28codec.input_gain = 6.0  # dB
29
30
31# Setup ADC Input
32codec.dac_enabled = True  # BUG: DAC must be enabled for ADC functionality
33codec.adc_volume = 0.0  # dB
34codec.adc_enabled = True
35codec.adc_muted = False
36
37while True:
38    print(np.max(np.array(i2s.read(block=True), dtype=np.int16)))

Bi-directional I2S

Utilize the DAC and ADC simultaneously with the help of the PIO I2S library. Plays a 440hz sine wave at 16-bit, 44.1khz through the line output. Connect the line output to IN1L to test the ADC.

examples/tlv320aic3204_bidirectional.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2026 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import array
 7import math
 8
 9import board
10import pio_i2s
11import ulab.numpy as np
12
13import relic_tlv320aic3204
14
15# Initialize codec
16codec = relic_tlv320aic3204.TLV320AIC3204(
17    i2c=board.STEMMA_I2C(),
18    mclk=board.GP17,
19    rst=board.GP16,
20)  # defaults to 16 bit, 44.1khz sample rate
21i2s = pio_i2s.I2S(
22    bit_clock=board.GP18,
23    word_select=board.GP19,
24    data_out=board.GP20,
25    data_in=board.GP21,
26    sample_rate=codec.sample_rate,
27    channel_count=1,
28)
29
30# Setup DAC Output
31codec.dac_volume = 0.0  # dB
32codec.dac_enabled = True
33codec.dac_muted = False
34
35# Line Output
36codec.dac_to_line_output = True
37codec.line_output_enabled = True
38codec.line_output_muted = False
39
40# Connect IN1L to Left MICPGA and IN1R to Right MICPGA
41codec.connect_input(relic_tlv320aic3204.INPUT_1, relic_tlv320aic3204.IMPEDANCE_20K)
42codec.input_gain = 6.0  # dB
43
44# Setup ADC Input
45codec.adc_volume = 0.0  # dB
46codec.adc_enabled = True
47codec.adc_muted = False
48
49# Play sine wave
50length = codec.sample_rate // 440
51sine_wave = array.array(i2s.buffer_format, [0] * i2s.buffer_size)
52for i in range(i2s.buffer_size):
53    sine_wave[i] = min(max(int(math.sin(math.pi * 2 * i / length) * (2**15)), -32768), 32767)
54i2s.write(sine_wave, loop=True)
55
56while True:
57    print(np.max(np.array(i2s.read(block=True), dtype=np.int16)))