Can anyone help in Creating PWM signal of 1mS to 2mS using ledC function of ESP32?

I want to generate PWM signal from ESP 32 on board pins using LedC library functions.

/**
 * Sets up a channel (0-15), a PWM duty cycle frequency, and a PWM resolution (1 - 16 bits) 
 */
double      ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);

/**
 * Writes out a duty cycle value to the specified channel. The duty cycle value should be
 * between 0 - 2^PWM resolution
 */
void        ledcWrite(uint8_t channel, uint32_t duty);

/**
 * Attach the given pin to the specified channel (0-15)
 */
void        ledcAttachPin(uint8_t pin, uint8_t channel);

/**
 * Detach the previously attached pin
 */
void        ledcDetachPin(uint8_t pin);

Where are you stuck ?

Have you looled at the source code for the analogWrite() function of the ESP that became available recently ?

static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 };
static int cnt_channel = LEDC_CHANNELS;
void analogWrite(uint8_t pin, int value) {
  // Use ledc hardware for internal pins
  if (pin < SOC_GPIO_PIN_COUNT) {
    if (pin_to_channel[pin] == 0) {
      if (!cnt_channel) {
          log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS);
          return;
      }
      pin_to_channel[pin] = cnt_channel--;
      ledcAttachPin(pin, cnt_channel);
      ledcSetup(cnt_channel, 1000, 8);
    }
    ledcWrite(pin_to_channel[pin] - 1, value);
  }
}

"Creating PWM signal of 1mS to 2mS"

Are you trying to drive one or more servos? Maybe this article will help:

Hi,
ESP32 LEDc resolution is calculated as follows:

Calculation of adjustments for each frequency band:
Resolution = log2(Clock(ESPClock/2)/f) + 1.
Ex: Frequency = 50,000 Hz
ESP Clock/2 = 80,000,000
Resolution = 80,000,000/50,000 = 1,600 ....log2(1600) = 10 + 1 = 11
Resolution for 50KHz is 11.

Duty = (2** Resolution);
Duty 50% = (2** Resolution) /2;
Duty 20% = (2** Resolution) /5;
Duty 50% = (2** Resolution)/2 ex: 2**11 = 2048 2048/2 = 1024

1 ms = 1000Hz Resolution = 80,000,000/1,000 = 80,000 ....log2(80,000) = 16 + 1 = 17
...........Duty 50% = 2**17 = 131,072 .... 131,072/2 = 65,536

2 ms = 500Hz Resolution = 80,000,000/500 = 160,000 ....log2(160,000) = 17 + 1 = 18
...........Duty 50% = 2**18 = 262,144 .... 262,144/2 = 131,072

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.