Using the PCM5122 breakout in hardware mode with Arduino involves wiring up the breakout to your Arduino-compatible microcontroller and running the provided example code.
If you want to use this DAC with its I2C driver, you can reference the Arduino - I2C Mode page.
Wiring
You can power the I2S DAC with 3.3 to 5VDC, however, the data lines are 3.3V logic only. You'll want to use a 3.3V logic level board.
Here is an Adafruit Metro RP2040 wired up to the DAC for hardware control mode:
- Board 3.3V to DAC VIN (red wire)
- Board GND to DAC GND (black wire)
- Board D9 to DAC BCK (green wire)
- Board D10 to DAC WSEL (white wire)
- Board D11 to DAC DIN (orange wire)
No additional libraries are needed for this example.
// SPDX-FileCopyrightText: 2025 Ladyada for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include <I2S.h>
#include <math.h>
#define pBCLK D9 // BITCLOCK - I2S clock
#define pWS D10 // LRCLOCK - Word select
#define pDOUT D11 // DATA - I2S data
// Create I2S port
I2S i2s(OUTPUT);
const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 500; // amplitude of square wave
const int sampleRate = 16000; // 16 KHz is a good quality
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
int16_t sample = amplitude; // current sample value
int count = 0;
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println(F("Adafruit PCM51xx Hardware Mode Test"));
// Initialize I2S peripheral
Serial.println("Initializing I2S...");
i2s.setBCLK(pBCLK);
i2s.setDATA(pDOUT);
i2s.setBitsPerSample(16);
// Start I2S at the sample rate
if (!i2s.begin(sampleRate)) {
Serial.println("Failed to initialize I2S!");
}
}
void loop() {
if (count % halfWavelength == 0) {
// invert the sample every half wavelength count multiple to generate square wave
sample = -1 * sample;
}
// write the same sample twice, once for left and once for the right channel
i2s.write(sample);
i2s.write(sample);
// increment the counter for the next sample
count++;
}
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the I2S peripheral initialized. In the loop, a sine tone will play through the 3.5 mm jack output.
Page last edited September 26, 2025
Text editor powered by tinymce.