/
Example_04_Speaker.ino
161 lines (123 loc) · 5.15 KB
/
Example_04_Speaker.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/******************************************************************************
Example_04_Speaker.ino
Demonstrates analog audio input (on INPUT1s), sets volume control, and Speaker
output on the WM8960 Codec.
Audio should be connected to both the left and right "INPUT1" inputs,
they are labeled "RIN1" and "LIN1" on the board.
This example will pass your audio source through the mixers and gain stages of
the codec using all of the analog bypass paths.
It will output the sound on the Speaker outputs.
You can now control the volume of the codecs built in class-d amp using this
function:
codec.setSpeakerVolumeDB(6.00); Valid inputs are -73.00 to 6.00 (1.00 dB steps)
Development platform used:
SparkFun ESP32 IoT RedBoard v10
HARDWARE CONNECTIONS
**********************
ESP32 ------- CODEC
**********************
QWIIC ------- QWIIC *Note this connects GND/3.3V/SDA/SCL
GND --------- GND *optional, but not a bad idea
5V ---------- VIN *needed to power codec's onboard AVDD (3.3V vreg)
**********************
CODEC ------- AUDIO IN
**********************
GND --------- TRS INPUT SLEEVE *ground for line level input
LINPUT1 ----- TRS INPUT TIP *left audio
RINPUT1 ----- TRS INPUT RING1 *right audio
**********************
CODEC -------- AUDIO OUT
**********************
SL+ --------- Left Speaker +
SL- --------- Left Speaker -
SR+ --------- Right Speaker +
SR- --------- Right Speaker -
*Note, with a class-d speaker amp like this, you need to connections like above.
Each speaker must be connected to its correct + and -.
You cannot connect the "-" side of the speaker to GND.
You cannot share the "-" side of two speakers (like with common "TRS-wired"
headphones).
Pete Lewis @ SparkFun Electronics
October 14th, 2022
https://github.com/sparkfun/SparkFun_WM8960_Arduino_Library
This code was created using some code by Mike Grusin at SparkFun Electronics
Included with the LilyPad MP3 example code found here:
Revision history: version 1.0 2012/07/24 MDG Initial release
https://github.com/sparkfun/LilyPad_MP3_Player
Do you like this library? Help support SparkFun. Buy a board!
SparkFun Audio Codec Breakout - WM8960 (QWIIC)
https://www.sparkfun.com/products/21250
All functions return 1 if the read/write was successful, and 0
if there was a communications failure. You can ignore the return value
if you just don't care anymore.
For information on the data sent to and received from the CODEC,
refer to the WM8960 datasheet at:
https://github.com/sparkfun/SparkFun_Audio_Codec_Breakout_WM8960/blob/main/Documents/WM8960_datasheet_v4.2.pdf
This code is released under the [MIT License](http://opensource.org/licenses/MIT).
Please review the LICENSE.md file included with this example. If you have any questions
or concerns with licensing, please contact techsupport@sparkfun.com.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <Wire.h>
#include <SparkFun_WM8960_Arduino_Library.h>
// Click here to get the library: http://librarymanager/All#SparkFun_WM8960
WM8960 codec;
void setup()
{
Serial.begin(115200);
Serial.println("Example 4 - Speaker");
Wire.begin();
if (codec.begin() == false) //Begin communication over I2C
{
Serial.println("The device did not respond. Please check wiring.");
while (1); // Freeze
}
Serial.println("Device is connected properly.");
// General setup needed
codec.enableVREF();
codec.enableVMID();
// Setup signal flow through the analog audio bypass connections
codec.enableLMIC();
codec.enableRMIC();
// Connect from INPUT1 to "n" (aka inverting) inputs of PGAs.
codec.connectLMN1();
codec.connectRMN1();
// Disable mutes on PGA inputs (aka INTPUT1)
codec.disableLINMUTE();
codec.disableRINMUTE();
// Set input boosts to get inputs 1 to the boost mixers
codec.setLMICBOOST(WM8960_MIC_BOOST_GAIN_0DB);
codec.setRMICBOOST(WM8960_MIC_BOOST_GAIN_0DB);
codec.connectLMIC2B();
codec.connectRMIC2B();
// Enable boost mixers
codec.enableAINL();
codec.enableAINR();
// Connect LB2LO (booster to output mixer (analog bypass)
codec.enableLB2LO();
codec.enableRB2RO();
// Set gainstage between booster mixer and output mixer
codec.setLB2LOVOL(WM8960_OUTPUT_MIXER_GAIN_0DB);
codec.setRB2ROVOL(WM8960_OUTPUT_MIXER_GAIN_0DB);
// Enable output mixers
codec.enableLOMIX();
codec.enableROMIX();
// CLOCK STUFF, These settings will get you 44.1KHz sample rate, and class-d
// freq at 705.6kHz
codec.enablePLL(); // Needed for class-d amp clock
codec.setPLLPRESCALE(WM8960_PLLPRESCALE_DIV_2);
codec.setSMD(WM8960_PLL_MODE_FRACTIONAL);
codec.setCLKSEL(WM8960_CLKSEL_PLL);
codec.setSYSCLKDIV(WM8960_SYSCLK_DIV_BY_2);
codec.setDCLKDIV(WM8960_DCLKDIV_16);
codec.setPLLN(7);
codec.setPLLK(0x86, 0xC2, 0x26); // PLLK=86C226h
codec.enableSpeakers();
Serial.println("Volume set to +0dB");
codec.setSpeakerVolumeDB(0.00);
Serial.println("Example complete. Listen to left/right INPUT1 on Speaker outputs.");
}
void loop()
{
// Nothing to see here.
}