Simple test

Ensure your device works with this simple test that uses synthio to generate basic tones, outputs this signal to the codec via I2S, and plays back the DAC output with both headphones and speakers.

examples/wm8960_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
 3# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
 4#
 5# SPDX-License-Identifier: Unlicense
 6
 7"""
 8Demonstrates I2C Output on WM8960 Codec by generating a simple tone using synthio. Sounds like an
 9alarm clock.
10
11It will output the sound on the headphone outputs.
12It is setup to do a capless headphone setup, so connect your headphones ground to "OUT3" and this
13provides a buffered VMID.
14
15HARDWARE CONNECTIONS
16
17**********************
18MCU --------- CODEC
19**********************
20QWIIC ------- QWIIC       *Note this connects GND/3.3V/SDA/SCL
21GND --------- GND         *optional, but not a bad idea
225V ---------- VIN         *needed to power codec's onboard AVDD (3.3V vreg)
23AUDIO_TXD --- DDT         *aka DAC_DATA/I2S_SDO/"serial data out", this carries the I2S audio data
24                           from MCU to codec DAC
25AUDIO_BCLK -- BCK         *aka BCLK/I2S_SCK/"bit clock", this is the clock for I2S audio, can be
26                           controlled via controller or peripheral.
27AUDIO_SYNC -- DLRC        *aka I2S_WS/LRC/"word select"/"left-right-channel", this toggles for left
28                           or right channel data.
29
30**********************
31CODEC ------- AUDIO OUT
32**********************
33OUT3 -------- TRS OUTPUT SLEEVE          *buffered "vmid" (aka "HP GND")
34HPL --------- TRS OUTPUT TIP             *left HP output
35HPR --------- TRS OUTPUT RING1           *right HP output
36
37You can now control the volume of the codecs built in headphone buffers using this function:
38codec.setHeadphoneVolumeDB(6.00)
39Valid inputs are -74.00 (MUTE) up to +6.00, (1.00dB steps).
40
41For information on the data sent to and received from the CODEC, refer to the WM8960 datasheet at:
42https://github.com/sparkfun/SparkFun_Audio_Codec_Breakout_WM8960/blob/main/Documents/WM8960_datasheet_v4.2.pdf
43"""
44
45import time
46import board
47import digitalio
48import audiobusio
49import synthio
50from adafruit_wm8960 import WM8960
51
52codec = WM8960(board.I2C(), 44100, 16)
53codec.volume = 1.0
54codec.headphone = 0.5
55codec.speaker = 0.5
56
57# Configure I2S Output
58audio = audiobusio.I2SOut(board.AUDIO_BCLK, board.AUDIO_SYNC, board.AUDIO_TXD)
59
60# Setup synthio
61synth = synthio.Synthesizer(sample_rate=codec.sample_rate)
62audio.play(synth)
63
64led = digitalio.DigitalInOut(board.LED)
65led.switch_to_output()
66
67while True:
68    print("note on")
69    synth.press(65)  # midi note 65 = F4
70    led.value = True
71    time.sleep(0.5)
72    synth.release(65)  # release the note we pressed
73    led.value = False
74    print("note off")
75    time.sleep(0.5)

Input

Demonstrates analog stereo audio input bypass on INPUT1, INPUT2, and INPUT3 simultaneously.

