Adjusting PWM frequency externally

Is it possible to adjust the PWM frequency of the LEDC from the HA with a number template?
Finding the “right” frequency for various motors when they work well and more importantly silent is tedious task because of the recompiling.

1 Like

Did you try user defined actions with variables from HA? :point_down:

Once the number helper is brought into the esp with the home assistant component. Under that entry use an on_value trigger with the linked action.

https://esphome.io/components/output/ledc.html#output-ledc-set-frequency-action

1 Like

I have already tried that, but somethings is not right.
I see the values changing in the esphome log as I adjust them in the HA, but it seams it does not actually change the PWM frequency value.
This is my code:

number:
  - platform: template
    id: pwmFreq
    name: PWM Frequency
    icon: mdi:sync
    optimistic: true
    min_value: 10
    max_value: 1000
    initial_value: 10
    step: 10
    on_value:
      - output.ledc.set_frequency:
          id: pwm_a
          frequency: 100Hz
fan:
  - platform: speed
    output: pwm_a
    name: "Vent"
    speed_count: 3

output:
  - platform: ledc
    id: pwm_a
    pin: 19
    frequency: 10 Hz
    power_supply: psu
    min_power: 33%
    max_power: 100%

power_supply:
  - id: 'psu'
    pin:
      number: 21
      inverted: false

You have the frequency set at a fixed number, 100. You have to return the value of the number.

frequency: !lambda 'return id(pwmFreq).state';

Thanks for the reply, but that does not compile.

ERROR Error while reading config: Invalid YAML syntax:

while parsing a block mapping
  in "/config/esphome/test200.yaml", line 45, column 11
expected <block end>, but found '<scalar>'
  in "/config/esphome/test200.yaml", line 46, column 56

Are you saying this is how the code should look like?

number:
  - platform: template
    id: pwmFreq
    name: PWM Frequency
    icon: mdi:sync
    optimistic: true
    min_value: 10
    max_value: 1000
    initial_value: 10
    step: 10
    on_value:
      - output.ledc.set_frequency:
          id: pwm_a
          frequency: !lambda 'return id(pwmFreq).state';
fan:
  - platform: speed
    output: pwm_a
    name: "Vent"
    speed_count: 3

output:
  - platform: ledc
    id: pwm_a
    pin: 19
    frequency: 10 Hz
    power_supply: psu
    min_power: 33%
    max_power: 100%

power_supply:
  - id: 'psu'
    pin:
      number: 21
      inverted: false

Yes. I don’t think that error is from the lambda. Open the nodes yaml, line 46, 56th character is where the error is. Post the full yaml if you can’t find it.

This is the full code. It was working before I added lambda
56th character is the end of the lambda
if I take out “;” it also does not work

esphome:
  name: test200
  friendly_name: test200

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xAP+XHCM5uqlds+JO5e3VRX8B4HXLA1lZUjU0KWr9ac="

ota:
  - platform: esphome
    password: "b0fed970247ce6544ae4c876b4f87e91"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test200 Fallback Hotspot"
    password: "4airnlN4OnLx"

captive_portal:

number:
  - platform: template
    id: pwmFreq
    name: PWM Frequency
    icon: mdi:sync
    optimistic: true
    min_value: 10
    max_value: 1000
    initial_value: 10
    step: 10
    on_value:
      - output.ledc.set_frequency:
          id: pwm_a
          frequency: !lambda 'return id(pwmFreq).state';
fan:
  - platform: speed
    output: pwm_a
    name: "Vent"
    speed_count: 3

output:
  - platform: ledc
    id: pwm_a
    pin: 19
    frequency: 10 Hz
    power_supply: psu
    min_power: 33%
    max_power: 100%

power_supply:
  - id: 'psu'
    pin:
      number: 21
      inverted: false

It looks like the frequency can’t be hard coded under output. Remove the key. it will default to 1000 at start until you change the value of the number. If that is a problem at boot you can set the pwm to 10 with

esphome:
  name: test200
  friendly_name: test200
  on_boot:
    priority: -100
    then:
      - output.ledc.set_frequency:
          id: pwm_a
          frequency: 10
output:
  - platform: ledc
    id: pwm_a
    pin: 19
    power_supply: psu
    min_power: 33%
    max_power: 100%

still does not compile…

ERROR Error while reading config: Invalid YAML syntax:

