I2S does not work in STEREO with INMP441
Posted: Sun Nov 12, 2023 8:03 pm
I connected two microphones according to the following scheme. Both microphones work in mono format. When I try to get a signal in stereo format (RIGHT_LFET), the useful signal is only on the left channel ( x = 0, 2, 4 .... 418), and random values are received from the right channel (x = 1,3...419). It is also unclear to me why half (x = 420...819) the read values are 0, in any I2S format. Because of this, in stereo mode, there is only 820/4 per channel.
The main problem is how to make INMP441 work in stereo format ?
The main problem is how to make INMP441 work in stereo format ?
Code: Select all
#include <Arduino.h>
#include "driver/i2s.h"
#include <cmath>
#define I2S_WS_1 19
#define I2S_SD_1 21
#define I2S_SCK_1 18
static uint32_t sample_buffer_size = 820;
static uint32_t sampling_rate = 44100;
static signed short sampleBuffer_1[sample_buffer_size];
static int i2s_init(int pin_SCK, int pin_WS, int pin_SD, int port ,i2s_channel_fmt_t Format )
{
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX ),
.sample_rate = sampling_rate,
.bits_per_sample = (i2s_bits_per_sample_t)16,
.channel_format = Format,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.dma_buf_count = 4,
.dma_buf_len = sample_buffer_size,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = -1,
};
i2s_pin_config_t pin_config = {
.bck_io_num = pin_SCK, // IIS_SCLK
.ws_io_num = pin_WS, // IIS_LCLK
.data_out_num = -1, // IIS_DSIN
.data_in_num = pin_SD, // IIS_DOUT
};
esp_err_t ret = 0;
ret = i2s_driver_install((i2s_port_t)port, &i2s_config, 0, NULL);
if (ret != ESP_OK)
{
Serial.println("Error in i2s_driver_install");
}
ret = i2s_set_pin((i2s_port_t)port, &pin_config);
if (ret != ESP_OK)
{
Serial.println("Error in i2s_set_pin");
}
ret = i2s_zero_dma_buffer((i2s_port_t)port);
if (ret != ESP_OK)
{
Serial.println("Error in initializing dma buffer with 0");
}
return int(ret);
}
void i2s_microphone1(void *arg)
{
if (i2s_init(I2S_SCK_1, I2S_WS_1, I2S_SD_1, 1, I2S_CHANNEL_FMT_RIGHT_LEFT))
Serial.println("Failed to start I2S_1!");
const uint16_t i2s_bytes_to_read = sample_buffer_size;
size_t bytes_read;
while (true)
{
bytes_read = 0;
i2s_read((i2s_port_t)1, (void *)sampleBuffer_1, i2s_bytes_to_read, &bytes_read, portMAX_DELAY);
for (int x = 0; x < bytes_read; x+=2)
{
if (abs(sampleBuffer_1[x]) > 1000 )
Serial.println("channel left = " + (String)sampleBuffer_1[x] + " channel right = " + (String)sampleBuffer_1[x+1] );
}
}
}
void setup()
{
Serial.begin(115200);
xTaskCreate(i2s_microphone1, "I2S_microphone_1", 1024, (void *)sample_buffer_size, 1, NULL);
}
void loop()
{
vTaskDelete(NULL);
}