From decf50ed492a06aeba7ce1735a0ab324bc4dad73 Mon Sep 17 00:00:00 2001 From: Markus <974709+Links2004@users.noreply.github.com> Date: Thu, 27 Jun 2024 03:48:01 +0200 Subject: [PATCH] Fix LEDC 100% is not 100% duty with ESP32 IDF (#6997) --- esphome/components/ledc/ledc_output.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/esphome/components/ledc/ledc_output.cpp b/esphome/components/ledc/ledc_output.cpp index 1040ac25b6..90e11fe4ad 100644 --- a/esphome/components/ledc/ledc_output.cpp +++ b/esphome/components/ledc/ledc_output.cpp @@ -115,12 +115,15 @@ void LEDCOutput::write_state(float state) { const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1; const float duty_rounded = roundf(state * max_duty); auto duty = static_cast(duty_rounded); - #ifdef USE_ARDUINO ESP_LOGV(TAG, "Setting duty: %u on channel %u", duty, this->channel_); ledcWrite(this->channel_, duty); #endif #ifdef USE_ESP_IDF + // ensure that 100% on is not 99.975% on + if ((duty == max_duty) && (max_duty != 1)) { + duty = max_duty + 1; + } auto speed_mode = get_speed_mode(channel_); auto chan_num = static_cast(channel_ % 8); int hpoint = ledc_angle_to_htop(this->phase_angle_, this->bit_depth_);