Left, then right
The first sketch plays a 440 Hz tone in the left ear for a second, then the right, then both, then a second of silence, at 44.1 kHz. It proves the wiring, the clocks and the jack in one go, and it shows the one rule of a stereo frame: the left sample goes first.
What you need
- An ESP32-S3 dev board and its USB cable.
- The TK103 and six jumper wires, or four: XSMT is optional.
- Headphones or a powered speaker. Hold headphones away from your ears the first time, until you have heard the level.
Wire it
| TK103 | ESP32-S3 |
|---|---|
| GND | GND |
| 3V3 | 3V3 |
| BCK | GPIO 42 |
| DIN | GPIO 41 |
| LRCK | GPIO 40 |
| XSMT | GPIO 39, optional |
Then plug the headphones or the speaker's cable into the jack.
Left is the first sample
The sketch writes each frame as two 16-bit numbers, frame[0] then
frame[1], and the order is the whole of stereo. The chip takes the first
sample of every frame, sent while LRCK is LOW, as the left channel, and the
jack carries left on the tip of the plug and right on the ring.
tone2(440, 1000, 1, 0) puts the sine sample in frame[0] and 0 in
frame[1], so only the left ear hears it. 0, 1 is the right ear, and 1, 1
both, which sounds as if it comes from the middle.
What it prints
left
right
both
leftOne word as each second starts. If the words appear and the ears do not agree with them, the wiring is fine and the fault is between the jack and your ears.
Make it yours
- Louder: raise
level. 3000 is under a tenth of full scale, chosen to be gentle in headphones; the largest a 16-bit sample holds is 32767. - Another note: change the 440. The same formula makes any frequency well below half the sample rate.
- Another rate: keep
RATEat 32000 or above with 16-bit slots. Below that, read Three wires, no master clock first.
The code
tone2() makes one sine sample at a time and writes it as a frame of two: frame[0] for the left ear, frame[1] for the right, each either the sample or 0. At 44.1 kHz with 16-bit stereo the bit clock is 1.4112 MHz, inside TI's table.
// TK103 PCM5102 DAC: a tone in the left ear, then the right, then both.
//
// Wiring, TK103 to ESP32-S3:
// GND -> GND
// 3V3 -> 3V3
// BCK -> GPIO 42
// DIN -> GPIO 41
// LRCK -> GPIO 40
// XSMT -> GPIO 39 (optional: HIGH plays, LOW mutes; pulled HIGH on
// the board when nothing is connected)
// Headphones or powered speakers in the 3.5 mm jack. Never a bare
// speaker, never a speaker amplifier's output.
//
// Arduino IDE: Tools > Board > "ESP32S3 Dev Module" (esp32 core 3.x),
// USB CDC On Boot: Enabled. Serial Monitor at 115200.
#include <ESP_I2S.h>
const int PIN_BCK = 42;
const int PIN_DIN = 41;
const int PIN_LRCK = 40;
const int PIN_XSMT = 39;
const int RATE = 44100;
I2SClass dac;
// left and right are 0 or 1: which ear hears the tone.
void tone2(float hz, int ms, int left, int right) {
const int16_t level = 3000; // gentle in headphones
int16_t frame[2];
float phase = 0, step = 2 * PI * hz / RATE;
for (long i = 0; i < (long)RATE * ms / 1000; i++) {
int16_t s = (int16_t)(level * sinf(phase));
frame[0] = left ? s : 0; // the left sample goes first
frame[1] = right ? s : 0;
dac.write((uint8_t *)frame, sizeof(frame));
phase += step;
if (phase > 2 * PI) phase -= 2 * PI;
}
}
void setup() {
Serial.begin(115200);
pinMode(PIN_XSMT, OUTPUT);
digitalWrite(PIN_XSMT, HIGH); // un-mute
dac.setPins(PIN_BCK, PIN_LRCK, PIN_DIN);
if (!dac.begin(I2S_MODE_STD, RATE, I2S_DATA_BIT_WIDTH_16BIT,
I2S_SLOT_MODE_STEREO)) {
Serial.println("I2S did not start");
while (true) delay(1000);
}
}
void loop() {
Serial.println("left");
tone2(440, 1000, 1, 0);
Serial.println("right");
tone2(440, 1000, 0, 1);
Serial.println("both");
tone2(440, 1000, 1, 1);
tone2(0, 1000, 0, 0);
}Compiled with esp32 core 3.3.10 for the ESP32S3 Dev Module; not run on hardware. The owner's bench code, on the older I2S driver, ran the TK103 on these same four GPIOs. ESP_I2S comes with the core, so there is nothing to install. The XSMT wire is optional: the board holds it HIGH without one.
When it does not work
The sketch runs, so the fault is on the wires or in the jack. Check BCK on GPIO 42, DIN on 41 and LRCK on 40, that the red LED is lit, and that the plug is all the way in. Then take the XSMT wire off: the board holds it HIGH by itself.
The sketch sends the left sample first and the chip puts it on the tip of the plug, which is the left ear. Check which way round the headphones are, and any adapter between them and the jack, before changing the sketch.
Tools > USB CDC On Boot must be Enabled for Serial to reach the USB port on most ESP32-S3 boards. Set it, upload again, and reopen the Serial Monitor at 115200.
begin() refused the settings or the pins. Check the four PIN_ numbers are GPIOs your board has and that nothing else in the sketch uses them, and that the board is set to ESP32S3 Dev Module on esp32 core 3.x.
Edit this page — content/books/i2s-dac/left-then-right.mdx
Questions about this product
See what other owners have asked, and read their solutions.
I2S DAC
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.