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) {