Ledc PWM frequency not being set correctly

dotbot
Posts: 6
Joined: Mon Jan 13, 2020 7:23 am

Ledc PWM frequency not being set correctly

Postby dotbot » Thu Oct 22, 2020 2:57 pm

Hi All
I've been trying to configure PWM on GPIO 5 and 17 (used esp-idf) and while I can get the duty cycle to be set correctly the frequency does not match the value set in the ledc_timer_config_t object. For whatever reason, the period is set to 3.6seconds (rather than a frequency of 25kHz as set). Is there another timer or something i need to set? I've used this code for gpios 16 and 17 in another project and it seems to work with out issue.

Code: Select all

#define MOTOR_PWM_CHANNEL_LEFT LEDC_CHANNEL_1
#define MOTOR_PWM_CHANNEL_RIGHT LEDC_CHANNEL_2
#define MOTOR_PWM_TIMER LEDC_TIMER_1
#define MOTOR_PWM_BIT_NUM LEDC_TIMER_10_BIT

#define RIGHT_PWM_PIN GPIO_NUM_17
#define LEFT_PWM_PIN GPIO_NUM_5

void motor_pwm_init(void)
{
    ledc_channel_config_t ledc_channel_left = {0}, ledc_channel_right = {0};
    ledc_channel_left.gpio_num = LEFT_PWM_PIN;
    ledc_channel_left.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_channel_left.channel = MOTOR_PWM_CHANNEL_LEFT;
    ledc_channel_left.intr_type = LEDC_INTR_DISABLE;
    ledc_channel_left.timer_sel = MOTOR_PWM_TIMER;
    ledc_channel_left.duty = 750;
	
    ledc_channel_right.gpio_num = RIGHT_PWM_PIN;
    ledc_channel_right.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_channel_right.channel = MOTOR_PWM_CHANNEL_RIGHT;
    ledc_channel_right.intr_type = LEDC_INTR_DISABLE;
    ledc_channel_right.timer_sel = MOTOR_PWM_TIMER;
    ledc_channel_right.duty = 750;
	
    ledc_timer_config_t ledc_timer = {0};
    ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_timer.bit_num = MOTOR_PWM_BIT_NUM;
    ledc_timer.timer_num = MOTOR_PWM_TIMER;
    ledc_timer.freq_hz = 25000;

    ESP_ERROR_CHECK( ledc_timer_config(&ledc_timer) );
	ESP_ERROR_CHECK( ledc_channel_config(&ledc_channel_left) );
	ESP_ERROR_CHECK( ledc_channel_config(&ledc_channel_right) );
	
}

dotbot
Posts: 6
Joined: Mon Jan 13, 2020 7:23 am

Re: Ledc PWM frequency not being set correctly

Postby dotbot » Fri Oct 23, 2020 1:30 am

worked it out, silly mistake

I was setting the timers freq_hz with an integer when it needed uint32_t, not sure how it worked in my last project but it seems to be working now

Who is online

Users browsing this forum: No registered users and 120 guests