ESP32-S3 Control Issues

Hi everyone,

I am currently working on an esp32-s3 with Simplefoc shield v2.0.4. I am using AS5048A magnetic encoder and Cubemars GL40 motor. You can check the test evrimoment from the image.

You can also check the code that I am using

#include <Arduino.h>
#include <SimpleFOC.h>

BLDCMotor motor = BLDCMotor(14, 2.25, 70); 
BLDCDriver3PWM driver = BLDCDriver3PWM(15,16,17,7);
InlineCurrentSense currentSense = InlineCurrentSense(0.01, 50, 40, 41);
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);

float target = 0;

Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target, cmd); }
void doCommand(char* cmd) { command.motor(&motor, cmd); }

void sensor_test(void)



void setup() {

  // sensor

  driver.voltage_power_supply = 16;

  //current sense

  //sensor index
  motor.voltage_sensor_align = 1;
  motor.velocity_index_search = 3;

  //motion control
  motor.torque_controller = TorqueControlType::foc_current;
  motor.controller = MotionControlType::torque;
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

  motor.motion_downsample = 0.0;

  // PID
  motor.PID_velocity.P = 0.2;
  motor.PID_velocity.I = 10;
  motor.PID_velocity.D = 0;
  motor.PID_velocity.output_ramp = 100.0;
  motor.PID_velocity.limit = 0.3;

  motor.LPF_velocity.Tf = 0.1;

  motor.P_angle.P = 20.0;
  motor.P_angle.I = 0.0;
  motor.P_angle.D = 0.0;
  motor.P_angle.output_ramp = 0.0;
  motor.P_angle.limit = 1000.0;

  motor.LPF_angle.Tf = 0.0;

  // motor.PID_current_q.P = 3.0;
  // motor.PID_current_q.I = 300.0;
  // motor.PID_current_q.D = 0.0;
  // motor.PID_current_q.output_ramp = 0.0;
  // motor.PID_current_q.limit = 7.0;

  // motor.LPF_current_q.Tf = 0.02;

  // motor.PID_current_d.P = 3.0;
  // motor.PID_current_d.I = 300.0;
  // motor.PID_current_d.D = 0.0;
  // motor.PID_current_d.output_ramp = 0.0;
  // motor.PID_current_d.limit = 7.0;

  // motor.LPF_current_d.Tf = 0.02;

  motor.velocity_limit = 50.0;

  motor.current_limit = 2.0;

  //motor.modulation_centered = 1.0;

  motor.monitor_variables = _MON_VEL | _MON_TARGET | _MON_CURR_D | _MON_CURR_Q;


  command.add('T', doTarget, "target angle");
  command.add('M', doCommand, "motor command");

  Serial.println(F("Motor ready."));
  Serial.println(F("Set the target using serial terminal:"));

void loop() {
#if 1

Here is the issue; I am trying to drive in a torque control mode. When I tried to debug from Simplefoc studio, Current Q and Current D do not respond to anyting. They remain 0 and do not give responds to any change I made. When I give target like 0.1A, driver gives full force up to 2.3A (I checked from Oscilloscope). Even I change target to 0, it remains like that and do not stop. Also there is a shaking issue on motor drive. You can check screenshots from the below.

as seen in the upper image, Current Q remain 0 and do not goes to target. Also there is a shaking issue that I mentioned can be seen in velocity graph.

I couldn’t solve the issue. Any idea to solve? I am glad to hear any advice. Thanks

Hey @YigitArda ,

Have you tried tuning the current_PID? It seems you’ve commented it out in your code…

Is it working well in torque-voltage mode and velocity mode?

You have a nice setup!


Yes I tried current PID however as I said it current values do not gives responses to me all I can see it just 0. torque voltage mode is working same while velocity mode is completly disaster. It just shaking in velocity mode.

I also tried same setup and variant of code in ESP32-wroom and it worked fine.

Before you can try the current mode, you should make sure torque-voltage and velocity mode are working as expected.
Sounds like torque-voltage is working?
If so, then the next step is to tune your PIDs and LPF for velocity mode, and get that working.

Then, you can check the current sensing - check it first in the velocity mode by printing the values and seeing if they make sense. If you’re getting only 0s it means something is wrong, and you’ll have to find this problem first.

Sadly, voltage torque mode is giving same results with foc-torque mode. I tried to change every possible parameter and I couldn’t change anything, it do not give me any response. Also openloop-velocity is not working with the ESP32-S3. I tried with 2 different s3 modules to check if they works but they seems fine.

Could something be wrong about S3 library? It seems kinda different from the wrooms library. I tried the code and setup with STM32 F4 series and an esp32-wroom and they were fine as I mentioned before.

Ok, if even open loop is not working, then the problem is already at a very low level, probably to do with the PWM. Normally, the S3 should be well supported. So if open loop is not even working for you, you have to check the chip is outputting PWM. If it is, then the problem is with the driver. If the chip isn’t outputting PWM, it is a software or configuration issue.

Are you using PlatformIO or ArduinoIDE?

@runger hey,

I think I found something. Code was stucking in init functions so I start an debug section and code stucks in adcRead() function.

I couldn’t solve that part right now. Therefore I changed adcRead() function with analogread() function.

with this change I can drive the motor but it is not the best. How can I fix the adcRead funtion any idea?

Hi @YigitArda ,

Thank you very much for reporting this.

I will look into it. Can I ask you for some details?

  • Which GPIO pin are you using / which ADC is this on? InlineCurrentSense currentSense = InlineCurrentSense(0.01, 50, 40, 41);
    These PIN numbers look high to me, which model ESP32 is it?
  • Are you using WiFi also?

– edit –

Looking at the pinout diagrams for the ESP32 S3, are you sure you can use these pins, 40,41,50? I think you have to use ADC pins GPIO1-10 or GPIO11-20.
And if you want to use WiFi at the same time I think it has to be pins GPIO1-10 on ADC1 because you can’t use ADC2 while WiFi is on.

adcRead is an optimized function from simplefoc for the esp series as the normal analogRead function takes far too long for use in the control loop. It could be that there is some kind of problem in adcRead when you use an S3 but as runger said:

So I would advice to first try using pins from ADC1 and switching back to adcRead before exploring more complicated possible answers to your problem.