examples/wm8960_input.py
 1# SPDX-FileCopyrightText: Copyright (c) 2022 Pete Lewis for SparkFun Electronics
 2# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: MIT
 5
 6"""
 7Demonstrates analog audio input (on INPUT2s), sets volume control, and headphone output on the
 8WM8960 Codec.
 9
10Audio should be connected to both the left and right "INPUT2" inputs, they are labeled "RIN2" and
11"LIN2" on the board.
12
13This example will pass your audio source through the mixers and gain stages of the codec using all
14of the analog bypass paths.
15
16It will output the sound on the headphone outputs.
17It is setup to do a capless headphone setup, so connect your headphones ground to "OUT3" and this
18provides a buffered VMID.
19
20You can now control the volume of the codecs built in headphone buffers using this function:
21codec.setHeadphoneVolumeDB(6.00); Valid inputs are -74.00 (MUTE) up to +6.00, (1.00dB steps).
22
23HARDWARE CONNECTIONS
24
25**********************
26MCU --------- CODEC
27**********************
28QWIIC ------- QWIIC       *Note this connects GND/3.3V/SDA/SCL
29GND --------- GND         *optional, but not a bad idea
305V ---------- VIN         *needed to power codec's onboard AVDD (3.3V vreg)
31
32**********************
33CODEC ------- AUDIO IN
34**********************
35GND --------- TRS INPUT SLEEVE        *ground for line level input
36LINPUT2 ----- TRS INPUT TIP           *left audio
37RINPUT2 ----- TRS INPUT RING1         *right audio
38
39**********************
40CODEC -------- AUDIO OUT
41**********************
42OUT3 --------- TRS OUTPUT SLEEVE          *buffered "vmid" (aka "HP GND")
43HPL ---------- TRS OUTPUT TIP             *left HP output
44HPR ---------- TRS OUTPUT RING1           *right HP output
45
46Originally authored by Pete Lewis @ SparkFun Electronics, October 14th, 2022
47https://github.com/sparkfun/SparkFun_WM8960_Arduino_Library
48
49For information on the data sent to and received from the CODEC, refer to the WM8960 datasheet at:
50https://github.com/sparkfun/SparkFun_Audio_Codec_Breakout_WM8960/blob/main/Documents/WM8960_datasheet_v4.2.pdf
51"""
52
53import board
54from adafruit_wm8960 import Input, WM8960
55
56codec = WM8960(board.I2C())
57
58# Select the desired input. Available options are MIC1 (single-ended), MIC2 (differential),
59# MIC3 (differential), LINE2, or LINE3.
60codec.input = Input.MIC1
61
62# Configure the microphone boost gain
63codec.gain = 0.5
64
65# Bypass analog signal to analog output
66codec.monitor = 1.0
67
68# Enable the amplifier and set the output volume
69codec.headphone = 0.5

3D Enhance

Demonstrates 3D Enhance and ADC/DAC loopback feature of WM8960 Codec.

examples/wm8960_3d_enhance.py
 1# SPDX-FileCopyrightText: Copyright (c) 2022 Pete Lewis for SparkFun Electronics
 2# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: MIT
 5
 6"""
 7Demonstrates analog audio input (on INPUT1s), ADC/DAC Loopback, sets volume control, and Headphone
 8output on the WM8960 Codec.
 9
10Audio should be connected to both the left and right "INPUT1" inputs, they are labeled "RIN1" and
11"LIN1" on the board.
12
13This example will pass your audio source through the mixers and gain stages of the codec into the
14ADC. Turn on Loopback (so ADC is feed directly to DAC).
15Then send the output of the DAC to the headphone outs.
16
17You can now control the volume of the codecs built in headphone amp using this function:
18codec.setHeadphoneVolumeDB(6.00)
19Valid inputs are -74.00 (MUTE) up to +6.00, (1.00dB steps).
20
21HARDWARE CONNECTIONS
22
23**********************
24MCU --------- CODEC
25**********************
26QWIIC ------- QWIIC       *Note this connects GND/3.3V/SDA/SCL
27GND --------- GND         *optional, but not a bad idea
285V ---------- VIN         *needed to power codec's onboard AVDD (3.3V vreg)
29
30**********************
31CODEC ------- AUDIO IN
32**********************
33GND --------- TRS INPUT SLEEVE        *ground for line level input
34LINPUT1 ----- TRS INPUT TIP           *left audio
35RINPUT1 ----- TRS INPUT RING1         *right audio
36
37**********************
38CODEC -------- AUDIO OUT
39**********************
40OUT3 --------- TRS OUTPUT SLEEVE          *buffered "vmid" (aka "HP GND")
41HPL ---------- TRS OUTPUT TIP             *left HP output
42HPR ---------- TRS OUTPUT RING1           *right HP output
43
44Originally authored by Pete Lewis @ SparkFun Electronics, October 14th, 2022
45https://github.com/sparkfun/SparkFun_WM8960_Arduino_Library
46
47For information on the data sent to and received from the CODEC, refer to the WM8960 datasheet at:
48https://github.com/sparkfun/SparkFun_Audio_Codec_Breakout_WM8960/blob/main/Documents/WM8960_datasheet_v4.2.pdf
49"""
50
51import time
52import board
53from adafruit_wm8960 import Input, WM8960
54
55codec = WM8960(board.I2C())
56codec.loopback = True
57codec.input = Input.MIC1
58codec.gain = 0.5
59codec.volume = 1.0
60
61codec.headphone = 0.5
62
63# Toggle 3D enhance on and off
64while True:
65    codec.enhance = 0.0 if codec.enhance else 1.0
66    time.sleep(2.0)

