analogWrite doesn't work but digitalWrite does on esp32 when BLDC used

Hey,
I am using an esp32 Wroom 32 dev board and I'm having trouble controlling a led with the analogWrite() function. I am using the simpleFOC libary to control a BLDC and it is working just fine. When I write a code only to fade the led it is working. But when I control the BLDC, the analogWrite function seems to stop working, only digitalWrite works. If I change the led control in this code to digitalWrite functions it will work. Does anyone has an idea what is going on?


#include <SimpleFOC.h>
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
BLDCMotor motor = BLDCMotor(11);
BLDCDriver6PWM driver = BLDCDriver6PWM(26, 25, 27, 32, 12, 33);
float target_voltage = 0.5;
Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }

void setup() {

  pinMode(4,OUTPUT);
  analogWrite(4,50);

  sensor.init();
  motor.linkSensor(&sensor);
  driver.voltage_power_supply = 5;
  driver.init();
  motor.linkDriver(&driver);
  motor.voltage_sensor_align = 5;
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  motor.controller = MotionControlType::torque;
  Serial.begin(115200);
  motor.useMonitoring(Serial);
  motor.init();
  motor.initFOC();
  command.add('T', doTarget, "target voltage");
  Serial.println(F("Motor ready."));
  Serial.println(F("Set the target voltage using serial terminal:"));
  _delay(1000);
}

void loop() {
  motor.loopFOC();
  motor.move(target_voltage);
  analogWrite(4,100)
  command.run();
}

The Nano ESP32 section of the forum is specific for the Arduino Nano ESP32. Hence your topic has been moved to a more suitable location on the forum.

Without looking closely at the library I can only guess.
The analogWrite function uses one of the timers to generate PWM.
Maybe the library uses the same timer?

Yes, that was the problem. I used the ledc library for separate timing and now it works flawlessly. Thank you!

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