Ledc library with esp32 and coreless motors

hello, im using ESP32 pico kit and i have to use ledc function to configurate four coreless motors,
i used this code and the Project compile correctly but ledc methodes stay in black and motors don't rotate.
can anyone help me?

const int A1 = 0;
const int A2 = 4;
const int D1 = 2;
const int D2 = 15;
const int freq = 5000;
const int A1CH = 1;
const int A2CH = 0;
const int D1CH = 2;
const int D2CH = 3;
const int resolution = 16;
void setup() {
    ledcSetup(A1CH, freq, resolution);
    ledcSetup(A2CH, freq, resolution);
    ledcSetup(D1CH, freq, resolution);
    ledcSetup(D2CH, freq, resolution);
    
    ledcAttachPin(A1, A1CH);
    ledcAttachPin(A2, A2CH);
    ledcAttachPin(D1, D1CH);
    ledcAttachPin(D2, D2CH);
             
}

void loop() {
   for(int i=100;i<255;i=i+5)
   {
    ledcWrite(A1CH,i);
    ledcWrite(A2CH,i);
    ledcWrite(D1CH,i);
    ledcWrite(D2CH,i);
    delay(10);
   }
    
}

Check out the code for ESP32servo.cpp

I consider using the ESP32 ledc API to control motors is 2nd best. The better option would be to use the ESP32 MCPWM, the code looks harder but the motor control is much better. If you search on this site, you can find where I have posted my MCPWM code using the Arduino IDE, which should get you started.

Here is the link t the ESP32servo.cpp

The MCPWM API can be found here. The MCPWM has its own internal timers, instead of using one of the internal hardware timers.

Ledc calculations:

ledcSetup (A1CH, freq, resolution);

A1CH = channel
freq = frequency
Resolution = log2 (Clock (80MHz) / f) + 1
          f ESP Clock
  ex: 5,000 HZ = 80,000,000 / 5,000 = 16,000
  log2 (16,000) = ~ 14 + 1 = 15

Duty 50% = (2 ** Resolution) / 2

ex: 2**15 = 32.768 32.768/2 = 16.384

ledcAttachPin (A1, A1CH);
A1 = ESP32 pin
A1CH = channel

Duty
ledcWrite (A1CH, i);
AICH = channel
i = Duty

See example in this link:

RV mineirin

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