Automatic Level Control

Demonstrates how to use the automatic level control feature of the WM8960 Codec.

examples/wm8960_automatic_level_control.py
 1# SPDX-FileCopyrightText: Copyright (c) 2022 Pete Lewis for SparkFun Electronics
 2# SPDX-FileCopyrightText: Copyright (c) 2024 Cooper Dalrymple
 3#
 4# SPDX-License-Identifier: MIT
 5
 6"""
 7Demonstrates how to use the automatic level control feature of the WM8960 Codec.
 8
 9Attach a potentiomenter to GND/A0/3V3 to actively adjust the ALC target setting.
10
11This example sets up the codec for analog audio input (on INPUT1s), ADC/DAC Loopback, sets headphone
12volume, and Headphone output on the WM8960 Codec.
13
14Audio should be connected to both the left and right "INPUT1" inputs, they are labeled "RIN1" and
15"LIN1" on the board.
16
17This example will pass your audio source through the mixers and gain stages of the codec into the
18ADC. Turn on Loopback (so ADC is feed directly to DAC).
19Then send the output of the DAC to the headphone outs.
20
21We will use the user input via potentiometer on A0 to set the ALC target value. The ALC will adjust
22the gain of the pga input buffer to try and keep the signal level at the target.
23
24HARDWARE CONNECTIONS
25
26**********************
27MCU --------- CODEC
28**********************
29QWIIC ------- QWIIC       *Note this connects GND/3.3V/SDA/SCL
30GND --------- GND         *optional, but not a bad idea
315V ---------- VIN         *needed to power codec's onboard AVDD (3.3V vreg)
32
33**********************
34MCU --------- POTENTIOMTER (aka blue little trimpot)
35**********************
36GND --------- "right-side pin"
37A0 ---------- center pin            *aka center tap connection
383V3 --------- "left-side pin"
39
40**********************
41CODEC ------- AUDIO IN
42**********************
43GND --------- TRS INPUT SLEEVE        *ground for line level input
44LINPUT1 ----- TRS INPUT TIP           *left audio
45RINPUT1 ----- TRS INPUT RING1         *right audio
46
47**********************
48CODEC ------- AUDIO OUT
49**********************
50OUT3 -------- TRS OUTPUT SLEEVE          *buffered "vmid" (aka "HP GND")
51HPL --------- TRS OUTPUT TIP             *left HP output
52HPR --------- TRS OUTPUT RING1           *right HP output
53
54Originally authored by Pete Lewis @ SparkFun Electronics, October 14th, 2022
55https://github.com/sparkfun/SparkFun_WM8960_Arduino_Library
56
57For information on the data sent to and received from the CODEC, refer to the WM8960 datasheet at:
58https://github.com/sparkfun/SparkFun_Audio_Codec_Breakout_WM8960/blob/main/Documents/WM8960_datasheet_v4.2.pdf
59"""
60
61import time
62import board
63from analogio import AnalogIn
64from adafruit_simplemath import map_range
65from adafruit_wm8960 import Input, WM8960
66
67analog_in = AnalogIn(board.A0)
68
69codec = WM8960(board.I2C())
70codec.input = Input.MIC1
71codec.gain = 0.5
72codec.volume = 1.0
73codec.headphone = 0.5
74
75codec.alc = True
76codec.alc_gain = (
77    0.75,  # target
78    1.0,  # max gain
79    0.0,  # min gain
80    0.0,  # noise gate
81)
82codec.alc_time = (
83    0.024,  # attack
84    0.192,  # decay
85    0.0,  # hold
86)
87
88codec.loopback = True
89
90gain = list(codec.alc_gain)
91while True:
92    gain[0] = map_range(analog_in.value, 0, 65536, 0.0, 1.0)
93    codec.alc_gain = gain
94    time.sleep(1.0)