🎙 Detecting sound level using ESP8266 and ESPHome

Filed under:

In a few weeks I am moving to a new place where I have lots of room for my records and DJ booth. Seems like the ideal opportunity to add a nice WS2812 ledstrip at the back of my DJ booth and have it light up automatically in the evening when I start playing records.

Luckily I had everything laying around in the house. I'll be using my favorite module, the ESP8266, as a controller and a KY-038 microphone sound sensor as the sensor.

Wiring

The KY-038 provides two outputs: one analog A0 to wire up to the ADC pin and measure through voltage changes, and one digital to wire up to a free GPIO pin to have a simple binary output. Initially when starting off I used the analog output but afterwards switched to the digital one because it felt more accurate.

Wire it up like this:

KY-038

KY-038 ESP8266
D0 D5 (or any other of the GPIO pins)
G GND
+ 3V3 (or 5, but 3 works as well)
A0 A0 (if you want to use ADC)

The sensor itself contains a potentiometer which you turn to adjust sound sensitivity. This was trial and error adjusting it with sound running in the background.

Code

Using ESPHome this was super simple. Before adding it to Home Assistant I just used the logger component to verify the output of the sensor.

With ESPHome you can easily choose between the ADC output or GPIO (digital) output

esphome:
  name: wemos_sound_level_detector
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

logger:
ota:
api:

# BELOW WILL READ OUT THE ANALOG SIGNAL
# sensor:
  # - platform: adc
  #   pin: A0
  #   name: "Sound level"
  #   update_interval: 1s
  #   filters:
  #     - multiply: 3.3 # see https://esphome.io/components/sensor/adc.html

# DEBOUNCED FOR 1S BECAUSE THE TRIGGER PERIOD IS VERY SHORT
binary_sensor:
  - platform: gpio
    pin: D2
    name: "Sound level"
    filters:
      - delayed_off: 1s

Note the debounce period I added because the sensor toggles between the on and off state very quickly. Now, just run esphome sound_level_detector.yaml run and upload it to your board and your good to go!

sound level demo

Automating

I haven't written the actual automation yet but it should be very simple. ESPHome integrates nicely with Home Assistant using its own API and the sensor will be auto discovered.

The automation signature will look like this:

  1. Check if we are in the evening (using a time of day sensor)
  2. Watch the state of our binary_sensor advertised by ESPHome
  3. If the previous condition has the toggle set to True, turn on the LED strip
  4. If the previous condition has the toggle set to False and the LED strip is on, toggle it off.

Very simple, but a very effective automation! Going further I could also set a certain colour when the intensity of a track changes (think: hard pounding warehouse snares and kicks).

You can take a look at the rest of my ESPHome configs here: https://github.com/thibmaek/homeassistant-conf/tree/master/esphome

The Fritzing chart wrongfully shows a NodeMCU, but in reality I used a Wemos D1 Mini because it was the first one I could grab