Skip to Content

Audio with YDA138-E Amplifier, PCM5102 and ESP32

Audio with YDA138-E Amplifier, PCM5102 and ESP32

The YDA138-E is a compact Class-D audio amplifier. It can deliver up 20 W stereo output into 4 Ω speakers. With a power supply in the range of about 9 V to 13.5 V, it works well with common 12 V power systems.

In this tutorial you will learn how to use the YDA138-E in combination with the PCM5102 DAC to build a Internet radio, a Bluetooth player and a MP3 player.

Required Parts

You will need a PCM5102 DAC module, a YDA138-E Amplifier Board, an ESP32 and a pair of passive speakers. You can use other speakers than the ones I listed – just make sure that their power rating is around 20 watts at 4 Ω or 10 watts at 8 Ω.

Next you need a microcontroller. I picked an ESP32 lite but most other ESP32 boards will be fine as well.

To play MP3 files from an SD Card you will furthermore need an SD Card and an SD Card reader module. Also a breadboard and some cables will be handy for a temporary build.

YDA138-E Amplifier

PCM5102 DAC

2 x Speaker 20 Watt 4 Ohm

Micro SD Card Reader

Micro SD Card 8GB

ESP32 lite Lolin32

ESP32 lite

USB data cable

USB Data Cable

Dupont wire set

Dupont Wire Set

Half_breadboard56a

Breadboard

Makerguides is a participant in affiliate advertising programs designed to provide a means for sites to earn advertising fees by linking to Amazon, AliExpress, Elecrow, and other sites. As an Affiliate we may earn from qualifying purchases.

Technical Features of the YDA138-E Amplifier

The YDA138-E amplifier board is a dual-channel Class-D amplifier centered on YDA138 IC. It reaches about 88 percent efficiency at typical operating conditions and allows the module to run without a heatsink. See the linked datasheet for the technical details of the YDA138 chip:

The board operates within a voltage range of 9.0 to 13.5 and is capable of delivering 10W per channel into a 8Ω or 20W into a 4Ω speakers.

The total harmonic distortion plus noise (THD+N) is 0.02% at 1W output. The signal-to-noise ratio is around 103 dB under typical conditions and channel separation is about −70 dB.

The board features a hybrid output stage with a Class D architecture for the primary speaker terminals and a separate Class AB architecture for the 3.5mm headphone output. This headphone driver is activated via a mechanical switch inside the headphone jack that mutes the speaker output upon insertion.

The board furthermore incorporates a dual-gang logarithmic potentiometer that attenuates the input signal before the fixed 18dB gain stage. The picture below shows the front and back of the board:

Front and back of the YDA138-E amplifier board
Front and back of the YDA138-E amplifier board

The YDA138-E includes over-current protection that disables the output if a short circuit or overload is detected. Thermal protection shuts down the amplifier if the chip temperature becomes too high. The device also includes pop-noise reduction during power transitions.

Pinout of the YDA138-E Amplifier Module

The picture below shows the pinout of the YDA138-E Amplifier Module. The speaker outputs and power supply are at the left of the board, while audio input and volume control are at the right:

Pinout of the YDA138-E Amplifier Module
Pinout of the YDA138-E Amplifier Module

Technical Specification

The following table summarizes the Technical Specification of the YDA138-E Amplifier:

ParameterValue / Description
Amplifier typeClass-D, Class AB Headphone
Supply voltage9 V to 13.5 V
Output power2 × 10 W (8 Ω load) or 20 W (4 Ω load)
Efficiency> 88% at 10 W
Signal-to-noise ratio~103 dB
Total harmonic distortion0.02 % at 1 W
Switching frequency524kHz / 466kHz
Protection featuresOver-current, thermal shutdown
Additional featuresPop-noise reduction, volume control

Architecture of the YDA138-E Audio System

The YDA138-E amplifier expects a line level audio input. You could generate this directly from the ESP32 using its internal digital-analog-converters (DACs). See the Playing Sound with PAM8403 and ESP32 tutorial. But the audio quality would not be very good due to the low resolution (8 bits) of the internal DAC.

We therefore use the PCM5102 as an external DAC that has a much higher resolution. For details about the PCM5102 see the Playing Audio with ESP32 and PCM5102A tutorial. The picture below shows the architecture of our audio system with the ESP32 microcontroller, the PCM5102 DAC and the YDA138-E amplifier:

Architecture of the YDA138-E Audio System
Architecture of the YDA138-E Audio System

The ESP32 generates a digital audio signal and transmits it via the I2S protocol digitally to the PCM5102 DAC. The PCM5102 converts the digital audio into a line level, analog signal for the left and right channel. The YDA138-E amplifies the analog signal to drive two speakers with up to 20 Watts.

Connecting the YDA138-E and PCM5102 to an ESP32

In this section we are connecting the YDA138-E Amplifier and the PCM5102 DAC to the ESP32 microcontroller. The picture below shows the complete wiring diagram:

Connecting YDA138-E to PCM5102 and ESP32
Connecting YDA138-E to PCM5102 and ESP32

Start by connecting the ESP32 to the PCM5102 via the I2S interface. You could configure different pins but I am using LCK=32, BCK=25 and DIN=33 here. The following table shows the connections between the ESP32 and the PCM5102 you need to make:

PCM5102A ESP32
VIN3V3
GNDG
LRCK32
BCK25
DIN33
SCKG

If you have soldered the SCK bridge on the PCM5102 you actually won’t need the SCK to ground connection. I strongly recommend that you read the Playing Audio with ESP32 and PCM5102A tutorial first, if you haven’t configured or used the PCM5102 before. There are other bridges that need to be soldered to ensure that the PCM5102 works!

The YDA138-E Amplifier is connected via line out to the PCM5102. The following table shows the required connections:

YDA138-EPCM5102
LLROUT
GAGND
RROUT

You also have to provide an external power between 9 to 13.5V to the YDA138-E. At 12 V and 20 Watts output you need a power supply that can generate up to 3.5 Ampere. Since the YDA138-E is a Class D amplifier with 88% efficiency you typically don’t need a heatsink, however.

Polarity of Speakers

When connecting the loudspeakers make sure that you match the polarity of the output pins of the YDA138-E with the polarity of the speaker pins. Typically the speaker pins are marked with “+” and “-” signs or have differently shaped pins (thinner for minus).

If there are no markings, you can connect a 1.5V AA battery and if the membrane moves outwards, the plus pole of the battery indicates the plus pole of the speaker. Loudspeaker polarity is defined so that a positive voltage moves the cone forward.

Connecting SD Card Reader

If you want to play MP3 files you need to connect an SD Card reader that stores the audio files on an SD Card. The wiring diagram below shows you how to connect the additional SD Card reader:

Connecting YDA138-E to PCM5102 and ESP32 with SD Card Reader
Connecting YDA138-E to PCM5102 and ESP32 with SD Card Reader

The SD Card Reader communicates via SPI and the default SPI pins of the ESP32 for SPI are CS=5, MOSI=23, CLK=18 and MISO=19. The table below summarizes the connections you need to make between the SD Card Reader and the ESP32:

SD Card ReaderESP32
3V33V
GNDG
CS/SS5
MOSI23
CLK/SCK18
MISO19

If you are not sure which pins are the default SPI pins of your ESP32 have a look at our Find I2C and SPI default pins tutorial. For more information about the SD Card reader see the SD Card Module with ESP32 tutorial.

Installing Libraries

We are going to use arduino-audio-tools library by Phil Schatzmann to build the Internet, Bluetooth and MP3 player. To install this library go to the arduino-audio-tools repo, click on the green “<> Code” button and then “Download ZIP” to download the library as a ZIP file as shown below:

Then open a Sketch, go to Sketch -> Include Library -> Add .ZIP Library … to install the downloaded ZIP library (arduino-audio-tools-main.zip):

For some of the code examples we need two more libraries by Phil Schatzmann; namely the arduino-libhelix library and the ESP32-A2DP library. You can install them in the same way. Click on the link to go to the github repo, click on the green “<> Code” button to download the libraries (arduino-libhelix-main.zip, ESP32-A2DP-main.zip) and then install them.

If this is the first time you program an ESP32 board from your Arduino IDE, you will need to install the ESP32 core as well. For details see the Install ESP32 core in Arduino IDE tutorial.

The next three sections contain the code for a Bluetooth Player, an Internet Radio and an MP3 Player that you can run on the PAM8403, PCM5102 and ESP32 audio system.

Code for a Bluetooth Player

This first code example configures the ESP32 to operate as a Bluetooth A2DP audio sink that receives stereo audio streams from a paired device such as a smartphone and outputs the digital audio data via the I2S peripheral.

/*
www.makerguides.com

Libraries:
- ESP32 Core 3.3.7
- [arduino-audio-tools](https://github.com/pschatzmann/arduino-audio-tools) 
  Version: 1.2.2
- [arduino-libhelix](https://github.com/pschatzmann/arduino-libhelix)
  Version: 0.9.2
- [ESP32-A2DP](https://github.com/pschatzmann/ESP32-A2DP)
  Version: 1.8.8
*/

