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

Add ADC multisampling #6330

Merged
merged 7 commits into from
May 16, 2024
Merged

Add ADC multisampling #6330

merged 7 commits into from
May 16, 2024

Conversation

Mat931
Copy link
Contributor

@Mat931 Mat931 commented Mar 7, 2024

What does this implement/fix?

Added a new feature to sample the ADC multiple times in one reading and return the average. This is recommended by Espressif for noise reduction. Compared to increasing the update rate of the sensor this has the advantage that some math operations only need to be executed once and the averaging is done using integers.
For now it only takes multiple samples on the ESP32 if attenuation is not set to auto.
Tested on the ESP32, ESP8266 and RP2040. Testers needed for LibreTiny.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#3664

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# Example config.yaml
external_components:
  - source: github://pr#6330
    components: [adc]
    refresh: always

sensor:
  - platform: adc
    id: my_voltage
    pin: GPIO36
    attenuation: 11db
    update_interval: 1s
    samples: 100

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@codecov-commenter
Copy link

codecov-commenter commented Mar 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.21%. Comparing base (4d8b5ed) to head (de4de9a).
Report is 603 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6330      +/-   ##
==========================================
+ Coverage   53.70%   54.21%   +0.50%     
==========================================
  Files          50       50              
  Lines        9408     9594     +186     
  Branches     1654     1691      +37     
==========================================
+ Hits         5053     5201     +148     
- Misses       4056     4069      +13     
- Partials      299      324      +25     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Mat931 Mat931 marked this pull request as ready for review March 7, 2024 10:35
@Mat931 Mat931 requested a review from a team as a code owner March 7, 2024 10:35
@probot-esphome
Copy link

probot-esphome bot commented Mar 7, 2024

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (adc) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

edwardtfn added a commit to Blackymas/NSPanel_HA_Blueprint that referenced this pull request Mar 17, 2024
Use esphome/esphome#6330 to enable sampling on adc sensor and smooth the data.
Solves #1918
@edwardtfn
Copy link
Contributor

You might wanna know we are playing with this solution on Blackymas/NSPanel_HA_Blueprint#1918 with positive results so far.
Just a couple of people testing, but it looks to be improving the temperature measurements in an Sonoff NSPanel (ESP32 with ESP-IDF).
I will try to get more people testing in the next couple of days.
Thanks for your work on this. 😃

@andythomas
Copy link

andythomas commented Mar 24, 2024

I would like to second this PR and share the data I collected using it with the NSPanel as already indicated in the last comment. A measurement without samples or any other filtering leads to the following plot. The red curve depicts the Gaussian fit to the data with a width of 0.2°C.

If I utilize the new samples: 12 I get the following result with a width of <=0.04°C.

Also, it seems the data scattering is not entirely caused by white noise and the samples improved the result more than the expected 1/sqrt(N), when modifying N even changing bucket size and measurement resolution to adjust for the narrower Gaussian. Consequently, I think this feature provides a qualitative improvement compared to, e.g., an averaging filter.

@esphome esphome bot marked this pull request as draft March 24, 2024 22:35
@esphome
Copy link

esphome bot commented Mar 24, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@Mat931 Mat931 marked this pull request as ready for review April 4, 2024 00:09
@esphome esphome bot requested a review from jesserockz April 4, 2024 00:09
@probot-esphome
Copy link

probot-esphome bot commented Apr 4, 2024

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (adc) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@edwardtfn
Copy link
Contributor

I'm getting this when compiling with ESPHome 2024.5.0b4:


