-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESP32 I2S 24 bit data reading from INMP441 Mic and Frequency Mirroring #9663
Comments
Hi @raprakashvi, you may try the latest version 3.0.0-rc3 to use the new I2S library. There should be possible to setup I2S in 24 bit mode. Take a look at the examples so you can simply implement it. |
Hi @P-R-O-C-H-Y , thanks for sharing the link. Which library manager is the newer I2S library added to? I had to downgrade my Arduino core to 2.0.9 from 2.0.16 to remove noise at 1/3rd of the max frequency as discussed in another open issue. Meanwhile, is it possible to suggest improvements in the current code as for transferring data over wifi to a Python script would convert 24 bit to 16 bit again from what I have seen? Thanks. |
@raprakashvi new I2S library is included in 3.0.0 version which is now only in development release stage so you need to update your json link in Arduino IDE. Stable version of 3.0.0 is planned to be released next week. |
@raprakashvi The 3.0.0 stable have been release on Monday. Feel free to take a look and try the new I2S library. |
Hi all, posting here if anyone in future is struggling with the same problem. I was able to fix the frequency inversion problem and noise issue at 1/3rd of max frequency by updating to latest Arduino core 3.0, and reading the data as 32 bit. But if I tried to send it over wifi and read it on python client side, it did not work, so I had to use the RX transformation of ESP_I2S library to transform to 16 bit and then send it over. Below is my code for future reference. #include <WiFi.h>
#include "ESP_I2S.h"
#define I2S_WS 22
#define I2S_SD 12
#define I2S_SCK 21
#define BUFFER_LEN 512
static I2SClass i2s;
const char* ssid = "ADD"; // Replace with your SSID
const char* password = "PASS"; // Replace with your WiFi password
const int serverPort = 12345;
WiFiServer server(serverPort);
int16_t i2sBuffer[BUFFER_LEN]; // Adjusted to 16-bit buffer for new I2S library
void setup() {
Serial.begin(921600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the IP address to the serial monitor
server.begin();
Serial.println("Initializing I2S bus...");
// Set up the pins used for audio input
i2s.setPins(I2S_SCK, I2S_WS, -1, I2S_SD);
// Initialize the I2S bus in standard mode with transformation to 16-bit
if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO, I2S_STD_SLOT_LEFT)) {
Serial.println("Failed to initialize I2S bus!");
return;
}
// Configure the I2S receiver to transform 32-bit data to 16-bit data
if (!i2s.configureRX(16000, I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO, I2S_RX_TRANSFORM_32_TO_16)) {
Serial.println("Failed to configure I2S RX transformation!");
return;
}
Serial.println("I2S bus initialized and configured.");
}
void loop() {
WiFiClient client = server.available();
if (client) {
client.println("ready");
while (client.connected()) {
size_t bytes_read = i2s.readBytes((char *)i2sBuffer, sizeof(i2sBuffer));
if (bytes_read > 0) {
// Send the 16-bit data over WiFi
client.write((const char *)i2sBuffer, bytes_read);
} else {
Serial.println("No bytes read, trying again...");
delay(100); // Small delay to avoid flooding the Serial Monitor
}
}
}
} |
That is the exact reason why we added the transform methods :) |
Board
ESP WROOM 32
Device Description
Regular ESP32 development board
Hardware Configuration
#define I2S_WS 22
#define I2S_SD 12
#define I2S_SCK 21
#define I2S_PORT I2S_NUM_0
#define BUFFER_LEN 1024
I2S
Version
v2.0.9
IDE Name
Arduino
Operating System
Windows 10
Flash frequency
80 Mhz
PSRAM enabled
no
Upload speed
921600
Description
Hi all,
I am working on a project to capture environmental noise and through the help of community have made decent progress. Currently, I am able to capture acceptable quality of sound using INMP441 mic. The problem comes when I plot the FFT. I am observing frequency mirroring along max_freq/2 , in my case 4000 Hz as my max frequency is 8000 Hz.
As of now , I am reading data in 16 bit format, even though the microphone datasheet says it is 24 bit. In 16 bit I am able to record sound but the issue persists. My question is, how can I modify my code to accept 24 bit data?
Below are two suggestions 1,2, which boils down to recording data as 32 bit, and then dropping the pad byte (or last byte). Would very much appreciate any help here. I have exhausted my limits and still getting 0 value when I try 32 bit or no value.
I was able to fix presence of noise at 1/3rd max frequency by downgrading Arduino Core to 2.0.9 from 2.0.16 as mentioned in another issue but to fix the frequency, apparently I need to figure out the 24 bit part which I am unable to do and reaching out after many tests.
Sketch
Debug Message
Other Steps to Reproduce
Once you run this code, you can use a python script to read the data and reproduce it:
,
For this case, the spectrogram generated will be mirrored at 4000 Hz.
I have checked existing issues, online documentation and the Troubleshooting Guide
The text was updated successfully, but these errors were encountered: