Advertisement
atuline

INMP441.ino

Dec 10th, 2020 (edited)
5,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. /*
  2.  * ESP32 I2S Noise Level Example.
  3.  *
  4.  * By: maspetsberger
  5.  *
  6.  * Updated by: Andrew Tuline
  7.  *
  8.  * This example calculates a mean noise level.
  9.  *
  10.  * Tie the L/R pin to ground. Other pins are listed below.
  11.  * VDD goes to 3.3V
  12.  * GND goes to Gnd
  13.  *
  14.  */
  15.  
  16. #include <driver/i2s.h>
  17.  
  18. #define I2S_WS  15         // aka LRCL
  19. #define I2S_SD  32         // aka DOUT
  20. #define I2S_SCK 14         // aka BCLK
  21.  
  22. const i2s_port_t I2S_PORT = I2S_NUM_0;
  23. const int BLOCK_SIZE = 64;
  24. const int SAMPLE_RATE = 10240;
  25.  
  26. float mean = 0;
  27. bool INMP_flag = 0;
  28.  
  29. void setup() {
  30.  
  31.   Serial.begin(115200);
  32.  
  33.   Serial.println("Configuring I2S...");
  34.   esp_err_t i2s_err;
  35.  
  36.   // The I2S config as per the example
  37.   const i2s_config_t i2s_config = {
  38.       .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // Receive, not transfer
  39.       .sample_rate = SAMPLE_RATE,                         // 16KHz
  40.       .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // could only get it to work with 32bits
  41.       .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // LEFT when pin is tied to ground.
  42.       .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
  43.       .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,     // Interrupt level 1
  44.       .dma_buf_count = 8,                           // number of buffers
  45.       .dma_buf_len = BLOCK_SIZE                     // samples per buffer
  46.   };
  47.  
  48.   // The pin config as per the setup
  49.   const i2s_pin_config_t pin_config = {
  50.       .bck_io_num = I2S_SCK,       // BCLK aka SCK
  51.       .ws_io_num = I2S_WS,        // LRCL aka WS
  52.       .data_out_num = -1,         // not used (only for speakers)
  53.       .data_in_num = I2S_SD       // DOUT aka SD
  54.   };
  55.  
  56.   // Configuring the I2S driver and pins.
  57.   // This function must be called before any I2S driver read/write operations.
  58.   i2s_err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  59.   if (i2s_err != ESP_OK) {
  60.     Serial.printf("Failed installing driver: %d\n", i2s_err);
  61.     while (true);
  62.   }
  63.   i2s_err = i2s_set_pin(I2S_PORT, &pin_config);
  64.   if (i2s_err != ESP_OK) {
  65.     Serial.printf("Failed setting pin: %d\n", i2s_err);
  66.     while (true);
  67.   }
  68.   Serial.println("I2S driver installed.");
  69.  
  70.   delay(1000);                        // Enough time to see these messages. This need to be BEFORE the INMP test.
  71.  
  72.   getINMP();
  73.   if (mean != 0.0) {
  74.     Serial.println("INMP is present.");
  75.     INMP_flag = 1;
  76.   }
  77.  
  78. } // setup()
  79.  
  80.  
  81.  
  82. void loop() {
  83.  
  84.   if (INMP_flag) {
  85.     getINMP();
  86.     Serial.print(abs(mean)); Serial.print(" ");
  87.     Serial.println(1600);
  88.    
  89.   } else {
  90.     // Analog Read here!!
  91.   }
  92.  
  93. } // loop()
  94.  
  95.  
  96.  
  97. void getINMP() {
  98.   // Read multiple samples at once and calculate the sound pressure
  99.   int32_t samples[BLOCK_SIZE];
  100.   int num_bytes_read = i2s_read_bytes(I2S_PORT,
  101.                                       (char *)samples,
  102.                                       BLOCK_SIZE,     // the doc says bytes, but its elements.
  103.                                       portMAX_DELAY); // no timeout
  104.  
  105.   int samples_read = num_bytes_read / 8;
  106.   if (samples_read > 0) {
  107.  
  108.     for (int i = 0; i < samples_read; ++i) {
  109.       mean += samples[i];
  110.     }
  111.     mean = mean/BLOCK_SIZE/16384;
  112.   }  
  113. }
  114.  
  115. // actually we would need to call `i2s_driver_uninstall(I2S_PORT)` upon exit.
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement