I read a lot of reports online of these sensors being unreliable, or being difficult to tune. The latter part is partly true, but there's a lot of misunderstanding about how to interpret the data. Once you understand how you should interpret it, it becomes much clearer how you should calibrate it and how to configure your ESPHome device to use the data it provides.
My first thought was higher measured voltage = louder noise. After all, every sensor I'd worked with worked in this way, but most of them reported the 'human friendly' value - e.g. temperature, CO2, humidity, etc.
Sound is different. You probably know that sound is made up of waves, and you've probably seen a waveform corresponding to audio.
Louder sounds aren't represented by just a higher peak, but by a higher deviation. For example, your baseline is set at 0. When a sound happens, the waveform starts and you have values that are positive and negative - so shouting might be represented as many points going from -0.5, then to +0.5, then back to -0.5, then to +0.5, etc.
The KY-037 works in this way too. When the noise is 'ambient' (in this context, that just means you're remaining quiet to measure the 'quiet' voltage), the voltage fluctuates only slighty. When you make noise, the voltage fluctuates wildly above and below the 'ambient' values. More noise = more fluctuation.
With this knowledge at hand, I was able to put together the appropriate lambda which compares current measured voltage against the previous measured voltage and determine the deviation (aka the delta).
Using abs() on this value ensured that the negative values become positive - e.g. -0.27 becomes 0.27. By sampling this frequently, and then using a moving window average, I was able to calculate the average 'noise level' in the environment the sensor is in.
To calibrate this, I used a sound meter app on my phone. I don't need it to be ultra-precise, I just needed it to be good enough. I sat silently, measuring the delta over a time and seeing what the sound meter app said. That corresponded to my 'quiet' measurement.
Then, I loaded up a 100hz test tone video on youtube, and used my headphones and the app to measure a constant noise level. This would be my 'noisy room' condition. Then, I used the exact same video and headphones with the KY-037 setup and measured the delta.
From this, I was able to adjust the potentiometer on the KY-037 and re-measure. Eventually, I managed to get the values and tuning dialled in to satisfactory levels and ultimately add environmental sound quality monitoring to my other measurements.
Note: You should adjust the values in the calibrate_linear according to your own specific conditions and calibrated measurements
esphome-esp32-ky037.yml
- This file (
esphome-esp32-ky037.yml
) is useful for short-term updates of values. It smoothes the output values to avoid heavy fluctuations, but is only useful for looking at short-term trends (e.g. < 30 minutes).
esphome-esp32-ky037-long-duration.yml
- This file (
esphome-esp32-ky037-long-duration.yml
) is useful for long-term updates. It averages the input values using a 5-minute window, and sends 1 value every 15 seconds. The output values (after passing through various functions) are also smoothed over a 5-minute window, and 1 value is sent every 15 seconds. This version would be most useful for a home-assistant dashboard.