Esp32 + l298n + as5600 [Solved: wrong pins]

Hi there,

I’ve just set up ESP32 with L298N motor driver and AS5600 sensor.

The sensor reads perfectly well using the basic getting started code for sensor only, so there’s no problem there.

I’m using the Example code for the Backdrivable Stepper Motor (slightly modified for AS5600 sensor) and I’m not sure how to control it.

When I plug in the ESP32, the motor aligns to the sensor successfully. But what now?

I’ve tried setting targets on the serial monitor using commands like M0.5 M10 M200 but all it does is increase power supply current.

  • How do I make it spin in one direction?
  • How do I set position in torque control mode?
  • How do I set speed in torque control mode?

I’ve tried switching to Velocity control mode (MC3) but I get some very erratic results with horrible noise and vibrations going backwards and forwards… I had to unplug it for fear of frying something (already smoked up one L298N by driving it using 24v using this library!)

The only value that produces forward motion in velocity mode seems to be M10 where I get a smooth spin in one direction. But any other value (M0.5, M100, M15) produces erratic backwards and forwards motion.

M-10 also doesn’t change direction in velocity mode… only spins in one direction.
M6 seems to make it spin in the opposite direction, and then M6.1 and M6.2 changes the speed slightly. But that’s about it… no other values work.

My L298N was extremely hot throughout the test. Motor voltages all set to 12v. Power supply is currently 12v.

EDIT: I changed the motor voltage to 3.7 (stepper model datasheet voltage) and the driver is now running cooler. Excessive noise is also gone. But I still have no idea how to set direction, angle, etc! It all just goes in random directions and spins the other way when you try to stop it manually…

All I want is to just get the wheel spinning in one direction for a start, then to be able to set speed and position.
Thanks!
TwinLizzie

//Code:
#include <SimpleFOC.h>

// Stepper motor instance
StepperMotor motor = StepperMotor(50);
// Stepper driver instance
StepperDriver4PWM driver = StepperDriver4PWM(34, 35, 32, 33,  18, 19);

// encoder instance
//Encoder encoder = Encoder(A1, A2, 2048);
// channel A and B callbacks
//void doA(){encoder.handleA();}
//void doB(){encoder.handleB();}

MagneticSensorI2C as5600 = MagneticSensorI2C(0x36, 12, 0x0E, 4);

// commander interface
Commander command = Commander(Serial);
void onMotor(char* cmd){ command.motor(&motor, cmd); }

void setup() {

  // initialize encoder sensor hardware
  as5600.init();
  //encoder.enableInterrupts(doA, doB); 
  // link the motor to the sensor
  motor.linkSensor(&as5600);

  // choose FOC modulation
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

  // power supply voltage [V]
  driver.voltage_power_supply = 12;
  driver.init();
  // link the motor to the sensor
  motor.linkDriver(&driver);

  // set control loop type to be used
  motor.controller = MotionControlType::torque;

  // controller configuration based on the control type 
  motor.PID_velocity.P = 0.2;
  motor.PID_velocity.I = 20;
  motor.PID_velocity.D = 0;
  // default voltage_power_supply
  motor.voltage_limit = 3.7;

  // velocity low pass filtering time constant
  motor.LPF_velocity.Tf = 0.01;

  // angle loop controller
  motor.P_angle.P = 20;
  // angle loop velocity limit
  motor.velocity_limit = 50;

  // use monitoring with serial for motor init
  // monitoring port
  Serial.begin(115200);
  // comment out if not needed
  motor.useMonitoring(Serial);

  // initialise motor
  motor.init();
  // align encoder and start FOC
  motor.initFOC();

  // set the initial target value
  motor.target = 2;

  // define the motor id
  command.add('M', onMotor, "motor");

  // Run user commands to configure and the motor (find the full command list in docs.simplefoc.com)
  Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V."));
  
  _delay(1000);
}


void loop() {
  // iterative setting FOC phase voltage
  motor.loopFOC();

  // iterative function setting the outter loop target
  // velocity, position or voltage
  // if tatget not set in parameter uses motor.target variable
  motor.move();

  // user communication
  command.run();
}

Did you try these commands? MC0, MC1, MC2, … ?

  • C - Motion control type config
    • D - downsample motion loop
    • 0 - torque
    • 1 - velocity
    • 2 - angle
    • 3 - velocity_openloop
    • 4 - angle_openloop

Hi, and welcome, TwinLizzie!

