PDM microphone/From bits to numbers/03. From bits to samples
From bits to numbers · 03 of 9

From bits to samples

The ESP32-S3 runs the microphone's clock at 64 or 128 times the sample rate, about 1 to 2 MHz for 16,000 samples a second, and counts the bits that come back in windows. Each window becomes one 16-bit sample. The clock also tells the microphone which mode to run in.

Counting, in hardware

A sample is a number that says how far the pressure has moved at one instant. To get one from a PDM stream, count the 1s in a short window of bits: more 1s, higher pressure. Do that sixteen thousand times a second and you have audio at a 16 kHz sample rate.

Count the 1s in a window
64 bits a sample
Bits per sample
Clock at 16 kHz
1.0 MHz
Microphone mode
standard
Levels a count can take
65
64 bits a sample: a clock of about 1 MHz. One of the two ratios ESP-IDF's PDM receiver uses, and inside the microphone's standard mode (1.0 MHz to 4.8 MHz). The real filter does better than this plain count.

The ESP32-S3 does not literally count. Its filter weighs each bit and its neighbours, a low-pass filter, which does better than a plain count. The idea is the same, and so is the arithmetic that matters: the clock is the window size times the sample rate. Turning many fast bits into fewer slow samples is called decimation.

The clock picks the microphone's mode

The microphone has no settings pin for its mode. It watches the clock:

ClockMode
under 50 kHzasleep, sends nothing
150 to 900 kHzlow power: 320 µA, 57 dB signal-to-noise
1.0 to 4.8 MHzstandard: 750 µA, 58 dB signal-to-noise

Those are the datasheet's typical figures. ESP-IDF's PDM receiver runs the clock at 64 or 128 times the sample rate, so at 16 kHz it is about 1 to 2 MHz: standard mode, which is what the book's sketches get.

One line of setup

mic.setPinsPdmRx(PIN_CLK, PIN_DATA);
mic.begin(I2S_MODE_PDM_RX, 16000, I2S_DATA_BIT_WIDTH_16BIT,
          I2S_SLOT_MODE_MONO);

I2S_MODE_PDM_RX turns on the clock and the filter. After that, mic.readBytes() hands back ready-made 16-bit samples, two bytes each, and the bitstream is never seen by the sketch.

Only I2S0 can receive PDM, and the ESP_I2S library puts a PDM input there by itself. The ESP32-S3's other I2S peripheral, I2S1, stays free for an output, which is how the live build plays the microphone into the TK103.

When it does not work

Can I use a higher sample rate than 16 kHz?

The clock scales with it: 64 or 128 times the rate. At 16 kHz that is about 1 to 2 MHz, inside the microphone's standard mode of 1.0 to 4.8 MHz. Work out the clock for any other rate the same way and keep it inside that range. The book's sketches all use 16 kHz, which covers speech.

Can the microphone and a speaker both run at once?

Yes. The ESP32-S3 has two I2S peripherals. Only I2S0 can take PDM in, so the microphone uses it, and the amplifier or DAC goes on I2S1. The ESP_I2S library picks the port for you: give it a second I2SClass object.

Why does the sketch say I2S_MODE_PDM_RX and not I2S_MODE_STD?

STD is ordinary I2S, which expects whole numbers on a bit clock and a word-select line. PDM_RX turns on the counting filter and makes one clock on the pin named in setPinsPdmRx(). With STD the ESP32-S3 would read the bitstream as garbage.

Where this goes next

What −26 dBFS means for the sample values a sketch prints.

Why the numbers look small

Edit this page — content/books/pdm-microphone/from-bits-to-samples.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

PDM Microphone

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum