From 7590d5eacb746fb0f8edf01e8e0acda79598a451 Mon Sep 17 00:00:00 2001 From: Martin <25747549+martgras@users.noreply.github.com> Date: Wed, 26 Jan 2022 13:33:59 +0100 Subject: [PATCH] set adc width to 13 bits for esp32-s2 (#3117) --- esphome/components/adc/adc_sensor.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index c3e5221bf7..ad9cf29b6f 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -15,6 +15,11 @@ namespace esphome { namespace adc { static const char *const TAG = "adc"; +// 13 bits for S3 / 12 bit for all other esp32 variants +// create a const to avoid the repated cast to enum +#ifdef USE_ESP32 +static const adc_bits_width_t ADC_WIDTH_MAX_SOC_BITS = static_cast(ADC_WIDTH_MAX - 1); +#endif void ADCSensor::setup() { ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str()); @@ -23,14 +28,14 @@ void ADCSensor::setup() { #endif #ifdef USE_ESP32 - adc1_config_width(ADC_WIDTH_BIT_12); + adc1_config_width(ADC_WIDTH_MAX_SOC_BITS); if (!autorange_) { adc1_config_channel_atten(channel_, attenuation_); } // load characteristics for each attenuation for (int i = 0; i < (int) ADC_ATTEN_MAX; i++) { - auto cal_value = esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t) i, ADC_WIDTH_BIT_12, + auto cal_value = esp_adc_cal_characterize(ADC_UNIT_1, (adc_atten_t) i, ADC_WIDTH_MAX_SOC_BITS, 1100, // default vref &cal_characteristics_[i]); switch (cal_value) {