- Saved searches
- Use saved searches to filter your results more quickly
- License
- ytdec/esp32-bluetooth-receiver
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Using an ESP32 and PCM5102 as a Bluetooth audio receiver
- Share this:
- Related
- Saved searches
- Use saved searches to filter your results more quickly
- mesopa/ESP32-Bluetooth-Audio-Receiver-PCM5102
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Enable ESP32 as a bluetooth receiver
License
ytdec/esp32-bluetooth-receiver
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Demo of A2DP audio sink role
This is the demo of API implementing Advanced Audio Distribution Profile to receive an audio stream.
This example involves the use of Bluetooth legacy profile A2DP for audio stream reception, AVRCP for media information notifications, and I2S for audio stream output interface.
Applications such as bluetooth speakers can take advantage of this example as a reference of basic functionalities.
To play the sound, there is a need of loudspeaker and possibly an external I2S codec. Otherwise the example will only show a count of audio data packets received silently. Internal DAC can be selected and in this case external I2S codec may not be needed.
For the I2S codec, pick whatever chip or board works for you; this code was written using a PCM5102 chip, but other I2S boards and chips will probably work as well. The default I2S connections are shown below, but these can be changed in menuconfig:
ESP pin | I2S signal |
---|---|
GPIO22 | LRCK |
GPIO25 | DATA |
GPIO26 | BCK |
If the internal DAC is selected, analog audio will be available on GPIO25 and GPIO26. The output resolution on these pins will always be limited to 8 bit because of the internal structure of the DACs.
- Set serial port under Serial Flasher Options.
- Set the use of external I2S codec or internal DAC for audio output, and configure the output PINs under A2DP Example Configuration
- Enable Classic Bluetooth and A2DP under Component config —> Bluetooth —> Bluedroid Enable
Build the project and flash it to the board, then run monitor tool to view serial output.
(To exit the serial monitor, type Ctrl-] .)
After the program is started, the example starts inquiry scan and page scan, awaiting being discovered and connected. Other bluetooth devices such as smart phones can discover a device named «ESP_SPEAKER». A smartphone or another ESP-IDF example of A2DP source can be used to connect to the local device.
Once A2DP connection is set up, there will be a notification message with the remote device’s bluetooth MAC address like the following:
I (106427) BT_AV: A2DP connection state: Connected, [64:a2:f9:69:57:a4]
If a smartphone is used to connect to local device, starting to play music with an APP will result in the transmission of audio stream. The transmitting of audio stream will be visible in the application log including a count of audio data packets, like this:
I (120627) BT_AV: A2DP audio state: Started I (122697) BT_AV: Audio packet count 100 I (124697) BT_AV: Audio packet count 200 I (126697) BT_AV: Audio packet count 300 I (128697) BT_AV: Audio packet count 400
Also, the sound will be heard if a loudspeaker is connected and possible external I2S codec is correctly configured. For ESP32 A2DP source example, the sound is noise as the audio source generates the samples with a random sequence.
- For current stage, the supported audio codec in ESP32 A2DP is SBC. SBC data stream is transmitted to A2DP sink and then decoded into PCM samples as output. The PCM data format is normally of 44.1kHz sampling rate, two-channel 16-bit sample stream. Other decoder configurations in ESP32 A2DP sink is supported but need additional modifications of protocol stack settings.
- As a usage limitation, ESP32 A2DP sink can support at most one connection with remote A2DP source devices. Also, A2DP sink cannot be used together with A2DP source at the same time, but can be used with other profiles such as SPP and HFP.
About
Enable ESP32 as a bluetooth receiver
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.
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)
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.
After that is done, we can connect the PCM5102 to the ESP32. If all is soldered well, it should look like this:
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.
#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…
- your cable is not connected or broken.
- you might not have selected the right board
- your ESP32 is not in flashing/uploading mode.
- 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.
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.
Share this:
Related
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
mesopa/ESP32-Bluetooth-Audio-Receiver-PCM5102
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Demo of A2DP audio sink role
This is the demo of API implementing Advanced Audio Distribution Profile to receive an audio stream.
This example involves the use of Bluetooth legacy profile A2DP for audio stream reception, AVRCP for media information notifications, and I2S for audio stream output interface.
Applications such as bluetooth speakers can take advantage of this example as a reference of basic functionalities.
To play the sound, there is a need of loudspeaker and possibly an external I2S codec. Otherwise the example will only show a count of audio data packets received silently. Internal DAC can be selected and in this case external I2S codec may not be needed.
For the I2S codec, pick whatever chip or board works for you; this code was written using a PCM5102 chip, but other I2S boards and chips will probably work as well. The default I2S connections are shown below, but these can be changed in menuconfig:
ESP pin | I2S signal |
---|---|
GPIO22 | LRCK |
GPIO25 | DATA |
GPIO26 | BCK |
If the internal DAC is selected, analog audio will be available on GPIO25 and GPIO26. The output resolution on these pins will always be limited to 8 bit because of the internal structure of the DACs.
- Set the use of external I2S codec or internal DAC for audio output, and configure the output PINs under A2DP Example Configuration
- Enable Classic Bluetooth and A2DP under Component config —> Bluetooth —> Bluedroid Enable
Build the project and flash it to the board, then run monitor tool to view serial output.
idf.py -p PORT flash monitor
(To exit the serial monitor, type Ctrl-] .)
After the program is started, the example starts inquiry scan and page scan, awaiting being discovered and connected. Other bluetooth devices such as smart phones can discover a device named «ESP_SPEAKER». A smartphone or another ESP-IDF example of A2DP source can be used to connect to the local device.
Once A2DP connection is set up, there will be a notification message with the remote device’s bluetooth MAC address like the following:
I (106427) BT_AV: A2DP connection state: Connected, [64:a2:f9:69:57:a4]
If a smartphone is used to connect to local device, starting to play music with an APP will result in the transmission of audio stream. The transmitting of audio stream will be visible in the application log including a count of audio data packets, like this:
I (120627) BT_AV: A2DP audio state: Started I (122697) BT_AV: Audio packet count 100 I (124697) BT_AV: Audio packet count 200 I (126697) BT_AV: Audio packet count 300 I (128697) BT_AV: Audio packet count 400
Also, the sound will be heard if a loudspeaker is connected and possible external I2S codec is correctly configured. For ESP32 A2DP source example, the sound is noise as the audio source generates the samples with a random sequence.
- For current stage, the supported audio codec in ESP32 A2DP is SBC. SBC data stream is transmitted to A2DP sink and then decoded into PCM samples as output. The PCM data format is normally of 44.1kHz sampling rate, two-channel 16-bit sample stream. Other decoder configurations in ESP32 A2DP sink is supported but need additional modifications of protocol stack settings.
- As a usage limitation, ESP32 A2DP sink can support at most one connection with remote A2DP source devices. Also, A2DP sink cannot be used together with A2DP source at the same time, but can be used with other profiles such as SPP and HFP.