while parsing a block mapping
  in "/config/esphome/test200.yaml", line 51, column 11
expected <block end>, but found '<scalar>'
  in "/config/esphome/test200.yaml", line 52, column 56

full code…

esphome:
  name: test200
  friendly_name: test200
  on_boot:
    priority: -100
    then:
      - output.ledc.set_frequency:
          id: pwm_a
          frequency: 10

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xAP+XHCM5uqlds+JO5e3VRX8B4HXLA1lZUjU0KWr9ac="

ota:
  - platform: esphome
    password: "b0fed970247ce6544ae4c876b4f87e91"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test200 Fallback Hotspot"
    password: "4airnlN4OnLx"

captive_portal:

number:
  - platform: template
    id: pwmFreq
    name: PWM Frequency
    icon: mdi:sync
    optimistic: true
    min_value: 10
    max_value: 1000
    initial_value: 10
    step: 10
    on_value:
      - output.ledc.set_frequency:
          id: pwm_a
          frequency: !lambda 'return id(pwmFreq).state';
fan:
  - platform: speed
    output: pwm_a
    name: "Vent"
    speed_count: 3

output:
  - platform: ledc
    id: pwm_a
    pin: 19
    power_supply: psu
    min_power: 33%
    max_power: 100%

power_supply:
  - id: 'psu'
    pin:
      number: 21
      inverted: false

I pulled this off someone’s blog. It compiles but I don’t have an esp handy to test it.

esphome:
  name: test200
  friendly_name: test200

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xAP+XHCM5uqlds+JO5e3VRX8B4HXLA1lZUjU0KWr9ac="

ota:
  - platform: esphome
    password: "b0fed970247ce6544ae4c876b4f87e91"

wifi:
  ssid: xxxxxxxxx
  password: xxxxxxxxxxx

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Test200 Fallback Hotspot"
    password: "4airnlN4OnLx"

captive_portal:

number:
  - platform: template
    id: pwmFreq
    name: PWM Frequency
    icon: mdi:sync
    optimistic: true
    unit_of_measurement: 'Hz'
    min_value: 10
    max_value: 1000
    initial_value: 10
    step: 10
    on_value:
      lambda: |-
        int newFreq = (int)(10000000.0*id(pwmFreq).state)+1000;
        id(pwm_a).update_frequency(newFreq);
fan:
  - platform: speed
    output: pwm_a
    name: "Vent"
    speed_count: 3

output:
  - platform: ledc
    id: pwm_a
    pin: 19
#    power_supply: psu
#    min_power: 33%
#    max_power: 100%

power_supply:
  - id: 'psu'
    pin:
      number: 21
      inverted: false

Yes, it compiles, but when I change the frequency, I get this error in the log.

[19:11:54][E][ledc.output:202]: Frequency -2147482624.000000 can't be achieved with any bit depth
[19:11:54][E][component:164]: Component ledc.output set Error flag: unspecified
[19:11:54][E][component:176]: Component ledc.output cleared Error flag
[19:11:58][D][number:054]: 'PWM Frequency' - Setting number value
[19:11:58][D][number:113]:   New number value: 320.000000
[19:11:58][D][number:012]: 'PWM Frequency': Sending state 320.000000
[19:11:58][E][ledc.output:202]: Frequency -2147482624.000000 can't be achieved with any bit depth
[19:11:58][E][component:164]: Component ledc.output set Error flag: unspecified
[19:11:58][E][component:176]: Component ledc.output cleared Error flag
[19:12:00][D][number:054]: 'PWM Frequency' - Setting number value
[19:12:00][D][number:113]:   New number value: 330.000000
[19:12:00][D][number:012]: 'PWM Frequency': Sending state 330.000000
[19:12:00][E][ledc.output:202]: Frequency -2147482624.000000 can't be achieved with any bit depth
[19:12:00][E][component:164]: Component ledc.output set Error flag: unspecified
[19:12:00][E][component:176]: Component ledc.output cleared Error flag

I was able to get it working using the number value

      lambda: |-
        int newFreq = (int)(id(pwmFreq).state);
        id(pwm_a).update_frequency(newFreq);

Thanks Mike. It compiles, but I am not able to test it as my motor driver is burned. I see how it look if I hook it to a LED.

EDIT: I have tested it with a LED, and it is working. Thank a lot once more.

1 Like