Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPH0645: values are wrong #3

Open
Electronlibre2012 opened this issue Mar 18, 2023 · 17 comments
Open

SPH0645: values are wrong #3

Electronlibre2012 opened this issue Mar 18, 2023 · 17 comments

Comments

@Electronlibre2012
Copy link

Electronlibre2012 commented Mar 18, 2023

Hello

i try your component and if i use advanced example code i have 121db and if i speak or clap my hand nothing happend.

I try the minimal sample code and i get 104db and same, not a lot of difference if i speak or clap my hand close to the mic nothing change.

I use a SPH0645 like this :

i2s:
  bck_pin: GPIO23
  ws_pin: GPIO18
  din_pin: GPIO19
  sample_rate: 48000            # default: 48000
  bits_per_sample: 32           # default: 32

any idea please why i doesnt work?

@stas-sl
Copy link
Owner

stas-sl commented Mar 18, 2023

I haven't used SPH0645 myself, but most probably it is either wiring or bit depth/channels/format issue. You might need to experiment a bit with different settings. For example trying to connect L/R to GND or +3V.

Have you also used bits_shift setting?

I looked into its spec, it says data precision is 18 bits:
image

So you might try:

  bits_per_sample: 32 
  bits_shift: 14  # 32 - 18

# or

  bits_per_sample: 24
  bits_shift: 6 # 24 - 18

@Electronlibre2012
Copy link
Author

thanks for your help,

bits_per_sample: 24
bits_shift: 6

give "-inf" value but

bits_per_sample: 32 
bits_shift: 14  # 32 - 18

value are still at 104db but when i clap my hand it goes to 112db max.

I already try to solder SEL to GND,

i try right now to put on 3.3V to see if something change

@Electronlibre2012
Copy link
Author

bits_per_sample: 32 
bits_shift: 14  # 32 - 18

with SEL on 3.3v give "-inf" value, "infinite" i guess...

@Electronlibre2012
Copy link
Author

bits_per_sample: 24
bits_shift: 6

with SEL on 3.3v give 119 db and not move.

Seems the more accurate is 32/14 with SEL on GND...

I dont understand. Must i try another value?

@Electronlibre2012
Copy link
Author

Electronlibre2012 commented Mar 18, 2023

[This shows the compatibility problem between the ESP32 I2S interface and the SPH0645LM4H microphone.](url) seems i cant do anything with this shi.... LOL

What mic do you use please? a INMP441 I2S ?

@stas-sl
Copy link
Owner

stas-sl commented Mar 18, 2023

Have your read Ivan's note on SPH0645? https://hackaday.io/project/166867-esp32-i2s-slm

I guess that might be the issue. It needs specific initialisation to be applied to work correctly. And also an additional DC blocker filter, which you can add actually in config, copying coefficients from Ivan's repository. But the main issue is that I don't have that specific initialisation needed.

You might try Ivan's project to see if it outputs correct values. Or you can try forking or copying my component locally and add https://github.com/ikostoski/esp32-i2s-slm/blob/b643efa1beae51b84d4ad9b2bfa35d439c81e9b6/esp32-i2s-slm.ino#LL272C3-L277C9 those lines to i2s component setup.

@stas-sl
Copy link
Owner

stas-sl commented Mar 18, 2023

Well, you found it yourself )

Yes, I'm using INMP441, but it is also a bit outdated. The best option AFAIK should be ICS 43434, but those are harder to find.

@stas-sl stas-sl changed the title values are wrong SPH0645: values are wrong Mar 18, 2023
@stas-sl
Copy link
Owner

stas-sl commented Mar 18, 2023

You can probably add it to config, without modifying the component, thought I haven't tried it:

esphome:
  on_boot:
    then:
      - lambda: !lambda |-
          REG_SET_BIT(I2S_TIMING_REG(0), BIT(9));   
          REG_SET_BIT(I2S_CONF_REG(0), I2S_RX_MSB_SHIFT);  

@Electronlibre2012
Copy link
Author

You might try Ivan's project to see if it outputs correct values. Or you can try forking or copying my component locally and add https://github.com/ikostoski/esp32-i2s-slm/blob/b643efa1beae51b84d4ad9b2bfa35d439c81e9b6/esp32-i2s-slm.ino#LL272C3-L277C9 those lines to i2s component setup.

yes, buts its arduino ide...not esphome...

with your peace of code added, its 98db and doesn change anymore ...sorry, doesnt work...

Thanks

I wait for the INMP441...in 3 weeks LOL

@stas-sl
Copy link
Owner

stas-sl commented Mar 18, 2023