See if you can get it spinning in open loop velocity mode first. That will verify that your basic parameters (things like the number of pole pairs, PWM pins used, etc) are working correctly.
Then you can progress to closed loop mode, perhaps velocity mode first. For closed loop your PID parameters may need to be tuned. Some motors spin quite well with the “example” parameters, but others need careful tuning before you get a good result.

The L298N is very resilient, but I think you did the right thing to turn the voltage down a bit. No need to stress it out during the initial tests, unless your motor is huge a low voltage will move it just fine.

It’s also a good idea to do initial tests with the motor unloaded (if you weren’t doing this anyway) just to exclude any mechanical problems during the first trials.

Let us know how it goes!

Hey there!

Makes sense that it could be a PID problem!

Speaking of which, I am getting strange errors when querying the PID values:
PID curr q| err
PID curr d| err
PID vel| err
PID angle| err
Limits| err

I also can’t get SimpleFOC studio running. It just spams “unknown cmd error” and doesn’t show any values in the tree view whatsoever.

This is strange.

Hi TwinLizzie,

Did you enter “M” here in SimpleFOC Studio?
image

Thanks, got SimpleFOC studio working now!

Still hung up here with the motor for now. Tried changing PID parameters (MVP, MVI, MVD), doesn’t make any difference. I’m using velocity open loop to get the motor spinning, but very few values produce anything other than erratic vibration with no spin.

I checked my pins, everything connected properly. The order is like this, am I right? ->

(IN1, IN2, IN3, IN4, ENB, ENA)

EDIT:
I’d happily try the motor tuning tutorial. It’s just that my instincts tell me this isn’t what’s wrong with it… I’m worried I might spend a whole day working on it with no results.

The only thing that seems to be making a difference is increasing current slightly (MLC1, MLC1.5) but it’s still very unusable. Phase resistance was also set (3.7) with little difference.

Just some additional info. The stepper I’m using is:

Cabling when working with a breadboard is a very frequent source of problems. It has happened to me more than once that after disconnecting everything and reconnecting it exactly the same as in the beginning, it worked … obviously something changed but it can solve problems that you were not aware you had on your cabling … other times does not solves anything :upside_down_face:

1 Like

Yeah. I did have some cabling problems with my encoder in the beginning (the ground pin header literally popped off of the AS5600 module in the beginning… had to resolder!)

But at the moment all connections seem fine. Even did continuity tests with multimeter… everything has continuity. Not using a breadboard btw… just straight up jumper cables.

I might try this another day with a BLDC motor, if I ever need one. I’ll also try it with another stepper motor.

Thought I’d give this library a try now that my AS5600 arrived, as I need closed loop control for my robotics projects… But yeah… SimpleFOC is unlikely to replace my standard FastAccelstepper + step/direction system for now!

EDIT:
Thanks for the help btw. Here’s a pic of my wiring for more info…

Current status: Sensor works, code works, but motor doesn’t turn reliably except at very specific torque values and speeds. Just makes noise without moving mostly:

1 Like

Never used SimpleFOC to move a stepper motor but with BLDC motors with a low phase resistance setting motor.current_limit to a low value can solve some problems. Do not know if it apply or setup.

PS: very nice planetary gear :slight_smile:

Update:

I think I’m close to understanding what the hell is going on. GPIO pin 34 on ESP32 seems unusable. I was currently testing it by using it as an Enable pin for a standard DRV8825 stepper driver with step/direction system (not the SimpleFOC library). It was rendering my stepper immobile! Changed it to pin 33 and it started working fine again.

Maybe if I change the GPIO pin 34 to something else I might be able to fix this problem! Will test shortly on SimpleFOC once I finish my experiments with the DRV8825 and will then report back to you.

EDIT:


Apparently Pin 34 and Pin 35 are input only. What a week!

Everything solved now.

Please rename this thread to: ESP32 + L298N + AS5600 (SOLVED: WRONG PINS!)

Works pretty well although I notice that there is a speed limit and lots of heat being produced by the L298N driver.

I’m still struggling with MC2 (angle mode) but again I’ll leave that up for another project as I’ll be sticking with the STEP/DIRECTION system for my robot arms.

Thanks once again for the help!

2 Likes

Hey, I’m so glad you found it!

We’ll remember this one for the next time someone has ESP32 problems they can’t explain. Thanks a lot!

This is my goto page for ESP-32 WROOM.

2 Likes