How loud is the room
The first sketch: the TK101 on two GPIOs, the ESP32-S3 reading 512 samples at a time, and the Serial Plotter drawing how loud each 32 ms was. It takes the average out of every block before it looks for the peak, and it prints the raw size with no gain, so you see how small ordinary sound is.
What you need
- An ESP32-S3 dev board and its USB cable.
- The TK101, and four jumper wires.
Wire it
| TK101 | ESP32-S3 |
|---|---|
| GND | GND |
| 3V3 | 3V3 |
| CLK | GPIO 5 |
| DATA | GPIO 6 |
Upload, then open Tools > Serial Plotter at 115200. The red LED lights as soon as the board has power.
Why it takes the average out
Each block's samples sit on a small offset, so silence is not exactly 0. Measured from zero, the peak would be the offset plus the sound, and a silent room would plot as a steady line above the floor. The sketch adds up the block, divides by its length, and measures every sample from that average instead.
What it prints
One line per block, about 31 a second: peak: and a number from 0 to
32767. The plotter draws it as one line named peak.
A quiet room plots near the floor. Talking at arm's length lifts it by tens; a clap near the board should jump it by hundreds or more. The top of the scale is 32767, and ordinary sound never comes close: Why the numbers look small works out why.
Try each of these and watch the plot:
- Talk at arm's length, then a hand's width from the board.
- Cover the can with a fingertip while you talk.
- Clap once near the board, then once from across the room.
The code
Reads 512 samples, 32 ms at 16 kHz, subtracts their average, and prints the biggest distance from it as peak: for the Serial Plotter. Nothing to install: ESP_I2S comes with the ESP32 core 3.x.
// TK101 PDM Microphone: how loud is the room? Open Tools > Serial Plotter.
//
// Wiring, TK101 to ESP32-S3:
// GND -> GND
// 3V3 -> 3V3
// CLK -> GPIO 5
// DATA -> GPIO 6
//
// Arduino IDE: Tools > Board > "ESP32S3 Dev Module" (esp32 core 3.x),
// USB CDC On Boot: Enabled. Serial Plotter at 115200.
#include <ESP_I2S.h>
const int PIN_CLK = 5;
const int PIN_DATA = 6;
const int RATE = 16000;
I2SClass mic;
int16_t samples[512]; // 32 ms at 16 kHz
void setup() {
Serial.begin(115200);
mic.setPinsPdmRx(PIN_CLK, PIN_DATA);
if (!mic.begin(I2S_MODE_PDM_RX, RATE, I2S_DATA_BIT_WIDTH_16BIT,
I2S_SLOT_MODE_MONO)) {
Serial.println("PDM did not start");
while (true) delay(1000);
}
}
void loop() {
size_t n = mic.readBytes((char *)samples, sizeof(samples)) / 2;
if (n == 0) return;
// Take out the average first: a microphone's samples sit on a small
// offset, and it would read as sound that is not there.
long sum = 0;
for (size_t i = 0; i < n; i++) sum += samples[i];
int offset = sum / (long)n;
int peak = 0;
for (size_t i = 0; i < n; i++) {
int a = abs(samples[i] - offset);
if (a > peak) peak = a;
}
Serial.print("peak:");
Serial.println(peak); // 0 to 32767
}This sketch was compiled with esp32 core 3.3.10 for the ESP32S3 Dev Module, not run. The microphone circuit was tested on the owner's bench on the same two pins, 5 and 6, with his own code on the older I2S driver.
When it does not work
Nothing is changing on DATA. Check CLK goes to GPIO 5 and DATA to GPIO 6, not swapped, and that 3V3 reaches the board: the red LED should be on. Without a clock the microphone sleeps and sends nothing.
begin() refused the setup. Check the board is set to ESP32S3 Dev Module on esp32 core 3.x, and that setPinsPdmRx() comes before begin(). Older cores do not have the ESP_I2S library at all and fail to compile instead.
Tools > USB CDC On Boot must be Enabled for Serial to reach the USB socket on most ESP32-S3 boards. Set it, upload again, and open the plotter at 115200.
That is the microphone working. Speech at arm's length works out to a few tens out of 32767; a clap near the board should jump to hundreds or more. Why the numbers look small explains the arithmetic.
The microphone into the TK103 and a pair of headphones, as you speak.
Hear it live →Edit this page — content/books/pdm-microphone/how-loud-is-the-room.mdx
Questions about this product
See what other owners have asked, and read their solutions.
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.