Page 1 of 1

ESP32-S3 PDM Microphone

Posted: Thu Aug 21, 2025 3:36 pm
by geoffr
I'm trying to use a PDM microphone with the ESP32-S3 and arduino-esp32 library. I know I am reading microphone data (as it reacts to me blowing into the microphone), however I'm consistently getting negative values interspersed into the I2S output data. Has anyone got PDM working that can see where my error is..?
Why am I seeing negative values at all (should be gated by the I2S.available() check)? And why am I seeing more than -1 (it returns -1 if nothing..?)

Code:

Code: Select all

void setup() {

  // setup 42 PDM clock and 41 PDM data pins
  I2S.setPinsPdmRx(42, 41);

  // start I2S at 16 kHz with 16-bits per sample
  if (!I2S.begin(I2S_MODE_PDM_RX, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO)) {
    Serial.println("Failed to initialize I2S!");
    while (1); // do nothing
  }

}

void loop() {

  if (I2S.available() > 0) {
  // Read data from microphone
  int8_t sample = I2S.read(); // -1 if no data is available or an error occurred.
  Serial.println(sample);
  }

}
Sample Output:

Code: Select all

-17
-14
-11
-6
-3
4
9
14
20
23
28
29
30

Re: ESP32-S3 PDM Microphone

Posted: Sat Aug 23, 2025 6:47 pm
by MicroController
Try

Code: Select all

int sample = I2S.read();

Re: ESP32-S3 PDM Microphone

Posted: Sun Aug 24, 2025 1:32 pm
by geoffr
Sure, changing to int means no negative numbers but I get samples in the ~6553x range which is not a valid sample (It's not that noisy!).

If you look at the definition of read() it's "Read the next available byte from the I2S interface." So, it's only 8 bits which ~65553x is definitely not.
( https://docs.espressif.com/projects/ard ... i/i2s.html )

Re: ESP32-S3 PDM Microphone

Posted: Sun Aug 24, 2025 2:31 pm
by MicroController
The documentation is wrong.
read() doesn't return a byte but one sample worth of data. Your samples are 16 bits (I2S_DATA_BIT_WIDTH_16BIT), so you have to interpret the sample values as signed 16-bit (int16_t).
Note that audio samples are usually represented and handled as signed values where the sound waves oscillate around 0.

Re: ESP32-S3 PDM Microphone

Posted: Wed Aug 27, 2025 4:23 pm
by geoffr
Looks like the documentation doesn't line up :(
read() - Read the next available byte from the I2S interface.
https://docs.espressif.com/projects/ard ... i/i2s.html

But that would change things!

Although as I noted above it still does not work correctly - I get samples in the ~6553x range which is not a valid sample (It's not that noisy!). Any further things I've missed?

Sample Output:

Code: Select all

65535
65534
65534
65534
65535
65535
65535
0
1
0
0
1
1
2

Re: ESP32-S3 PDM Microphone

Posted: Wed Aug 27, 2025 9:29 pm
by MicroController
Any further things I've missed?
My last post maybe?

Re: ESP32-S3 PDM Microphone

Posted: Thu Aug 28, 2025 12:13 am
by lbernstone
To be explicit about what MicroController is saying, cast it as an int16_t:
int16_t sample = (int16_t)I2S.read();
That should make those 65535 results show up as -1.

Re: ESP32-S3 PDM Microphone

Posted: Thu Aug 28, 2025 6:28 pm
by geoffr
yes, that does change 65535 to -1. I do however have other samples which are crazy large as seen by 65534 above. With the change to int16_t I get the output below. Why am I seeing -2 and -3 (or even much more negative!)

Code: Select all

0
-1
-1
1
0
0
-1
-2
-1
2
2
-1
-3
-2
-2
0
1

Code: Select all

=====================================
PDM test
=====================================
0
-6595
-32768
-29199
-32156
-30203
-31348
-30704
-31063
-30865
-30973

Re: ESP32-S3 PDM Microphone

Posted: Thu Aug 28, 2025 7:59 pm
by MicroController
I'm giving up.