diff --git a/components/volume_sensor/volume_sensor.cpp b/components/volume_sensor/volume_sensor.cpp index ea6971e..21a4ca7 100644 --- a/components/volume_sensor/volume_sensor.cpp +++ b/components/volume_sensor/volume_sensor.cpp @@ -13,6 +13,15 @@ float VolumeSensor::map_value_float(float x, float in_min, float in_max, return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } +void VolumeSensor::setup() { + #if defined(ESP32) + // Set attenuation to 0db to be as close to esp8266 as possible + analogSetPinAttenuation(this->pin_, ADC_0db); + // Set ADC resolution to 10 bits (0-1023) on ESP32 + analogReadResolution(10); + #endif +} + void VolumeSensor::dump_config() { LOG_SENSOR("", "Volume Sensor", this); ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_); @@ -108,4 +117,4 @@ void VolumeSensor::update() { } } // namespace volume_sensor -} // namespace esphome \ No newline at end of file +} // namespace esphome diff --git a/components/volume_sensor/volume_sensor.h b/components/volume_sensor/volume_sensor.h index d106e82..3342042 100644 --- a/components/volume_sensor/volume_sensor.h +++ b/components/volume_sensor/volume_sensor.h @@ -19,6 +19,7 @@ class VolumeSensor : public sensor::Sensor, public PollingComponent { void set_percentage_sensor(sensor::Sensor *sensor) { this->percentage_sensor_ = sensor; } void update() override; + void setup() override; void dump_config() override; protected: @@ -45,4 +46,4 @@ class VolumeSensor : public sensor::Sensor, public PollingComponent { }; } // namespace volume_sensor -} // namespace esphome \ No newline at end of file +} // namespace esphome