#include "AudioTools.h"
#include "BluetoothA2DPSink.h"

#define DIN_PIN 33   // serial data
#define LRCK_PIN 32  // word select
#define BCLK_PIN 25  // serial clock

I2SStream i2s;
BluetoothA2DPSink a2dp_sink(i2s);

void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Info);

  auto config = i2s.defaultConfig();
  config.pin_bck = BCLK_PIN;
  config.pin_ws = LRCK_PIN;
  config.pin_data = DIN_PIN;  
  i2s.begin(config);

  a2dp_sink.start("MyMusic");
}

void loop() { }

The code initializes an I2SStream interface with the assigned bit clock, word select, and serial data pins, which are connected to a PCM5102 digital-to-analog converter. The PCM5102 converts the incoming I2S digital audio signal into an analog stereo signal, which is then amplified by the YDA138 amplifier module to drive two connected speakers.

During setup, the ESP32 initializes the I2S driver with the specified pin configuration and starts the Bluetooth A2DP sink with the device name “MyMusic”, making the ESP32 discoverable as a Bluetooth audio receiver.

Once paired and connected, audio streamed over Bluetooth is routed directly through the I2S interface to the DAC and subsequently amplified for playback. The main loop remains empty because audio handling and Bluetooth communication are managed internally by the underlying libraries and run asynchronously in the background.

If you hear a rattling, “tak”, “tak” sound and no music, the volume is too high. Reduce the volume on your phone to zero and then slowly increase it.

Code for an Internet Radio

This code example uses an ESP32 as a Wi-Fi internet radio receiver that streams an online MP3 audio feed, decodes it in real time, and again outputs the audio signal via I2S to the PCM5102A DAC. The analog signal generated by the DAC is then amplified by the YDA138 to drive the connected speakers.

/*
www.makerguides.com

Libraries:
- ESP32 Core 3.3.7
- [arduino-audio-tools](https://github.com/pschatzmann/arduino-audio-tools) 
  Version: 1.2.2
- [arduino-libhelix](https://github.com/pschatzmann/arduino-libhelix)
  Version: 0.9.2
*/

#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include "AudioTools.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
#include "AudioTools/Communication/HTTP/ICYStream.h"

// PCM5102A
#define DIN_PIN 33   // serial data
#define LRCK_PIN 32  // word select
#define BCLK_PIN 25  // serial clock
#define VOLUME 0.05  // Volume

const char* ssid = "ssid";
const char* password = "pwd";
const char* url = "https://jazz.stream.laut.fm/jazz";

ICYStream icystream;
I2SStream i2s;
VolumeStream volume(i2s);
EncodedAudioStream mp3decode(&volume, new MP3DecoderHelix());
StreamCopy copier(mp3decode, icystream);

void callbackMetadata(MetaDataType type, const char* str, int len) {
  Serial.printf("%s: %s\n", toStr(type), str);
}

void setup() {
  Serial.begin(115200);
  AudioLogger::instance().begin(Serial, AudioLogger::Warning);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  auto config = i2s.defaultConfig(TX_MODE);
  config.pin_bck = BCLK_PIN;
  config.pin_ws = LRCK_PIN;
  config.pin_data = DIN_PIN;

  i2s.begin(config);
  volume.begin(config);
  volume.setVolume(VOLUME);
  mp3decode.begin();
  icystream.begin(url);
  icystream.setMetadataCallback(callbackMetadata);
}

void loop() {
  copier.copy();
}

During setup, the ESP32 connects to a specified Wi-Fi network using the provided SSID and password. You will need to replace the strings “ssid” and “pwd” with the credentials of your Wi-Fi network.

Once the wireless connection is established, the code initializes the I2S peripheral in transmit mode and assigns the bit clock, word select, and serial data pins to match the PCM5102 hardware interface. A VolumeStream object is layered on top of the I2S stream to apply volume scaling before the audio data is sent to the DAC.

The program opens an HTTP connection to the specified streaming URL using an ICYStream, which supports ICY metadata commonly used by internet radio stations. Here is list of URLs for some other internet radio stations you can try out:

"https://jazz.stream.laut.fm/jazz"
"http://vis.media-ice.musicradio.com/CapitalMP3";
"http://stream.srg-ssr.ch/m/rsj/mp3_128"
"http://stream.live.vc.bbcmedia.co.uk/bbc_world_service"
"http://icecast.omroep.nl/radio1-bb-mp3"
"http://stream-02-eu.relaxingjazz.com/stream/1/"

