Chapter 2. API Reference
Supported Range of Frequency and Duty Resolutions
The LED PWM Controller is designed primarily to drive LEDs. It provides a large flexibility of PWM duty cycle
settings. For instance, the PWM frequency of 5 kHz can have the maximum duty resolution of 13 bits. This means
that the duty can be set anywhere from 0 to 100% with a resolution of ~0.012% (2 ** 13 = 8192 discrete levels of the
LED intensity). Note, however, that these parameters depend on the clock signal clocking the LED PWM Controller
timer which in turn clocks the channel (see
PWM Controller (LEDC) [PDF]).
The LEDC can be used for generating signals at much higher frequencies that are sufficient enough to clock other
devices, e.g., a digital camera module. In this case, the maximum available frequency is 40 MHz with duty resolution
of 1 bit. This means that the duty cycle is fixed at 50% and cannot be adjusted.
The LEDC API is designed to report an error when trying to set a frequency and a duty resolution that exceed the
range of LEDC's hardware. For example, an attempt to set the frequency to 20 MHz and the duty resolution to 3
bits will result in the following error reported on a serial monitor:
E (196) ledc: requested frequency and duty resolution cannot be achieved,
reducing freq_hz or duty_resolution. div_param=128
→
In such a situation, either the duty resolution or the frequency must be reduced. For example, setting the duty
resolution to 2 will resolve this issue and will make it possible to set the duty cycle at 25% steps, i.e., at 25%, 50%
or 75%.
The LEDC driver will also capture and report attempts to configure frequency / duty resolution combinations that are
below the supported minimum, e.g.:
E (196) ledc: requested frequency and duty resolution cannot be achieved,
increasing freq_hz or duty_resolution. div_param=128000000
→
The duty resolution is normally set using ledc_timer_bit_t. This enumeration covers the range from 10 to 15
bits. If a smaller duty resolution is required (from 10 down to 1), enter the equivalent numeric values directly.
Application Example
The LEDC change duty cycle and fading control example: peripherals/ledc/ledc_fade.
The LEDC basic example: peripherals/ledc/ledc_basic.
API Reference
Header File
•
components/driver/include/driver/ledc.h
Functions
esp_err_t
ledc_channel_config(const
LEDC
channel
gpio_num/interrupt/source timer/frequency(Hz)/LEDC duty resolution.
Return
• ESP_OK Success
• ESP_ERR_INVALID_ARG Parameter error
Parameters
• ledc_conf: Pointer of LEDC channel configure struct
esp_err_t
ledc_timer_config(const
LEDC timer configuration Configure LEDC timer with the given source timer/frequency(Hz)/duty_resolution.
Return
• ESP_OK Success
Espressif Systems
timer configuration
ledc_channel_config_t
configuration
Configure
ledc_timer_config_t
Submit Document Feedback
and the ESP32-S2 Technical Reference Manual > LED
*ledc_conf)
LEDC
channel
with
*timer_conf)
318
try␣
try␣
the
given
channel/output
Release v4.4
Need help?
Do you have a question about the ESP32-S2 and is the answer not in the manual?