Have you applied DC blocking filter?

As reported here https://forums.adafruit.com/viewtopic.php?f=19&p=616024, looks like the signal is shifted down by half of full scale, so when calculating RMS it will be equivalent to pretty loud sound. Instead the signal should oscillate around 0. To achieve this, DC blocking filter should be used.

Again I can't test it, but I hope those are correct filter coefficients. So the final config should be smth like this:

esphome:
  name: sound-level-meter
  on_boot:
    then:
      - lambda: !lambda |-
          REG_SET_BIT(I2S_TIMING_REG(0), BIT(9));   
          REG_SET_BIT(I2S_CONF_REG(0), I2S_RX_MSB_SHIFT);  

external_components:
  - source: github://stas-sl/esphome-sound-level-meter

esp32:
  board: esp32dev
  framework:
    type: arduino

logger:
  level: DEBUG

i2s:
  bck_pin: 23
  ws_pin: 18
  din_pin: 19
  sample_rate: 48000            
  bits_per_sample: 32           
  bits_shift: 14

sound_level_meter:
  update_interval: 1s           
  mic_sensitivity: -26dB     
  mic_sensitivity_ref: 94dB

  groups:
    - filters:
        - type: sos
          coeffs: 
            # DC-blocker
            #        b0                 b1          b2      a1    a2          
            - [1.00123377961525, -1.00123377961525, 0.0, -0.9992, 0.0]
    - sensors:
        - type: eq
          name: Leq_1s

@Electronlibre2012
Copy link
Author

Thanks for your time and help! but it doesnt work. 98db and it moves to 101 when i clap hands...

i see some guys used 16000 as sample_rate but it doesnt change anything for me.

@Electronlibre2012
Copy link
Author

Electronlibre2012 commented Apr 13, 2023

hello i receive my inpm441

but now i have a compile error :

HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
Dependency Graph
|-- WiFi @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- Update @ 2.0.0
|-- noise-c @ 0.1.4
|   |-- libsodium @ 1.10018.1
Compiling /data/decibel-metre/.pioenvs/decibel-metre/src/esphome/components/sound_level_meter/sound_level_meter.cpp.o
Compiling /data/decibel-metre/.pioenvs/decibel-metre/src/main.cpp.o
Compiling /data/decibel-metre/.pioenvs/decibel-metre/lib64d/WiFi/WiFiSTA.cpp.o
Compiling /data/decibel-metre/.pioenvs/decibel-metre/lib64d/WiFi/WiFiScan.cpp.o
Compiling /data/decibel-metre/.pioenvs/decibel-metre/lib64d/WiFi/WiFiServer.cpp.o
Compiling /data/decibel-metre/.pioenvs/decibel-metre/lib64d/WiFi/WiFiUdp.cpp.o
src/esphome/components/sound_level_meter/sound_level_meter.cpp: In member function 'virtual void esphome::sound_level_meter::SoundLevelMeter::loop()':
src/esphome/components/sound_level_meter/sound_level_meter.cpp:60:19: error: missing template arguments before 'lock'
   std::lock_guard lock(this->defer_mutex_);
                   ^~~~
src/esphome/components/sound_level_meter/sound_level_meter.cpp: In member function 'void esphome::sound_level_meter::SoundLevelMeter::turn_on()':
src/esphome/components/sound_level_meter/sound_level_meter.cpp:69:19: error: missing template arguments before 'lock'
   std::lock_guard lock(this->on_mutex_);
                   ^~~~
src/esphome/components/sound_level_meter/sound_level_meter.cpp: In member function 'void esphome::sound_level_meter::SoundLevelMeter::turn_off()':
src/esphome/components/sound_level_meter/sound_level_meter.cpp:77:19: error: missing template arguments before 'lock'
   std::lock_guard lock(this->on_mutex_);
                   ^~~~
src/esphome/components/sound_level_meter/sound_level_meter.cpp: In static member function 'static void esphome::sound_level_meter::SoundLevelMeter::task(void*)':
src/esphome/components/sound_level_meter/sound_level_meter.cpp:105:24: error: missing template arguments before 'lock'
       std::unique_lock lock(this_->on_mutex_);
                        ^~~~
src/esphome/components/sound_level_meter/sound_level_meter.cpp:106:26: error: 'lock' was not declared in this scope
       this_->on_cv_.wait(lock, [this_] { return this_->is_on_; });
                          ^~~~
src/esphome/components/sound_level_meter/sound_level_meter.cpp:106:26: note: suggested alternative:
In file included from src/esphome/components/sound_level_meter/sound_level_meter.h:3,
                 from src/esphome/components/sound_level_meter/sound_level_meter.cpp:1:
