Skip to content

Esp32 HAL - Dynamic ADC attenuation#14623

Merged
thinkyhead merged 4 commits intoMarlinFirmware:bugfix-2.0.xfrom
simon-jouet:esp32-adc-attenuation
Jul 15, 2019
Merged

Esp32 HAL - Dynamic ADC attenuation#14623
thinkyhead merged 4 commits intoMarlinFirmware:bugfix-2.0.xfrom
simon-jouet:esp32-adc-attenuation

Conversation

@simon-jouet
Copy link
Contributor

The ESP32 ADC operates in the range of 0V to ~1.1V (1.0V -- 1.2V). In order for this ADC to work with the full range input of 3.3V the attenuation must be configured to attenuate the input voltage below 1.1V.

In the current implementation of the HAL the ADC attenuation is always the maximum of 11dB allowing the full 3.3V range input (well almost but that's for another discussion). Using the full 111dB attenuation works but also decreases precision when the voltage is lower.

In this patch I use the ADC calibration characteristics to get the operating voltage range of each attenuation level. Using the last ADC reading the attenuation level can be changed to the nearest operating range. This allows much higher precision, which is especially relevant for higher temperatures (close to 0V).

In this code the attenuation is switched when the reading is 100mV below the nominal characteristics and 50mV above the previous attenuation. This range is used to compensate for inaccuracies when shifting range. (e.g when you change to a higher attenuation the value might be less precise and therefore change back to a higher attenuation, repeating multiple times and oscillating around two attenuations).

The 100mV and 50mV values have not been picked for any specific reason and some (better) values might be more suitable, but with this the attenuation do shift properly without oscillations.

@simon-jouet simon-jouet force-pushed the esp32-adc-attenuation branch from 362d3a2 to adafc94 Compare July 14, 2019 15:15
@thinkyhead thinkyhead added A: ESP32 PR: Improvement T: HAL & APIs Topic related to the HAL and internal APIs. labels Jul 14, 2019
@thinkyhead thinkyhead merged commit f0de56a into MarlinFirmware:bugfix-2.0.x Jul 15, 2019
@vivian-ng
Copy link
Contributor

@simon-jouet
Due to the change here, is there a need to redefine HAL_ADC_RESOLUTION for the ESP32's HAL to 12 instead of the current 10?

Analog to Digital Converter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A: ESP32 PR: Improvement T: HAL & APIs Topic related to the HAL and internal APIs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants