After many tries it worked to connect digital mic and digital out to esp32 using two i2s interfaces.

Microphone circuit is sph0645 and output is max 98357a.

Below is a description of what pins i used and simple code to direct input from mic to output.

Using 4 pins to microphone in: SEL to VIN

22 – dout

25 – bclk

26 – lrcl

Output pins:

18 – bclk

19 – lrc

21 – din

#include <Arduino.h>
#include <driver/i2s.h>

#define I2S_SAMPLE_RATE   (44100)
#define I2S_SAMPLE_BITS   (16)
#define I2S_NUM_CHANNELS  (1)
#define I2S_DMA_BUFFER    (2048)
#define I2S_LRCK_PIN      (26)
#define I2S_BCLK_PIN      (25)
#define I2S_DOUT_PIN      (22)

void setup() {
  Serial.begin(115200);


  i2s_config_t i2s_config = {
   .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
  .sample_rate = I2S_SAMPLE_RATE,
  .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
  .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S),
  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  .dma_buf_count = 32,
  .dma_buf_len = 512,
  .use_apll = false
  };
  i2s_pin_config_t pin_config = {
    .bck_io_num = I2S_BCLK_PIN,
    .ws_io_num = I2S_LRCK_PIN,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = I2S_DOUT_PIN
  };
  i2s_driver_install((i2s_port_t)I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_pin((i2s_port_t)I2S_NUM_0, &pin_config);

 
  i2s_config_t i2s_config2 = {
   .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
  .sample_rate = I2S_SAMPLE_RATE,
  .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
  .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  .communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S),
  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  .dma_buf_count = 32,
  .dma_buf_len = 512,
  .use_apll = false
  };
  i2s_pin_config_t pin_config2 = {
    .bck_io_num = 18,
    .ws_io_num = 19,
    .data_out_num = 21,
    .data_in_num = I2S_PIN_NO_CHANGE
  };
  i2s_driver_install((i2s_port_t)I2S_NUM_1, &i2s_config2, 0, NULL);
  i2s_set_pin((i2s_port_t)I2S_NUM_1, &pin_config2);

  Serial.println("Mikrofon jest gotowy");




}

void loop() {
   // Bufor odczytu I2S
  static const int I2S_BUFLEN = 512;
  //static int16_t i2s_buf[I2S_BUFLEN];
  //size_t bytes_read = 0;
  size_t bytes_read;
  size_t bytes_w;
  uint8_t i2s_read_buffer[512];

  // Odczytanie danych z mikrofonu
  i2s_read(I2S_NUM_0, &i2s_read_buffer, 512, &bytes_read, portMAX_DELAY);
  i2s_write(I2S_NUM_1, &i2s_read_buffer, bytes_read,&bytes_w, portMAX_DELAY);

}

Podobne wpisy

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *