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 2166 times
May we have any suggestions to the problem? Thank you all.

Here is our code :
  1. /* LEDC (LED Controller) basic example
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include "driver/ledc.h"
  12. #include "esp_err.h"
  13.  
  14.  
  15. /*
  16.  *  PWM Output configuration
  17.  */
  18. #define LEDC_TIMER         LEDC_TIMER_0         //Using Timer0 as clock source
  19. #define LEDC_MODE          LEDC_LOW_SPEED_MODE  //Setting speed mode to low
  20. #define LEDC_OUTPUT_IO     (2)                  //Using GPIO 2 as PWM output
  21. #define LEDC_CHANNEL       LEDC_CHANNEL_0       //Using PWM channel 0
  22. #define LEDC_DUTY_RES      LEDC_TIMER_4_BIT     //Setting duty resolution
  23. #define LEDC_DUTY          (8)                  //Setting duty cycle to 50%. ((2 ** 2) - 1) * 50% = 8
  24. #define LEDC_FREQUENCY     (3000000)            //Setting PWM Frequency (Hz)
  25.  
  26.  
  27. /*
  28.  * pwm_init
  29.  *
  30.  * Initialise PWM controller
  31.  */
  32. static void pwm_init(void)
  33. {
  34.     // Prepare and then apply the LEDC PWM timer configuration
  35.     ledc_timer_config_t ledc_timer = {0};
  36.     ledc_timer.speed_mode       = LEDC_MODE;
  37.     ledc_timer.timer_num        = LEDC_TIMER;
  38.     ledc_timer.duty_resolution  = LEDC_DUTY_RES;
  39.     ledc_timer.freq_hz          = LEDC_FREQUENCY;
  40.     ledc_timer.clk_cfg          = LEDC_AUTO_CLK;
  41.  
  42.     ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  43.  
  44.     // Prepare and then apply the LEDC PWM channel configuration
  45.     ledc_channel_config_t ledc_channel = {0};
  46.     ledc_channel.speed_mode     = LEDC_MODE;
  47.     ledc_channel.channel        = LEDC_CHANNEL;
  48.     ledc_channel.timer_sel      = LEDC_TIMER;
  49.     ledc_channel.intr_type      = LEDC_INTR_DISABLE;
  50.     ledc_channel.gpio_num       = LEDC_OUTPUT_IO;
  51.     ledc_channel.duty           = 0;
  52.     ledc_channel.hpoint         = 0;
  53.     ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  54.  
  55.  
  56. }
  57.  
  58.  
  59. /*
  60.  * app_main
  61.  * Program entry point
  62.  */
  63. void app_main(void)
  64. {
  65.  
  66.     pwm_init();
  67.     // Set duty to 50%
  68.     ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
  69.     // Update duty to apply the new value
  70.     ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  71.     while(1);
  72. }
  73.  

ESP_Sprite
Posts: 9591
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3-DevKitM-1 PWM problem

Postby ESP_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 106 guests