Level 3Unit B · Session 9
9

Unit B · What your device can sense

How loud is it in here?

What we are making: a page showing the loudest sound in the last moment — and a lesson in why that is harder than it sounds.

Why one reading is useless

Sound is a vibration. Read the microphone once and you catch the wave wherever it happens to be — possibly at zero, in the middle of a shout. Read it once a second and you will miss almost everything.

So the program reads it two hundred times in a fifth of a second and keeps only the biggest. That is called peak detection, and it is how every sound meter works.

New words
PeakThe biggest value in a burst of readings.
SamplingReading fast and repeatedly, rather than once.
Sensitivity screwOn the module. It changes the microphone gain, not your code.
ESP32USB to the laptop3V3GNDGPIO 35Sound sensorthe one with a microphoneVCCGNDAOloudnessThere is a small screw on the module. It sets the sensitivity, not the reading.
The sound sensor. It measures how loud, not what was said.
Session 9 — listening properly — 3 slots
SLOT 2
the microphone's pin
int soundPin = 35;
SLOT 3
listen 200 times in a fifth of a second, keep the biggest
int loudest = 0;
for (int i = 0; i < 200; i++) {
int now = analogRead(soundPin);
if (now > loudest) loudest = now;
delay(1);
}
return String(loudest);
SLOT 4
say what the number is
return "<h1>" + reading() + "</h1>"
"<p>loudest sound just now</p>";

Do it

  1. Wire it to GPIO 35, fill the three slots, upload.
  2. Refresh the page in silence. Write down the number.
  3. Talk normally while somebody refreshes. Write that down.
  4. Clap once. Write that down.
  5. Compare the three. Is the gap big enough to build something on?

Change it

  1. Add a word to the page — QUIET, TALKING or LOUD — using two thresholds of your own, in slot 3.
  2. Change 200 to 20 in slot 3. Does a clap still register reliably?
  3. Change it to 2000 and see how slow the page becomes to load.
💡 Tip
That last experiment matters more than it looks. While the board is listening for two seconds, it cannot answer anybody else's request. A device that is busy is a device that looks broken, and this is the first time you have been able to feel that.
What you seeWhyWhat to do
Number is always about the sameThe sensitivity screw is at one end.Turn it slowly with a small screwdriver while watching the readings.
Always reads 4095Sensitivity too high, or wired to 5V.Turn the screw down. Use 3.3V.
A clap does not registerToo few samples.Raise the loop count. A clap is very short.
The page takes ages to loadYou are sampling for too long.That is the trade-off. Find a length that catches claps and still feels quick.

What you learned

  • Some things cannot be measured with one reading.
  • Sampling fast and keeping the peak is how you catch a brief event.
  • Time spent measuring is time not spent answering. Everything is a trade.
Challenge optional — only if you finish early
  • Work out how quiet your room actually is at its quietest. Write the number down.
8. Light and dark, and a number you choose