我有一个 DOIT ESP32-DEVKIT 和一个 DFRobot I2S MEMS 麦克风。 当麦克风和 ESP32 连接时,串行绘图仪应该有如下图所示的输出: 根据官方文档预期麦克风输出 然而,当我将麦克风连接到 ESP32 时,我看到的唯一输出是波动的 0 和 -1。 当前麦克风输出 它(非常零星地)飙升至 2000 或 -2000 之类的值。
麦克风根本不响应任何音频输入。
下图是当前麦克风与 ESP32 的连接情况 当前引脚设置
由于文档描述了使用不同类型的 ESP32,我尝试将引脚连接到 ESP32 上的类似引脚。我还购买了第二个(相同的)麦克风,但是使用这个麦克风时,它具有相同的结果。
我使用的代码(与文档中使用的代码基本相同,但定义了不同的引脚)。
DFrobot_MSM261.h 是文档提供的库,我已经安装了。
在嵌入式系统方面,我不是最精明的人,所以我担心我错过了一些非常明显的东西。
此时,我担心我的两个麦克风都烧坏了,或者它们与我的 ESP32 不兼容。
#define SAMPLE_RATE (44100)
#define I2S_SCK_IO (18)
#define I2S_WS_IO (25)
#define I2S_DI_IO (32)
#define DATA_BIT (32)
#define MODE_PIN (19)
DFRobot_Microphone microphone(I2S_SCK_IO, I2S_WS_IO, I2S_DI_IO);
char i2sReadrawBuff[100];
void setup() {
Serial.begin(115200);
pinMode(MODE_PIN,OUTPUT);
digitalWrite(MODE_PIN,LOW); // set LEFT channel
// digitalWrite(MODE_PIN,HIGH); // set RIGHT channel
while(microphone.begin(SAMPLE_RATE, DATA_BIT) != 0){
Serial.println(" I2S init failed");
}
Serial.println("I2S init success");
}
void loop() {
microphone.read(i2sReadrawBuff,100);
Serial.println((int16_t)(i2sReadrawBuff[2]|i2sReadrawBuff[3]<<8)); // read LEFT channel
// Serial.println((int16_t)(i2sReadrawBuff[0]|i2sReadrawBuff[1]<<8)); // read RIGHT channel
delay(100);
}
我从未使用过该库,但我以前使用过MEMS麦克风。就我而言,我只是使用了 i2s 库,所以我建议您至少尝试一下。
我通常用来测试麦克风的代码如下,您需要根据您的项目调整引脚:
/**
* ESP32 I2S Serial Plotter Example.
*/
#include <driver/i2s.h>
const i2s_port_t I2S_PORT = I2S_NUM_0;
const int BLOCK_SIZE = 1024;
void setup() {
Serial.begin(115200);
Serial.println("Configuring I2S...");
esp_err_t err;
// The I2S config as per the example
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // Receive, not transfer
.sample_rate = 16000, // 16KHz
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // could only get it to work with 32bits
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // although the SEL config should be left, it seems to transmit on right
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // Interrupt level 1
.dma_buf_count = 4, // number of buffers
.dma_buf_len = 8 // 8 samples per buffer (minimum)
};
// The pin config as per the setup
const i2s_pin_config_t pin_config = {
.bck_io_num = 26, // BCKL
.ws_io_num = 33, // LRCL
.data_out_num = -1, // not used (only for speakers)
.data_in_num = 32 // DOUT
};
// Configuring the I2S driver and pins.
// This function must be called before any I2S driver read/write operations.
err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
if (err != ESP_OK) {
Serial.printf("Failed installing driver: %d\n", err);
while (true);
}
err = i2s_set_pin(I2S_PORT, &pin_config);
if (err != ESP_OK) {
Serial.printf("Failed setting pin: %d\n", err);
while (true);
}
Serial.println("I2S driver installed.");
}
void loop() {
// Read multiple samples at once and calculate the sound pressure
int32_t samples[BLOCK_SIZE];
int num_bytes_read = i2s_read_bytes(I2S_PORT,
(char *)samples,
BLOCK_SIZE, // the doc says bytes, but its elements.
portMAX_DELAY); // no timeout
int samples_read = num_bytes_read / 8;
if (samples_read > 0) {
float mean = 0;
for (int i = 0; i < samples_read; ++i) {
mean += (samples[i] >> 14);
}
mean /= samples_read;
float maxsample = -1e8, minsample = 1e8;
for (int i = 0; i < samples_read; ++i) {
minsample = min(minsample, samples[i] - mean);
maxsample = max(maxsample, samples[i] - mean);
}
Serial.println(maxsample - minsample);
}
}
// actually we would need to call `i2s_driver_uninstall(I2S_PORT)` upon exit.