/data/cache/platformio/packages/toolchain-xtensa-esp32/xtensa-esp32-elf/include/c++/8.4.0/mutex:542:5: note:   'std::lock'
     lock(_L1& __l1, _L2& __l2, _L3&... __l3)
     ^~~~
src/esphome/components/sound_level_meter/sound_level_meter.cpp: In member function 'void esphome::sound_level_meter::SoundLevelMeter::defer(std::function<void()>&&)':
src/esphome/components/sound_level_meter/sound_level_meter.cpp:128:19: error: missing template arguments before 'lock'
   std::lock_guard lock(this->defer_mutex_);
                   ^~~~
Compiling /data/decibel-metre/.pioenvs/decibel-metre/lib915/ESPmDNS/ESPmDNS.cpp.o
*** [/data/decibel-metre/.pioenvs/decibel-metre/src/esphome/components/sound_level_meter/sound_level_meter.cpp.o] Error 1
Compiling /data/decibel-metre/.pioenvs/decibel-metre/libbc6/Update/HttpsOTAUpdate.cpp.o
========================== [FAILED] Took 3.02 seconds ==========================

if you have an idea? thanks

@stas-sl
Copy link
Owner

stas-sl commented Apr 13, 2023

Thanks for reporting it, indeed I unintentionally used newer C++ version language feature. I've just fixed code, please update and try again.

@Electronlibre2012
Copy link
Author

thanks! i can compile but i have -inf only...

here is my code, the inmp441 is 3.3V power :

esp32:
  board: wemos_d1_mini32
  framework:
    type: arduino

external_components:
  - source: github://stas-sl/esphome-sound-level-meter

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pwd
  manual_ip:
    static_ip: 192.168.1.x
    gateway: 192.168.1.1
    subnet: 255.255.255.0
    dns1: 192.168.1.1
  fast_connect: on
  power_save_mode: none


i2s:
  bck_pin: GPIO5
  ws_pin: GPIO25
  din_pin: GPIO26
  sample_rate: 48000            # default: 48000
  bits_per_sample: 32           # default: 32

  # right shift samples. 
  # for example if mic has 24 bit resolution, and
  # i2s configured as 32 bits, then audio data will be aligned left (MSB)
  # and LSB will be padded with zeros, so you might want to shift them right by 8 bits
  bits_shift: 8                 # default: 0

sound_level_meter:
  # update_interval specifies over which interval to aggregate audio data
  # you can specify default update_interval on top level, but you can also override
  # it further by specifying it on sensor level
  update_interval: 1s           # default: 60s

  # buffer_size is in samples (not bytes), so for float data type
  # number of bytes will be buffer_size * 4
  buffer_size: 1024             # default: 1024

  # see your mic datasheet to find sensitivity and reference SPL.
  # those are used to convert dB FS to db SPL
  mic_sensitivity: -26dB        # default: empty 
  mic_sensitivity_ref: 94dB     # default: empty 

  # for flexibility sensors are organized hierarchically into groups. each group
  # could have any number of filters, sensors and nested groups. 
  # for examples if there is a top level group A with filter A and nested group B
  # with filter B, then for sensors inside group B filters A and then B will be 
  # applied:
  # groups:
  #   # group A
  #   - filters:
  #       - filter A
  #     groups:
  #       # group B
  #       - filters:
  #           - filter B
  #         sensors:
  #           - sensor X
  groups:
    - sensors:
        - type: eq
          name: Leq_1s

sensor:
  - platform: internal_temperature
    name: "Internal Temperature Decibel metre"

any idea why it doesnt work please?

@Electronlibre2012
Copy link
Author

edit : i think my pinout are wrong :

bck_pin: GPIO5
ws_pin: GPIO25
din_pin: GPIO26

i have SO, SCK and WS and L/R so i have search schematics :
image

and i think

bck_pin: SCK pin
ws_pin: WS pin
din_pin: SO pin

and VCC to 3.3v
and GND to GND

and now it works great!

thanks!!!!!

mini value is 49dB...seems a bit high...

@stas-sl
Copy link
Owner

stas-sl commented Apr 14, 2023

49 dBA or or dBZ? dBZ values are a bit higher than dBA. In complete silence there should be <40 dBA. I was able to get 36-37 dBA with INMP441, however it might depend on mic instance, power noise, radio noise etc... and it might require a bit of experimentation to figure out where the noise comes from. Maybe turning off WiFI, trying different PINS, wires...

@Electronlibre2012
Copy link
Author

ok thanks, i will try :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants