ESP32: Stepper not rotating

I have an ESP32 with an AS5600 connected via I2C and a NEMA17 stepper motor connected to the L298N.

I connected the pins like this on the ESP32:

  • AS5600 SCL → D22
  • AS5600 SDA → D21
  • L298N EnA → D32
  • L298N In1 → D33
  • L298N In2 → D25
  • L298N In3 → D26
  • L298N In4 → D27
  • L298N EnB → D23

The two cables forming a pole are connected to Out1 and Out2 and the other pole is connected to Out3/4.

The L298N has 12V power from an external power supply. The grounds between the ESP32 and L298 are shared.

The code:

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

// AS5600 driver, I2C, these pins are hardcoded on the ESP32
#define ENC_I2C_SCL 22
#define ENC_I2C_SDA 21

// Pins for L298N driver
#define STEPPER_CH1_EN 32
#define STEPPER_L298N_IN1 33
#define STEPPER_L298N_IN2 25
#define STEPPER_L298N_IN3 26
#define STEPPER_L298N_IN4 27
#define STEPPER_CH2_EN 23

#define STEPPER_R 1.65

/**
 * @brief Pole pair number
 * Most of the stepper motors are 200 step per rotation motors making them 50 pole pair motors.
 * In practice you can know the pole_paris number by dividing the number of steps per rotation by 4.
 * If you are not sure what your pole_paris number is.
 * The library provides you an example code to estimate your pole_paris number in the examples examples/utils/calibration/find_pole_pairs_number.ino.
 */
StepperMotor motor = StepperMotor(50); //, STEPPER_R);
StepperDriver4PWM driver = StepperDriver4PWM(STEPPER_L298N_IN1,
                                             STEPPER_L298N_IN2,
                                             STEPPER_L298N_IN3,
                                             STEPPER_L298N_IN4,
                                             STEPPER_CH2_EN,
                                             STEPPER_CH1_EN);

// SDA 21
// SCL 22
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
Commander command = Commander(Serial);

void doMotor(char *cmd) { command.motor(&motor, cmd); }

void setup() {
    Serial.begin(115200);

    sensor.init();
    motor.linkSensor(&sensor);

    // driver.pwm_frequency = 50000;
    driver.voltage_power_supply = 12;
    driver.init();
    driver.enable();

    motor.linkDriver(&driver);
    // choose FOC modulation
    motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

    motor.controller = MotionControlType::angle;

    command.add('M', doMotor, strdup("motor"));
    motor.useMonitoring(Serial);
    motor.init();
    motor.initFOC();

    Serial.println("Motor ready.");
    // put your setup code here, to run once:
    motor.target = 10;
    _delay(1000);
}

void loop() {
    // main FOC algorithm function
    motor.loopFOC();
    motor.move();

    motor.monitor();

    // user communication
    command.run();
}

Console output:

MOT: Monitor enabled!
MOT: Init
MOT: Enable driver.
MOT: Align sensor.
MOT: sensor_direction==CW
MOT: PP check: fail - estimated pp: 110.70
MOT: Zero elec. angle: 2.57
MOT: Ready. 
Motor ready.
10.0000 12.0000 -0.0617 0.9587
10.0000 12.0000 -0.0381 0.9587
10.0000 12.0000 0.1545  0.9603
10.0000 12.0000 -0.0299 0.9587
10.0000 12.0000 -0.1327 0.9587

The values of the magnetic sensor update and are plausible, e.g they increase in the one direction and decrease in the other direction.

The controller is resisting the movement, but I think it is just randomly powering the pole by chance. It doesn’t return to the commanded position when overpowered. Velocity control also doesn’t work.

I tried switching the pins and it doesn’t really make a difference what I do. Right now I don’t know where the problem is and I am not sure where to look as I have never used this library before.