src/esphome/components/adc/adc_sensor.cpp: In member function 'virtual void esphome::adc::ADCSensor::setup()':
src/esphome/components/adc/adc_sensor.cpp:65:28: warning: 'ADC_ATTEN_DB_11' is deprecated [-Wdeprecated-declarations]
   for (int32_t i = 0; i <= ADC_ATTEN_DB_11; i++) {
                            ^~~~~~~~~~~~~~~
In file included from /data/cache/platformio/packages/framework-espidf@3.40407.0/components/driver/include/driver/adc.h:14,
                 from src/esphome/components/adc/adc_sensor.h:10,
                 from src/esphome/components/adc/adc_sensor.cpp:1:
/data/cache/platformio/packages/framework-espidf@3.40407.0/components/hal/include/hal/adc_types.h:54:5: note: declared here
     ADC_ATTEN_DB_11 __attribute__((deprecated)) = ADC_ATTEN_DB_12,  ///<This is deprecated, it behaves the same as `ADC_ATTEN_DB_12`
     ^~~~~~~~~~~~~~~
src/esphome/components/adc/adc_sensor.cpp: In member function 'virtual void esphome::adc::ADCSensor::dump_config()':
src/esphome/components/adc/adc_sensor.cpp:121:12: warning: 'ADC_ATTEN_DB_11' is deprecated [-Wdeprecated-declarations]
       case ADC_ATTEN_DB_11:
            ^~~~~~~~~~~~~~~
In file included from /data/cache/platformio/packages/framework-espidf@3.40407.0/components/driver/include/driver/adc.h:14,
                 from src/esphome/components/adc/adc_sensor.h:10,
                 from src/esphome/components/adc/adc_sensor.cpp:1:
/data/cache/platformio/packages/framework-espidf@3.40407.0/components/hal/include/hal/adc_types.h:54:5: note: declared here
     ADC_ATTEN_DB_11 __attribute__((deprecated)) = ADC_ATTEN_DB_12,  ///<This is deprecated, it behaves the same as `ADC_ATTEN_DB_12`
     ^~~~~~~~~~~~~~~
src/esphome/components/adc/adc_sensor.cpp: In member function 'virtual float esphome::adc::ADCSensor::sample()':
src/esphome/components/adc/adc_sensor.cpp:203:48: warning: 'ADC_ATTEN_DB_11' is deprecated [-Wdeprecated-declarations]
     adc1_config_channel_atten(this->channel1_, ADC_ATTEN_DB_11);
                                                ^~~~~~~~~~~~~~~
In file included from /data/cache/platformio/packages/framework-espidf@3.40407.0/components/driver/include/driver/adc.h:14,
                 from src/esphome/components/adc/adc_sensor.h:10,
                 from src/esphome/components/adc/adc_sensor.cpp:1:
/data/cache/platformio/packages/framework-espidf@3.40407.0/components/hal/include/hal/adc_types.h:54:5: note: declared here
     ADC_ATTEN_DB_11 __attribute__((deprecated)) = ADC_ATTEN_DB_12,  ///<This is deprecated, it behaves the same as `ADC_ATTEN_DB_12`
     ^~~~~~~~~~~~~~~
src/esphome/components/adc/adc_sensor.cpp:218:48: warning: 'ADC_ATTEN_DB_11' is deprecated [-Wdeprecated-declarations]
     adc2_config_channel_atten(this->channel2_, ADC_ATTEN_DB_11);
                                                ^~~~~~~~~~~~~~~
In file included from /data/cache/platformio/packages/framework-espidf@3.40407.0/components/driver/include/driver/adc.h:14,
                 from src/esphome/components/adc/adc_sensor.h:10,
                 from src/esphome/components/adc/adc_sensor.cpp:1:
/data/cache/platformio/packages/framework-espidf@3.40407.0/components/hal/include/hal/adc_types.h:54:5: note: declared here
     ADC_ATTEN_DB_11 __attribute__((deprecated)) = ADC_ATTEN_DB_12,  ///<This is deprecated, it behaves the same as `ADC_ATTEN_DB_12`
     ^~~~~~~~~~~~~~~
src/esphome/components/adc/adc_sensor.cpp:238:91: warning: 'ADC_ATTEN_DB_11' is deprecated [-Wdeprecated-declarations]
   uint32_t mv11 = esp_adc_cal_raw_to_voltage(raw11, &this->cal_characteristics_[(int32_t) ADC_ATTEN_DB_11]);
                                                                                           ^~~~~~~~~~~~~~~
In file included from /data/cache/platformio/packages/framework-espidf@3.40407.0/components/driver/include/driver/adc.h:14,
                 from src/esphome/components/adc/adc_sensor.h:10,
                 from src/esphome/components/adc/adc_sensor.cpp:1:
/data/cache/platformio/packages/framework-espidf@3.40407.0/components/hal/include/hal/adc_types.h:54:5: note: declared here
     ADC_ATTEN_DB_11 __attribute__((deprecated)) = ADC_ATTEN_DB_12,  ///<This is deprecated, it behaves the same as `ADC_ATTEN_DB_12`
     ^~~~~~~~~~~~~~~

@jesserockz
Copy link
Member

@edwardtfn
Unrelated to this PR. The field was changed in esp-idf 4.4.7
espressif/esp-idf@9b2661c
Needs to be fixed in its own PR in ESPHome.
Can you please report a new issue for it?

@jesserockz jesserockz merged commit 247b2ee into esphome:dev May 16, 2024
55 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants