Using an ESP32 and PCM5102 as a Bluetooth audio receiver

I wanted to upgrade my built-in car stereo with a bluetooth receiver, for streaming audio from my mobile phone. So I built one myself using the line-in port on my stereo that’s normally used for the CD-changer. In this case, I am using the ESP32 as a Bluetooth receiver and the PCM5102 as a DAC module for converting digital audio to an analog stereo signal. Advanced Audio Distribution Profile (A2DP) is a Bluetooth profile that allows hi-fi sound to be transmitted over a wireless Bluetooth connection. The connection between the ESP32 and the PCM5102 is based upon I2S (Inter-IC Sound) an electrical serial bus interface standard used for connecting digital audio devices together.

So what you need is:

  • PCM5102
  • ESP32 (In this example, I’ll be using the ESP32S)

While ordering the ESP32’s I thought it was handy if there were already pins attached as it speeds up the building process (less soldering). But in this case, it might have been handier if no pins were soldered in the first place. I wanted to make a compact build (keeping the boards close together), as it needs to fit in my car (a Subaru Forrester). So I desoldered some pins from the ESP32 to make space for the PCM5102 board. I did this using a solder sucker (a.k.a. desoldering pump).

Steps for removing the pins:

  • heat up your soldering iron,
  • heat up the pins on your ESP32
  • pump the solder away.
  • bend the pin 90 degrees using a nose plyer
  • while heating your pin from above, you can extract the pin using the nose plyer from below
  • use heat to extract the pin, don’t use your strength (using force might damage your ESP32)
pin bend 90 degrees using a nose plyer
heating the pin from above, extracting the pin using the nose plyer from below

Now the necessary pins on the ESP32 are removed, you can focus on the PCM5102. First, we need to close the SCK bridge on the PCB by soldering a small blob on the PCM5102.

(red circle) closing the SCK bridge on the PCB by soldering a small blob on the PCM5102

After that is done, we can connect the PCM5102 to the ESP32. If all is soldered well, it should look like this:

PCM5102 soldered to the ESP32

We need some code to program the ESP32 to make it all work. For that job, I am using the Arduino IDE to program the ESP32. So make sure you have the Arduino IDE and git installed on your machine. And before we can start, we also need to install the necessary libraries, so let’s go to your terminal window to install the libraries. You might want to copy and paste the following commands.
$ cd ~/Documents/Arduino/libraries
$ git clone https://github.com/pschatzmann/ESP32-A2DP

Next, start up your Arduino IDE and select the proper ESP32 board in Arduino IDE (go to Tools, Board, Board Manager, ESP 32 Arduino, and select the proper lidevice). In my case, I choose the “ESP32 Wrover Module”. Now we can use the following code to program your ESP32.

Arduino IDE with code for the ESP32 and PCM5102
#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

void setup() {
    i2s_pin_config_t my_pin_config = {
        .bck_io_num = 4,
        .ws_io_num = 15,
        .data_out_num = 2,
        .data_in_num = I2S_PIN_NO_CHANGE
    };
    a2dp_sink.set_pin_config(my_pin_config);
    a2dp_sink.start("MySubie");
}

void loop() {
}

Now click on the upload button on top of the Arduino IDE. The code will be sent to your ESP32. When finished you should see the “Done uploading” message.

However, if you do get the error:

Failed to connect to ESP32: Timed out… Connecting…

It means that:

  • your cable is not connected or broken.
  • you might not have selected the right board
  • your ESP32 is not in flashing/uploading mode.

To solve the latter:

  • Hold down the “BOOT” button on your ESP32 board
  • When you see the “Connecting….” message in your Arduino IDE, release your finger from the “BOOT” button.
Working ESP32 and PCM5102 as a Bluetooth audio receiver from above
Working ESP32 and PCM5102 as a Bluetooth audio receiver from aside

I really do love the small form factor of the ESP32 in combination with the PCM5102!

Suggestions for improving this article are welcome, please let me know and drop me a line.