The MP3-encoded audio data from the internet station is passed to an EncodedAudioStream configured with the Helix MP3 decoder, which performs real-time decoding of the compressed audio frames into raw PCM samples. These decoded samples are then forwarded through the volume control stage and ultimately transmitted over I2S to the DAC.

A metadata callback function is registered to receive and print stream metadata such as the current track title to the serial monitor. In the main loop, the StreamCopy object continuously transfers data from the network stream through the decoder and audio processing chain.

Code for an MP3 Player

This last program uses the ESP32 as a standalone MP3 audio player that reads audio files from an SD card, decodes them in real time, and outputs the resulting PCM signal via the I2S interface to the PCM5102A DAC.

/*
www.makerguides.com

Libraries:
- ESP32 Core 3.3.7
- [arduino-audio-tools](https://github.com/pschatzmann/arduino-audio-tools) 
  Version: 1.2.2
- [arduino-libhelix](https://github.com/pschatzmann/arduino-libhelix)
  Version: 0.9.2
*/

#include "AudioTools.h"
#include "AudioTools/Disk/AudioSourceSD.h"
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"

// PCM5102A
#define DIN_PIN 33   // serial data
#define LRCK_PIN 32  // word select
#define BCLK_PIN 25  // serial clock
#define VOLUME 0.05   // Volume

#define PATH "/"
#define EXT "mp3"

AudioSourceSD source(PATH, EXT);
I2SStream i2s;
MP3DecoderHelix decoder;
AudioPlayer player(source, i2s, decoder);

void printMetaData(MetaDataType type, const char* str, int len){
  Serial.printf("%s: %s\n", toStr(type), str);
}

void setup() {
  Serial.begin(115200);
  AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);

  auto config = i2s.defaultConfig(TX_MODE);
  config.pin_bck = BCLK_PIN;
  config.pin_ws = LRCK_PIN;
  config.pin_data = DIN_PIN;  
  i2s.begin(config);

  player.setMetadataCallback(printMetaData);
  player.setVolume(VOLUME);
  player.begin();
}

void loop() {
  player.copy();
}

An AudioSourceSD object is initialized to access MP3 files stored on the SD card in the root directory. The source is configured to select files with the “mp3” extension, allowing the system to iterate through compatible audio files available on the card. The AudioPlayer object combines the SD card source, an I2S output stream, and the Helix MP3 decoder into a single playback pipeline.

During setup, the ESP32 initializes serial communication for debugging and configures the I2S peripheral in transmit mode with explicitly assigned bit clock, word select, and serial data pins connected to the PCM5102.

The audio player is configured with a metadata callback function to print information such as track title or artist to the serial monitor when available. A digital volume level is also set before playback begins. Again, don’t set the VOLUME constant too high, otherwise the amplifier starts clipping and you will hear a rattling sound instead of music.

In the main loop, the player.copy() function continuously processes the audio stream. The player reads MP3 data from the SD card, decodes it using the Helix decoder into raw PCM samples, and sends the processed audio through the I2S interface to the DAC for playback. This loop-driven streaming mechanism ensures continuous audio output as long as valid MP3 files are available on the SD card.

Conclusions

In this project, you learned how to play audio using the ESP32 and the YDA138-E amplifier in conjunction with a PCM5102 DAC. You also learned how to stream internet radio, play MP3 files from an SD Card and play audio via Bluetooth. For more code examples see the examples folder of the arduino-tools-library.

If you want to add more functionality such as a volume controller and buttons to skip tracks, see our Playing Audio with ESP32 and PCM5102A tutorial. Similarly, if you need more background on the SD Card Reader module used here, have a look at the SD Card Module with ESP32 tutorial.

For other amplifiers with different speaker output wattages and power supply requirements see the following tutorials:

TutorialOutput WattagePower Supply
TDA7379 Class AB Audio Amplifier with ESP322 × 38 W9 .. 15 V
High-Power ESP32 Audio with TPA3116D2 and PCM51022 x 50 W4.5 .. 26 V
Audio with PAM8403, PCM5102 and ESP322 x 3 W2.5 .. 5.5 V
Stereo Amplifier with TPA31110 XH-A232, PCM5102 and ESP322 x 30 W8 .. 26 V
Playing Audio with ESP32 and MAX983571 x 3 W3.3 .. 5V
Playing Audio with ESP32 and PCM5102Aline level3.3 .. 5 V
Playing Sound with PAM8403 and ESP322 x 3 W2.5 .. 5.5 V
Audio with YDA138-E Amplifier, PCM5102 and ESP322 x 20 W9 .. 13.5 V

Feel free to leave any further questions in the comment section.

Happy Tinkering ; )