ESP32 LEDC Output

The LEDC output component exposes a LEDC PWM channel of the ESP32 as an output component.

The frequency range of LEDC is from 10Hz to 40MHz - however, higher frequencies require a smaller bit depth which means the output is not that accurate for frequencies above ~300kHz.

Example Usage For a Light

# Example configuration entry
output:
  - platform: ledc
    pin: GPIO19
    id: gpio_19

# Example usage in a light
light:
  - platform: monochromatic
    output: gpio_19
    name: "Kitchen Light"

Example Usage For a Piezo Buzzer

# Configure the output
output:
  - platform: ledc
    ######################################################
    # One buzzer leg connected to GPIO12, the other to GND
    ######################################################
    pin: GPIO12
    id: buzzer

# Example usage in an automation
on_press:
    then:
    ######################################################
    # Must be turned on before setting frequency & level
    ######################################################
    - output.turn_on: buzzer
    ######################################################
    # Frequency sets the wave size
    ######################################################
    - output.ledc.set_frequency:
        id: buzzer
        frequency: "1000Hz"
    ######################################################
    # level sets the %age time the PWM is on
    ######################################################
    - output.set_level:
        id: buzzer
        level: "50%"

Configuration variables:

  • pin (Required, Pin): The pin to use LEDC on. Can only be GPIO0-GPIO33.

  • id (Required, ID): The id to use for this output component.

  • frequency (Optional, float): At which frequency to run the LEDC channel’s timer. Defaults to 1000Hz.

  • All other options from Output.

Advanced options:

  • channel (Optional, int): Manually set the LEDC channel to use. Two adjacent channels share the same timer. Defaults to an automatic selection.

Note: When configuring custom frequencies for two or more outputs, ensure that you manually specify channel 0, 2, 4, 6 for each output. This will prevent issues that arise from automatic selection, which chooses adjacent channels with shared timers. See Issue #3114 for more details.

output.ledc.set_frequency Action

This Action allows you to manually change the frequency of an LEDC channel at runtime. Use cases include controlling a passive buzzer (for pitch control).

on_...:
  - output.ledc.set_frequency:
      id: ledc_output
      frequency: 100Hz

Configuration variables:

  • id (Required, ID): The ID of the LEDC output to change.

  • frequency (Required, templatable, float): The frequency to set in hertz.

See Also