A few months ago I requested a new feature from Amazon to make sure my Echo doesn’t yell at me in the silence of the night when I talk to it. Unfortunately they sent me a standard email and did nothing. So, hating it, I installed the home assistant, bought my first ESP8266, went back to studying programming, learned logarithm function, fell in love with ESPhome.
Now that I’ve been using it for a few days I decided to bring my “sensor” to make it easier for anyone who has any similar problem or want to make some use with a decibelimeter with the HW-484/KY-037 or any other sound sensor, because I believe it doesn’t have much difference.
You should be able to adjust the sensitivity of the same through the potentiometer, but as the exact sensitivity of each microphone can vary, I left a sensitivity slider on the sensor that should approximate the values of the decibelimeter, if you want to try to get it right, you can use some application in the your smartphone that should make it easier, my initial setting was at 36.5 sensitivity. If you do too, share which sensitivity was good for you and so we can build a pattern.
If you prefer to just use the sensor in its raw version, I’m already exporting this data to the home assistant.
The connection was made on analog port 0 and the power supply was made through 3.3v, I believe that the voltage can bring a lot of difference to your measurements.
I hope it’s useful for someone else. Feedback and suggestions to change the code are very welcome.
esphome:
name: volume-sensor
esp8266:
board: nodemcuv2
logger:
api:
encryption:
key: !secret apikey
ota:
password: !secret ota
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "Volume-Sensor Fallback Hotspot"
password: !secret wifi_password
captive_portal:
globals:
- id: esphome_sensitivity
type: float
initial_value: '36.5'
restore_value: yes
- id: esphome_volume
type: int
sensor:
- platform: adc
pin: A0
id: esphome_db
device_class: signal_strength
name: "Db SoundEsp"
icon: "mdi:volume-vibrate"
unit_of_measurement: "db"
update_interval: 5s
raw: true
filters:
- lambda: |-
unsigned int sample;
unsigned long startMillis= millis();
float peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < 500) {
sample = analogRead(A0);
if (sample < 1024){
if (sample > signalMax){
signalMax = sample;
}
else if (sample < signalMin){
signalMin = sample;
}
}
}
peakToPeak = map((signalMax - signalMin),1,1024,1.5,1024);
id(esphome_volume) = peakToPeak;
float state = id(esphome_sensitivity)*log10(peakToPeak)+15;
return(state);
- platform: template
name: "Volume SoundEsp"
icon: "mdi:volume-high"
unit_of_measurement: "%"
update_interval: 5s
lambda: return(map((id(esphome_db).state),15,150,0,100));
- platform: template
name: "RAW SoundEsp"
icon: "mdi:volume-source"
unit_of_measurement: "%"
update_interval: 5s
lambda: return(map(id(esphome_volume),1,1024,0,100));
number:
- platform: template
id: sensitivity_slider
name: "Sensitivity SoundEsp"
icon: "mdi:knob"
update_interval: 5s
initial_value: "36.5"
step: 0.1
min_value: 20
max_value: 40
mode: slider
set_action:
then:
- lambda: id(esphome_sensitivity) = x;