PWM for fans AND a buzzer

I’m building a component that controls two fans speed based on measured temperature.

I am using the ledc library with a ESP32 C3.

Controlling the fans works well.

I am now trying to add an active buzzer that beeps (currently as a switch). This is also using the ledc library as per the example in this link:

But when the buzzer is triggered the fans go out of whack (stop, then stop-start) until I restart.

The ESP32 should have separate PWM channels so I really don’t understand what causes the interference. Any idea?

Code (part where fans/buzzer is configured):

output:
  - platform: ledc
    pin: GPIO3
    id: buzzer_pwm
  - platform: ledc
    id: fan1_pwm
    pin: 
      number: GPIO6
      mode:
        output: True
        pulldown: True
    frequency: $fan_frequency
  - platform: ledc
    id: fan2_pwm
    pin: 
      number: GPIO21
      mode:
        output: True
        pulldown: True
    frequency: $fan_frequency

fan:
  - platform: speed
    id: fan1
    output: fan1_pwm
    name: 'Fan1 PWM'
    restore_mode: RESTORE_DEFAULT_ON
  - platform: speed
    id: fan2
    output: fan2_pwm
    name: 'Fan2 PWM'
    restore_mode: RESTORE_DEFAULT_ON

switch:
  - platform: template 
    name: 'Buzzer'
    id: buzzer
    turn_on_action: 
      then:
        - output.turn_on: buzzer_pwm
        - output.ledc.set_frequency:
            id: buzzer_pwm
            frequency: "1000Hz"
        - output.set_level:
            id: buzzer_pwm
            level: "100%"

    turn_off_action:
      then:
        - output.turn_off: buzzer_pwm

Assign non-adjacent channels explicitly… if you don’t assign the channels explicitly they will default to adjacent channels with shared timers.

1 Like