ESP32 LEDC Output
The LEDC output component exposes a LEDC PWMchannel 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.
Configuration variables
Section titled “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, frequency): At which frequency to run the LEDC channel’s timer. Defaults to
1kHz. -
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 issues#3114 for more details.
- phase_angle (Optional, float): Set a phase angle to the other channel of this timer. Range 0-360°, defaults to 0°
Example Usage For a Light
Section titled “Example Usage For a Light”# Example configuration entryoutput: - platform: ledc pin: GPIOXX id: gpio_
# Example usage in a lightlight: - platform: monochromatic output: gpio_19 name: "Kitchen Light"Example Usage For a Piezo Buzzer
Section titled “Example Usage For a Piezo Buzzer”# Configure the outputoutput: - platform: ledc ###################################################### # One buzzer leg connected to GPIO12, the other to GND ###################################################### pin: GPIO12 id: buzzer
# Example usage in an automationon_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: "1kHz" ###################################################### # level sets the %age time the PWM is on ###################################################### - output.set_level: id: buzzer level: "50%"Recommended frequencies
Section titled “Recommended frequencies”To get the highest available frequency while still getting the same bit depth it is recommended to pick one of the following frequencies.
Higher bit depth means that the light has more steps available to change from one value to another. This is especially noticeable when the light is below 10% and takes a long transition, e.g. turning slowly off.
| Frequency | Bit depth | Available steps for transitions |
|---|---|---|
| 1220Hz | 16 | 65536 |
| 2441Hz | 15 | 32768 |
| 4882Hz | 14 | 16384 |
| 9765Hz | 13 | 8192 |
| 19531Hz | 12 | 4096 |
The ESP8266 for instance has usually a frequency of 1000Hz with a resolution of 10 bits. This means that there are only 4 steps between each value.
output.ledc.set_frequency Action
Section titled “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: 100HzConfiguration variables:
- id (Required, ID): The ID of the LEDC output to change.
- frequency (Required, templatable, frequency): The frequency to set in Hz.