diff --git a/boards/m5stack-atom-echo/demo.yaml b/boards/m5stack-atom-echo/demo.yaml index 5247fb5..e857988 100644 --- a/boards/m5stack-atom-echo/demo.yaml +++ b/boards/m5stack-atom-echo/demo.yaml @@ -1,12 +1,9 @@ # Copyright 2025 Vinicius Fortuna -# SPDX-License-Identifier: Apache-2.0 +# SPDX-License-Identifier: Apache-2.0 -# This demo exercises the button, LED, IR transmitter, speaker and microphone. -# When the button is pressed, it will: -# - Toggle the LED -# - Send an IR command -# - Play a tone on the speaker -# The microphone is used by the VAD sensor, which logs when it detects speech. +# This demo exercises the microphone as an Analog Sound Level Meter. +# It continuously measures the ambient noise level and sets the light +# brightness to match the sound level. packages: board: !include ./board.yaml @@ -17,22 +14,35 @@ esphome: logger: -binary_sensor: - - id: !extend echo_button - on_release: - - light.toggle: - id: echo_led - transition_length: 0.5s - - remote_transmitter.transmit_nec: - address: 0x1234 - command: 0x5678 - - rtttl.play: - id: rtttl_player - rtttl: scale_up:d=32,o=5,b=100:c,c#,d#,e,f#,g#,a#,b - # Work around RTTTL player getting stuck after first play. - # See https://github.com/esphome/esphome/issues/10312 - - lambda: |- - id(rtttl_player).enable_loop(); +microphone: + - id: !extend echo_microphone + # Without this, the sound level is squished in a narrow range + # aroung -30 dB. + correct_dc_offset: true + +sensor: + - id: sound_level_sensor + platform: sound_level + microphone: echo_microphone + passive: false + measurement_duration: 50ms + rms: + name: "Sound Level" + on_value: + then: + - light.turn_on: + id: echo_led + brightness: !lambda |- + // The sound level is in dB, from approx -80 (quiet) to 0 (loud). + // Map it to a brightness from 0.0 to 1.0. + // We'll use a range of -60 to 0 dB. + const float db_min = -60.0; + const float db_max = -25.0; + float brightness = (x - db_min) / (db_max - db_min); + // Clamp to the valid range + if (brightness < 0.0) brightness = 0.0; + if (brightness > 1.0) brightness = 1.0; + return brightness; rtttl: - id: rtttl_player @@ -41,3 +51,19 @@ rtttl: on_finished_playback: then: - logger.log: Finished playing RTTTL tune. + +binary_sensor: + - id: !extend echo_button + on_release: + - remote_transmitter.transmit_nec: + address: 0x1234 + command: 0x5678 + # yamllint disable-line rule:comments-indentation + # TODO: Make this work with the microphone. + # - rtttl.play: + # id: rtttl_player + # rtttl: scale_up:d=32,o=5,b=100:c,c#,d#,e,f#,g#,a#,b + # # Work around RTTTL player getting stuck after first play. + # # See https://github.com/esphome/esphome/issues/10312 + # - lambda: |- + # id(rtttl_player).enable_loop();