Ledc PWM frequency not being set correctly
Posted: 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.
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) );
}