Chapter 2. API Reference
Change PWM Signal Once the channel starts operating and generating the PWM signal with the constant duty
cycle and frequency, there are a couple of ways to change this signal. When driving LEDs, primarily the duty cycle
is changed to vary the light intensity.
The following two sections describe how to change the duty cycle using software and hardware fading. If required,
the signal’s frequency can also be changed; it is covered in Section Change PWM Frequency.
Note: All the timers and channels in the ESP32-S2’s LED PWM Controller only support low speed mode. Any
change of PWM settings must be explicitly triggered by software (see below).
Change PWM Duty Cycle Using Software To set the duty cycle, use the dedicated function
ledc_set_duty(). After that, call ledc_update_duty() to activate the changes. To check the
currently set value, use the corresponding _get_ function ledc_get_duty().
Another way to set the duty cycle, as well as some other channel parameters, is by calling
ledc_channel_config() covered in Section Channel Configuration.
The range of the duty cycle values passed to functions depends on selected duty_resolution and should be
from 0 to (2 ** duty_resolution) - 1. For example, if the selected duty resolution is 10, then the duty
cycle values can range from 0 to 1023. This provides the resolution of ~0.1%.
Change PWM Duty Cycle using Hardware The LEDC hardware provides the means to gradually transition from
one duty cycle value to another. To use this functionality, enable fading with ledc_fade_func_install()
and then configure it by calling one of the available fading functions:
• ledc_set_fade_with_time()
• ledc_set_fade_with_step()
• ledc_set_fade()
Finally start fading with ledc_fade_start().
If not required anymore, fading and an associated interrupt can be disabled with
ledc_fade_func_uninstall().
Change PWM Frequency The LEDC API provides several ways to change the PWM frequency “on the fly”:
• Set the frequency by calling ledc_set_freq(). There is a corresponding function ledc_get_freq()
to check the current frequency.
• Change the frequency and the duty resolution by calling ledc_bind_channel_timer() to bind some
other timer to the channel.
• Change the channel’s timer by calling ledc_channel_config().
More Control Over PWM There are several lower level timer-specific functions that can be used to change PWM
settings:
• ledc_timer_set()
• ledc_timer_rst()
• ledc_timer_pause()
• ledc_timer_resume()
The first two functions are called “behind the scenes”by ledc_channel_config() to provide a startup of a
timer after it is configured.
Use Interrupts When configuring an LEDC channel, one of the parameters selected within
ledc_channel_config_t is ledc_intr_type_t which triggers an interrupt on fade completion.
For registration of a handler to address this interrupt, call ledc_isr_register().
Espressif Systems 317
Submit Document Feedback
Release v4.4