Skip to Content

High-Power ESP32 Audio with TPA3116D2 and PCM5102

High-Power ESP32 Audio with TPA3116D2 and PCM5102

The TPA3116D2 is a high-efficiency Class-D audio amplifier designed for driving speakers with up to 50 Watts per channel. The chip operates with low heat generation due to its high efficiency, which makes it suitable for compact and embedded designs.

It supports a wide supply voltage range from 4.5 V to 26 V, which allows flexible power options in ESP32-based projects. The amplifier also includes built-in protection features such as overcurrent, overtemperature, and undervoltage protection.

In this tutorial you will learn how to use the TPA3116D2 to build a Internet, Bluetooth or MP3 radio with up to 50 Watts of output.

Required Parts

For this project we will need a PCM5102 DAC module, a TPA3116D2 Amplifier Board, an ESP32 and a pair of passive speakers.

You can use other speakers than the ones listed. Just make sure that their power rating is around 50 Watts or higher if you want maximum loudness. At maximum output you probably will also need to add a heatsink for the TPA3116D2. Note that there are other TPA3116D2 boards that come with a heat sink.

I used an ESP32 lite for this project but most other ESP32 boards will be fine as well. However, for best performance an ESP32-S3 board with PSRAM is recommended.

To play MP3 files from an SD Card you will furthermore need an SD Card and an SD Card reader module.

Finally, a breadboard and some cables will be handy for testing the circuit before soldering everything together.

TPA3116D2 Amplifier

PCM5102 DAC

2 x Speaker 8 Ohm 50 Watts

Heatsink 9x9x12mm

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 TPA3116D2 Module

The TPA3116D2 is a Class-D audio power amplifier chip. It uses a pulse-width modulation switching output stage. This reduces power loss and improves efficiency. You can buy the chip itself but it requires some additional resistors and capacitors, and we therefore use a TPA3116D2 Module that has this extra parts already integrated. The picture below shows the front and back of the TPA3116D2 Module.

Front and back of the TPA3116D2 Module
Front and back of the TPA3116D2 Module

You can see the TPA3116D2 chip in the center with a silvery square in the center that serves as a thermal pad.

Power Supply and Efficiency

The device operates over a supply voltage range from 4.5 V to 26 V. Efficiency can reach over 90 percent under optimal conditions. High efficiency reduces heat dissipation but for full power output you will need to add a heat sink!

Audio Performance

The amplifier provides low total harmonic distortion. THD+N values are typically below 0.1 percent at nominal power levels. The signal-to-noise ratio is high, often exceeding 100 dB. This ensures clean audio reproduction. The device supports a wide frequency response. It can handle audio signals from 20 Hz to 20 kHz.

Protection Features

The TPA3116D2 includes several protection mechanisms. Overcurrent protection prevents damage during short circuits. Thermal protection shuts down the device if temperature exceeds safe limits. Undervoltage lockout ensures stable operation during low supply conditions.

Control and Integration

The amplifier include a control pin for a mute function (CS). Startup and shutdown sequences are designed to minimize audible pops.

The input stage is compatible with line-level analog signals. This makes it suitable for use with DACs like the PCM5102.

Pinout of the TPA3116D2 Module

The picture below shows the pinout of the TPA3116D2 Module:

Pinout of the TPA3116D2 Module

You have CS (Chip Select) that you can use to mute the output by connecting it to ground. Then you have the power supply pins (GND, VCC), followed by the left and right channel output for the speakers. Finally there is the line-in input (INR, GND, INL) for the audio signal.

Technical Specification

The following table summarizes the technical specification of the TPA3116D2:

ParameterTypical Value / Range
Amplifier TypeClass-D
Supply Voltage4.5 V to 26 V
Output Power (Stereo)Up to 2 × 50 W
EfficiencyUp to 90%
Switching Frequency~400 kHz
THD+N< 0.1%
Signal-to-Noise Ratio> 100 dB
Frequency Response20 Hz to 20 kHz
Quiescent CurrentLow (few mA typical)
Protection FeaturesOCP, OTP, UVLO, DC protection

For more detailed technical information see the datasheet for the TPA3116D2 linked below:

Architecture of the TPA3116D2 Audio System

The TPA3116D2 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 TPA3116D2 amplifier:

Architecture of the TPA3116D2 Audio System
Architecture of the TPA3116D2 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 TPA3116D2 amplifies the analog signal to drive the two speakers with up to 50 Watts.

Connecting the TPA3116D2 with PCM5102 and ESP32

In this section we are connecting the TPA3116D2 Amplifier to the PCM5102 DAC and the ESP32 microcontroller. The PCM5102 converts a digital audio to an analog audio signal that then can be fed into the TPA3116D2 Amplifier. The picture below shows the complete wiring diagram:

Connecting TPA3116D2 to PCM5102 and ESP32
Connecting TPA3116D2 to PCM5102 and ESP32

We start by connecting the ESP32 to the PCM5102 via the I2S interface. You could configure different pins (see the code in later sections) but here I am using LCK=32, BCK=25 and DIN=33. The following table shows the complete connections between the ESP32 and the PCM5102 :

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 but it doesn’t do any harm. I strongly recommend that you read the Playing Audio with ESP32 and PCM5102A tutorial, 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 correctly!

Next we connect the TPA3116D2 Amplifier via line out to the PCM5102. The following table shows the connections you need to make:

TPA3116D2PCM5102
INLLROUT
GNDAGND
INRROUT

Note that the labelling of the pins of the PCM5102 is a bit confusing. The left channel output on the PCM5102 should be labelled “ROUT” but actually reads “LROUT”.

Also note that you will need a separate power supply for the TPA3116D2 that can deliver between 4.5V up to 24V and 3A .

You can leave the CS (Chip Select) pin unconnected. Connecting CS to GND would disable (mute) the TPA3116D2.

Polarity of Speakers

The right and left loudspeakers are connected to the SPR+, SPR-, SPL+ and SPL- pins of the TPA3116D2.

Connecting speakers to TPA3116D2
Connecting speakers to TPA3116D2

Make sure that you match the polarity of the output pins 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 produces forward cone motion.

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 an additional SD Card reader:

Connecting TPA3116D2 to PCM5102 and ESP32 with SD Card Reader
Connecting TPA3116D2 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 the Find I2C and SPI default pins tutorial.

Installing Libraries

We are going to use arduino-audio-tools library by Phil Schatzmann to build our 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.

Finally, 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.

In the next three sections I will show you the code to build a Bluetooth Player, an Internet Radio and an MP3 Player using the arduino-tools-library.

Code for a Bluetooth Player

This first code example configures an 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() {
  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 TPA3116D2 amplifier module to drive the 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.

Code for an Internet Radio

This next 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 TPA3116D2 to drive the 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.3   // Volume [0,1]

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 PCM5102A hardware interface. A VolumeStream object is layered on top of the I2S stream to apply digital 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. The incoming MP3-encoded audio data 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, ensuring uninterrupted playback as long as the stream remains available.

In the following a 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/"

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. The analog output of the DAC is then amplified by the TPA3116D2 and send to the 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 "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.5   // 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 PCM5102A. 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.

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, I showed you how to build a high-power 50 Watts audio system with an ESP32 and the TPA3116D2 amplifier. You 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 questions in the comment section.

Happy Tinkering ; )