Repository: atomic14/esp32-i2s-mic-test Branch: main Commit: 84207ccc59f8 Files: 3 Total size: 4.4 KB Directory structure: gitextract_h1p6dho9/ ├── .github/ │ └── FUNDING.yml ├── README.md └── i2s_mic_test/ └── i2s_mic_test.ino ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/FUNDING.yml ================================================ # These are supported funding model platforms github: [atomic14] ko_fi: atomic14 ================================================ FILE: README.md ================================================ # The Simplest Test Code for an I2S Microphone on the ESP32 I can Imagine [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/Z8Z734F5Y) I've got a lot of audio projects. And I've tried to make these all available on GitHub. You can see all my projects here: [atomic14](https://www.youtube.com/channel/UC4Otk-uDioJN0tg6s1QO9lw) - please take a look and subscribe if you like them! Quite a few of the issues I get on the projects seem to boil down to people having problems with their microphones. This is understandable - there's a lot to take in on some of the projects as they are pretty complicated. It can be hard to tell if the problem is with the microphone, the code, or something completely unrelated. Without actually recording and playing back the audio, it's hard to know what the problem is. It's easy if you have an amplifier and speaker - but that's just adding another failure point. Is it the microphone or the speaker that is the problem? We can play audio directly back via the ADC through headphones - but again, this is another failure point. I think the most minimal thing we can do is to use the serial plotter in Arduino. This should work for everyone without any issues and is a very simple test. Open this sketch up using the Arduino IDE and hit run. Now go to Tools->Serial Plotter. There's only a few lines of code that you will need to change: ```c++ #define I2S_MIC_SERIAL_CLOCK GPIO_NUM_26 #define I2S_MIC_LEFT_RIGHT_CLOCK GPIO_NUM_22 #define I2S_MIC_SERIAL_DATA GPIO_NUM_21 ``` # Wiring up the INMP441 This is a popular cheap microphone with readily available breakout board |INMP441 | ESP32| Info| |---|---|---| |VDD|3v3|Power - DO NOT USE 5V!| |GND|GND|GND| |L/R|GND|Left channel or right channel| |WS|22|Left right clock| |SCK|26|Serial clock| |SD|21|Serial data| # Wiring up the ICS-43434 This is a better microphone (IMHO) with a less popular breakout board that I sell... [Available Here](https://www.tindie.com/products/21519/) |ICS43434 | ESP32| Info| |---|---|---| |VDD|3v3|Power - DO NOT USE 5V!| |GND|GND|GND| |LR|GND|Left channel or right channel| |WS|22|Left right clock| |SCK|26|Serial clock| |SD|21|Serial data| Feel free to change the pins - but make sure you adjust the code to match. You should see a waveform that looks like this if you whistle (if you can't whistle - try screaming 😱). ![Whistling](./images/whistling.png) Once you've got your microphone up and running, here are some projects that you might enjoy: [Audio Project Playlist](https://www.youtube.com/playlist?list=PL5vDt5AALlRfGVUv2x7riDMIOX34udtKD) ================================================ FILE: i2s_mic_test/i2s_mic_test.ino ================================================ #include // you shouldn't need to change these settings #define SAMPLE_BUFFER_SIZE 512 #define SAMPLE_RATE 8000 // most microphones will probably default to left channel but you may need to tie the L/R pin low #define I2S_MIC_CHANNEL I2S_CHANNEL_FMT_ONLY_LEFT // either wire your microphone to the same pins or change these to match your wiring #define I2S_MIC_SERIAL_CLOCK GPIO_NUM_32 #define I2S_MIC_LEFT_RIGHT_CLOCK GPIO_NUM_25 #define I2S_MIC_SERIAL_DATA GPIO_NUM_33 // don't mess around with this i2s_config_t i2s_config = { .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), .sample_rate = SAMPLE_RATE, .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, .communication_format = I2S_COMM_FORMAT_I2S, .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, .dma_buf_count = 4, .dma_buf_len = 1024, .use_apll = false, .tx_desc_auto_clear = false, .fixed_mclk = 0}; // and don't mess around with this i2s_pin_config_t i2s_mic_pins = { .bck_io_num = I2S_MIC_SERIAL_CLOCK, .ws_io_num = I2S_MIC_LEFT_RIGHT_CLOCK, .data_out_num = I2S_PIN_NO_CHANGE, .data_in_num = I2S_MIC_SERIAL_DATA}; void setup() { // we need serial output for the plotter Serial.begin(115200); // start up the I2S peripheral i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); i2s_set_pin(I2S_NUM_0, &i2s_mic_pins); } int32_t raw_samples[SAMPLE_BUFFER_SIZE]; void loop() { // read from the I2S device size_t bytes_read = 0; i2s_read(I2S_NUM_0, raw_samples, sizeof(int32_t) * SAMPLE_BUFFER_SIZE, &bytes_read, portMAX_DELAY); int samples_read = bytes_read / sizeof(int32_t); // dump the samples out to the serial channel. for (int i = 0; i < samples_read; i++) { Serial.printf("%ld\n", raw_samples[i]); } }