As5600 code question

#include <SimpleFOC.h>

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

BLDCMotor motor = BLDCMotor(4);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8);

float target_angle = 0;

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

void setup() {



driver.voltage_power_supply = 12;


motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

motor.controller = MotionControlType::angle;

motor.PID_velocity.P = 0.2f;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0;

motor.voltage_limit = 4;

motor.LPF_velocity.Tf = 0.01f;

motor.P_angle.P = 20;

motor.velocity_limit = 50;





command.add(‘T’, doTarget, “target angle”);

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

void loop() {



When using this code the motor is fixed but not moving.
What’s the matter?

Ok, this code should run the motor, but it is hard to say what is wrong from your description…

Which MCU are you using? Which driver are you using?

Have you investigated the electrical side? Is the driver powered, and does it receive PWM signals from the MCU?

Have you taken care to protect the system? The best is to use a PSU with a current limit. Then you can also see if the current limiting on the PSU trips, then probably there is something wrong with the setup or code.

You have 4 pole-pairs as the argument to the constructor. Is it correct? 4PP motors are usually in-runners, and fast-turning with low winding resistance. In this case, are you sure the voltage limit of 4V is low enough?

Regarding closed loop mode, is the sensor working? You uploaded the sensor sketch in another post, was this working as expected, returning 2PI radians for one revolution of the motor in the positive direction?
Usually it is good to try the open loop mode first (with a low voltage limit!) to check the motor and driver are working. And then to do the sensor test, to make sure the sensor is working. And then to combine the two and do the closed loop test, first in torque-voltage mode.

In this code you are using angle mode - for this you need to have working closed loop torque voltage mode, and a tuned PID for velocity mode. It’s the most complicated mode without current sensing. So I would make sure first that open-loop is working, then that the sensor is working, then closed-loop torque voltage mode, then velocity mode (and tune the PID well), and only then try angle mode, when all these other things are working…

These are some thoughts I have, I hope it is helpful for you!