ESP32-C3-DevKitM-1 PWM problem

crybite3
Posts: 2
Joined: Tue Apr 26, 2022 5:32 am

ESP32-C3-DevKitM-1 PWM problem

Postby crybite3 » Wed Apr 27, 2022 3:30 am

Hello everyone,

We are interfacing an IR camera with the ESP32-C3-DevKitM-1. Because the camera needs a 3MHz clock to drive so we used LEDC to generate a 3MHz PWM signal with 50% duty cycle.
But we found out that the negative edge of PWM signal is glittering:
PWM3MHz.PNG
PWM3MHz.PNG (5.95 KiB) Viewed 3009 times
May we have any suggestions to the problem? Thank you all.

Here is our code :

Code: main.c Select all


/* LEDC (LED Controller) basic example

This example code is in the Public Domain (or CC0 licensed, at your option.)

Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include "driver/ledc.h"
#include "esp_err.h"


/*
* PWM Output configuration
*/
#define LEDC_TIMER LEDC_TIMER_0 //Using Timer0 as clock source
#define LEDC_MODE LEDC_LOW_SPEED_MODE //Setting speed mode to low
#define LEDC_OUTPUT_IO (2) //Using GPIO 2 as PWM output
#define LEDC_CHANNEL LEDC_CHANNEL_0 //Using PWM channel 0
#define LEDC_DUTY_RES LEDC_TIMER_4_BIT //Setting duty resolution
#define LEDC_DUTY (8) //Setting duty cycle to 50%. ((2 ** 2) - 1) * 50% = 8
#define LEDC_FREQUENCY (3000000) //Setting PWM Frequency (Hz)


/*
* pwm_init
*
* Initialise PWM controller
*/
static void pwm_init(void)
{
// Prepare and then apply the LEDC PWM timer configuration
ledc_timer_config_t ledc_timer = {0};
ledc_timer.speed_mode = LEDC_MODE;
ledc_timer.timer_num = LEDC_TIMER;
ledc_timer.duty_resolution = LEDC_DUTY_RES;
ledc_timer.freq_hz = LEDC_FREQUENCY;
ledc_timer.clk_cfg = LEDC_AUTO_CLK;

ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));

// Prepare and then apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel = {0};
ledc_channel.speed_mode = LEDC_MODE;
ledc_channel.channel = LEDC_CHANNEL;
ledc_channel.timer_sel = LEDC_TIMER;
ledc_channel.intr_type = LEDC_INTR_DISABLE;
ledc_channel.gpio_num = LEDC_OUTPUT_IO;
ledc_channel.duty = 0;
ledc_channel.hpoint = 0;
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));


}


/*
* app_main
* Program entry point
*/
void app_main(void)
{

pwm_init();
// Set duty to 50%
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
// Update duty to apply the new value
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
while(1);
}

Sprite
Espressif staff
Espressif staff
Posts: 10619
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3-DevKitM-1 PWM problem

Postby Sprite » Wed Apr 27, 2022 5:57 am

The term you're looking for is 'jitter', not 'glitter'. It's because you're telling the PWM module to generate a 4-bit PWM, which means that one PWM cycle takes up 16 clock cycles. The base clock is 80MHz, meaning each PWM cycle is 5MHz, and this is not divisible by 3MHz, so instead the LEDC will try to 'dither' to approximate, meaning some cycles are longer and some are shorter.

You could reduce the jitter by changing LEDC_TIMER_4_BIT to LEDC_TIMER_1_BIT, but given that there is no integer divider that divides 80MHz into 3MHz, you'll always have some jitter.

crybite3
Posts: 2
Joined: Tue Apr 26, 2022 5:32 am

Re: ESP32-C3-DevKitM-1 PWM problem

Postby crybite3 » Fri Apr 29, 2022 8:51 am

We tested the camera can work in 2MHz, so we will go for it.
Thank you for your detailed explanation and quick reply.

Who is online

Users browsing this forum: No registered users and 1 guest