LEDC duty resolution / freq pair causing watchdog errors

ant000
Posts: 10
Joined: Wed Mar 11, 2026 4:31 am

LEDC duty resolution / freq pair causing watchdog errors

Postby ant000 » Fri Mar 20, 2026 5:36 am

I have some code using LEDC_TIMER_10_BIT with a frequency of 1kHz that runs but causes watchdog errors. The errors continue if I drop the freq to 500Hz so I tried 50Hz which worked. My understanding is that LEDC_TIMER_10_BIT should support up to about 78kHz, but I also understand that some of the PWM channels do not support LEDC_HIGH_SPEED_MODE, assuming I even need that.

Assuming this is correct, my question is, where can I find the information that shows the device PWM channel capabilities? I have an old ESP32 and a newer S3 and the datasheets do not appear to have this information.

Also, is there a definition of LEDC_HIGH_SPEED_MODE etc, as to their capabilities?

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby MicroController » Fri Mar 20, 2026 9:12 am

I have some code using LEDC_TIMER_10_BIT with a frequency of 1kHz that runs but causes watchdog errors.
The LEDC itself runs independent of any watchdog timer. The problem is likely in your code doing some busy-waiting where it shouldn't.

ant000
Posts: 10
Joined: Wed Mar 11, 2026 4:31 am

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby ant000 » Fri Mar 20, 2026 10:12 am

That is entirely probable, I am a beginner. This is the code in case it is something obvious. Still wouldn't mind knowing the other information if anyone knows.

Code: Select all

#include <stdio.h>
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/ledc.h"


#define B_LEDC_PIN 18                         // LED connected to GPIO 18
#define B_LEDC_CHANNEL LEDC_CHANNEL_0
#define B_LEDC_TIMER LEDC_TIMER_0
#define B_LEDC_MODE LEDC_HIGH_SPEED_MODE
#define B_LEDC_DUTY_RES LEDC_TIMER_10_BIT     // 10-bit resolution (0-1023)
#define B_LEDC_FREQUENCY (1000)               // 1 kHz PWM frequency (1000)
#define B_LEDC_PERIOD 1                       // 1ms period
#define B_MIN_DUTY 10
#define B_MAX_DUTY 300
#define B_PI_STEP 0.01

void led_mgmt(void *pvParameter){

	printf("LED on core: %d \n", xPortGetCoreID());
	

    ledc_timer_config_t ledc_timer = {
        .speed_mode = B_LEDC_MODE,
        .duty_resolution = B_LEDC_DUTY_RES,
        .timer_num = B_LEDC_TIMER,
        .freq_hz = B_LEDC_FREQUENCY
    };
	
	
    ledc_timer_config(&ledc_timer);

    ledc_channel_config_t ledc_channel = {
        .gpio_num = B_LEDC_PIN,
        .speed_mode = B_LEDC_MODE,
        .channel = B_LEDC_CHANNEL,
        .timer_sel = B_LEDC_TIMER,
        .duty = 0
    };
	
	
	
    ledc_channel_config(&ledc_channel);

	uint16_t led_duty;
	float pi_angle;

    while (1) {

		for (	pi_angle = M_PI / 2;
				pi_angle >= 0;
				pi_angle -= B_PI_STEP) {
					
			led_duty = sin(pi_angle) * (B_MAX_DUTY - B_MIN_DUTY) + B_MIN_DUTY;
            ledc_set_duty(B_LEDC_MODE, B_LEDC_CHANNEL, led_duty);
            ledc_update_duty(B_LEDC_MODE, B_LEDC_CHANNEL);
            vTaskDelay(B_LEDC_PERIOD / portTICK_PERIOD_MS);
        }

		for (	pi_angle = 0;
				pi_angle <= M_PI / 2;
				pi_angle += B_PI_STEP) {
					
			led_duty = sin(pi_angle) * (B_MAX_DUTY - B_MIN_DUTY) + B_MIN_DUTY;
            ledc_set_duty(B_LEDC_MODE, B_LEDC_CHANNEL, led_duty);
            ledc_update_duty(B_LEDC_MODE, B_LEDC_CHANNEL);
            vTaskDelay(B_LEDC_PERIOD / portTICK_PERIOD_MS);
        }
    }
}

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby MicroController » Fri Mar 20, 2026 10:26 am

Code: Select all

#define B_LEDC_PERIOD 1                       // 1ms period
...
vTaskDelay(B_LEDC_PERIOD / portTICK_PERIOD_MS);
There's your problem. By default, FreeRTOS's portTICK_PERIOD_MS is 10 ("tick rate"=100Hz). This results in your vTaskDelay(...) "delaying" for (1/10)=0, so no delay at all. This in turn makes your task use 100% of the CPU and the watchdog angry. Easiest solution is probably to just reduce the update rate of the LEDC to, e.g., 100Hz, i.e. change both B_PI_STEP and B_LEDC_PERIOD to 10x their current values (0.1f and 10 respectively).

Another option is to use a "High Resolution Timer" set to 1ms/1kHz.

ant000
Posts: 10
Joined: Wed Mar 11, 2026 4:31 am

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby ant000 » Fri Mar 20, 2026 10:45 am

Thanks, that make perfect sense. Does that mean I would have to remove vTaskDelay and block the core for that single task or probably better still, use an ISR timer with call back to run LEDC for higher frequencies?

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby MicroController » Fri Mar 20, 2026 11:00 am

Not sure if you actually want/need to change an LED's brightness 1000x per second. Also, changing the duty cycle of a 1kHz PWM at 1kHz is bound to result in tons of glitches at the output.

ant000
Posts: 10
Joined: Wed Mar 11, 2026 4:31 am

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby ant000 » Fri Mar 20, 2026 11:19 am

Noted. Thanks for your help. I'll use the low frequency for now and mess around with the higher frequencies when I get around to production quality video lighting tests. Might need a different MCU or go analogue.

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby MicroController » Fri Mar 20, 2026 11:33 am

Other options for fast "analog" output include the GPIOs' PDM ("Sigma Delta Modulator") (8 bits resolution, no DMA) and I2S in PDM mode (16 bits resolution).

MicroController
Posts: 2661
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: LEDC duty resolution / freq pair causing watchdog errors

Postby MicroController » Fri Mar 20, 2026 12:22 pm

where can I find the information that shows the device PWM channel capabilities? I have an old ESP32 and a newer S3 and the datasheets do not appear to have this information.
You'll probably find the information in the respective "Technical Reference Manuals" (TRMs) (vs. in the datasheets).

Who is online

Users browsing this forum: Bytespider, PetalBot and 11 guests