In my Arduino ESP32-A2DP library I am providing some very simple examples that show how to transform the ESP32 into a A2DP source and transmit sound to a Bluetooth Sink (e.g. some Bluetooth Speakers).
I had quite some questions on how to do this with files, microphones and I2S as input. So I started a small sister project which provides some additional sound tools. I plan to provide plenty of additional examples for some more advances scenarios.
Analog Microphones
The Sketch
Here is an first example sketch that you can use with an analog microphone as sound source:
#include "Arduino.h"
#include "BluetoothA2DPSource.h"
#include "AudioTools.h"
using namespace audio_tools;
/**
* @brief We use a mcp6022 analog microphone as input and send the data to A2DP
*/
ADC adc;
BluetoothA2DPSource a2dp_source;
// The data has a center of around 26427, so we we need to shift it down to bring the center to 0
FilterScaler<int16_t> scaler(1.0, -26427, 32700 );
// callback used by A2DP to provide the sound data
int32_t get_sound_data(Channels* data, int32_t len) {
arrayOf2int16_t *data_arrays = (arrayOf2int16_t *) data;
// the ADC provides data in 16 bits
size_t result_len = adc.read(data_arrays, len);
scaler.process(data_arrays, result_len);
return result_len;
}
// Arduino Setup
void setup(void) {
Serial.begin(115200);
// start i2s input with default configuration
Serial.println("starting I2S-ADC...");
adc.begin(adc.defaultConfig());
// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.start("MyMusic", get_sound_data);
}
// Arduino loop - repeated processing
void loop() {
}
I am using the built in ESP32 analog sampling functionality which is available using I2S. For this I provide the ADC class to make it simple to use. The data from the microphone was centered around 26427, so we need to reduce all values by this. This is done with the help of the FilterScaler. You could also amplify the sound (make the sound louder) by using a factor > 1.0
The Hardware
To test it I am using the tiny MCP6022 microphone module.
The MCP6022 is a analaog microphone which operates at 3.3 V
Pin Assignments
MCP6022 | ESP32 |
---|---|
VCC | 3.3 |
GND | GND |
OUT | GPIO34 |
8 Comments
Philip · 21. February 2023 at 16:19
Hello , is it possible to send a stream from the ESP to a mobile phone with a mic source and at the same time receive an audio stream from the mobile phone as an line out output?
pschatzmann · 21. February 2023 at 18:03
See https://github.com/pschatzmann/ESP32-A2DP/discussions/283
mohadese · 1. December 2022 at 13:04
Thanks for posting this topic. I wanted to ask for github address of “ADC Class” and ” FilterScaler” .
pschatzmann · 1. December 2022 at 13:09
Just click on the links in the blog and you will be forwarded to the AudioTools project
mohadese · 1. December 2022 at 13:48
Thanks a lot…
Is it possible to reduce the Nyquist frequency and sampling rate (I have a low freq sound) and still create a .wav , .mp3, or other formats?
pschatzmann · 1. December 2022 at 13:53
I am afraid that your question does not make any sense to me in this context.
I suggest to open an discussion on Github
Taku · 7. September 2022 at 12:10
Thank you for the very interesting post! I would love to use your sketch for my project. Where can I find the ADC class and the FilterScale class (in your arduino-audio-tools library)?
pschatzmann · 11. September 2022 at 21:29
This example is pretty old and a lot has changed in the meantime. Pleased use the (up to date) example from github.