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:
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
yamlesphome:name: wemos_sound_level_detectorplatform: ESP8266board: d1_miniwifi:ssid: !secret wifi_ssidpassword: !secret wifi_passwordlogger: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 SHORTbinary_sensor:- platform: gpiopin: D2name: "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!
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:
Check if we are in the evening (using a time of day sensor)
Watch the state of our binary_sensor advertised by ESPHome
If the previous condition has the toggle set to True, turn on the LED strip
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