# Motor doesn't turn in FOC mode

**URL:** https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144
**Category:** hardware support
**Created:** [October 4, 2020, 7:05am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144 "2020-10-04T07:05:33Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 4, 2020, 7:05am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/1 "2020-10-04T07:05:33Z")

</div>

Hi everyone

I recently got the SimpleFOCShield but I’m having trouble setting it up and using it with FOC.

**Hardware**

- BGM5208-75HS 24N22P
- AS5600 (in the white PCB version from AliExpress), connected over I2C with 4.7K pull ups
- Bench Power Supply set to 12V and current limited to under 1A because the motor was getting hot above that

Given these informations, I was setting up the motor and sensor using the following arguments.

```
BLDCMotor motor = BLDCMotor(9, 10, 6, 11, 8);
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);

```

From the SimpleFOC examples, I was able to run the magnetic sensor test with stable read outs as well as the open loop voltage control. The find pole pair util example returns a very inconsistent number of pole pairs, almost every run returns a different value. According to its data sheet it’s a 24N22P motor, so I’m assuming the number of pole pairs is 11, is that correct?

Following [the docs](https://docs.simplefoc.com/foc_shield_code), I wanted to try voltage control with FOC, however the motor wouldn’t move. As per a [suggestion from another thread](https://community.simplefoc.com/t/a2212-13t-bldc-motor-and-problems/74/9), I put `motor.monitor()` into the `loop()` function and the sensor data seems to be correct.

Do you have any ideas what could be the issue here? I’d appreciate any suggestion.

Cheers,  
Lucius

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 4, 2020, 7:41am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/2 "2020-10-04T07:41:50Z")

</div>

Hey Lucius,

This can very well be a hardware issue.  
Did you try to run your motor in the open loop mode?

> [@lulezi](#):
>
> From the SimpleFOC examples, I was able to run the magnetic sensor test with stable read outs as well as the open loop voltage control. The find pole pair util example returns a very inconsistent number of pole pairs, almost every run returns a different value. According to its data sheet it’s a 24N22P motor, so I’m assuming the number of pole pairs is 11, is that correct?

This should be right, it should have 11 pole pairs.

Try running the velocity open loop library example. The motor should turn smoothly for decent range of velocities (up to 20rad/s).  
If it doesn’t, this could suggest either motor or sensor hardware issue.

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 4, 2020, 8:12am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/3 "2020-10-04T08:12:55Z")

</div>

Thanks Antun!

I’ve tried the open loop example and it works without any problems. I also added `motor.linkSensor()` and `motor.monitor()` to the example and it shows seemingly correct values.

---

<div class="post-metadata">

### Author: ![Owen\_Williams](https://community.simplefoc.com/user_avatar/community.simplefoc.com/owen_williams/32/105_2.png) [@Owen\_Williams](https://community.simplefoc.com/u/Owen_Williams)
#### Post date: [October 4, 2020, 8:13am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/4 "2020-10-04T08:13:27Z")

</div>

Do you know the winding resistance of your motor? Hopefully it is over 2ohms.  
Settin psu to 1A is very sensible but should be used like a fuse i.e. you should be avoiding hitting it through your config in software. Do you have a ‘motor.voltage\_limit’ ? E.g to a low 1.5 initially. Go back to your working openloop and you’ll notice current on psu will drop.

Are you calling motor.initFOC()? Does it move clockwise then anti clockwise? When call initFOC() you should probably set motor.voltage\_sensor\_align to something sensible e.g similar to voltage\_limit.

The initFOC is supposed to detect whether the natural direction of motor agrees with the natural direction of your sensor, but it might be worth swapping two of your motor cables around to reverse the direction of your motor. Your symptoms might be explained by this.

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 4, 2020, 8:33am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/5 "2020-10-04T08:33:35Z")

</div>

Thanks Owen!

The winding resistance is about 6 Ω. I just tried `motor.voltage_limit = 1.5` in the open loop example. This works up to a `target_velocity` of `15`, after which the motor won’t turn. I increased the limit to `3`, which allows the motor to turn without problems even at `target_velocity = 20`.

Back to the FOC voltage example, I set `motor.voltage_sensor_align = 3`. It turns in both directions during `motor.initFoc()`, [but does so in “steps” instead of a smooth motion](https://www.dropbox.com/s/du2m1loy412xral/2020-10-04%2010.24.58.mov?dl=0).

Would it make sense to use both `voltage_limit` and `voltage_sensor_align` or does one prevent the other from having any impact?

I tried swapping the motor cables around which unfortunately lead to no improvement.

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 4, 2020, 11:29am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/6 "2020-10-04T11:29:58Z")

</div>

It is an interesting problem. So open-loop works and sensor works but the FOC doesn’t 😃  
This is very strange.

> [@lulezi](#):
>
> Back to the FOC voltage example, I set `motor.voltage_sensor_align = 3` . It turns in both directions during `motor.initFoc()` , [but does so in “steps” instead of a smooth motion](https://www.dropbox.com/s/du2m1loy412xral/2020-10-04%2010.24.58.mov?dl=0).

This is the initialization procedure at this moment. You can override it by adding the parameters to the `motor.initFOC(zero_offset, direction)` and these two you can get by running the `find_sensor_offset_and_direction.ino` example.  
But for this to work you need a functional FOC algorithm 😃

> [@lulezi](#):
>
> Would it make sense to use both `voltage_limit` and `voltage_sensor_align` or does one prevent the other from having any impact?

This will not impact much in your case, at least for the moment I think.

Some more questions:

- What is the microcontroller you are using?
- What library version are you using?

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 4, 2020, 12:09pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/7 "2020-10-04T12:09:29Z")

</div>

I’m using an Arduino UNO with SimpleFOC v1.5.0.

Do the motor movements in the video above look correct?

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 4, 2020, 12:41pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/8 "2020-10-04T12:41:55Z")

</div>

Yes this is correct. The initialization should look that way. Just the last movement when it goes to the right is not good. It should not stop that way.

Could you please show us the output of your serial monitor. You can just dump it here

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 4, 2020, 1:30pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/9 "2020-10-04T13:30:35Z")

</div>

```
MOT: Monitor enabled!
MOT: Init pins.
MOT: PWM config.
MOT: Enable.
MOT: Align sensor.
MOT: Absolute zero align.
MOT: Success!
MOT: Motor ready.
Motor ready.
Set the target voltage using serial terminal:

```

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 4, 2020, 1:55pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/10 "2020-10-04T13:55:18Z")

</div>

Everything is fine in the log.  
I am really not sure what to suggest as a next step.

Could you check also what is the percentage of the Arduino memory used with this example for your Arduino. Sometimes is memory related problems cause the problems that are not easy to debug.

And also, since there aren’t many things left. can you put your show us your full code.

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 4, 2020, 2:21pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/11 "2020-10-04T14:21:31Z")

</div>

Here’s the memory status on upload:

```
Sketch uses 17606 bytes (54%) of program storage space. Maximum is 32256 bytes.
Global variables use 1374 bytes (67%) of dynamic memory, leaving 674 bytes for local variables. Maximum is 2048 bytes.

```

And the code as I just ran it:

```
/**
 * Torque control example using voltage control loop.
 * 
 * Most of the low-end BLDC driver boards doesn't have current measurement therefore SimpleFOC offers 
 * you a way to control motor torque by setting the voltage to the motor instead hte current. 
 * 
 * This makes the BLDC motor effectively a DC motor, and you can use it in a same way.
 */
#include <SimpleFOC.h>

BLDCMotor motor = BLDCMotor(9, 10, 6, 11, 8);
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);

void setup() {

  // initialise magnetic sensor hardware
  sensor.init();
  // link the motor to the sensor
  motor.linkSensor(&sensor);

  // power supply voltage
  // default 12V
  motor.voltage_power_supply = 12;
  // aligning voltage 
  motor.voltage_sensor_align = 3;
  
  // choose FOC modulation (optional)
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

  // set motion control loop to be used
  motor.controller = ControlType::voltage;

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

  // initialize motor
  motor.init();
  // align sensor and start FOC
  motor.initFOC();

  Serial.println("Motor ready.");
  Serial.println("Set the target voltage using serial terminal:");
  _delay(1000);
}

// target voltage to be set to the motor
float target_voltage = 2;

void loop() {

  // main FOC algorithm function
  // the faster you run this function the better
  // Arduino UNO loop ~1kHz
  // Bluepill loop ~10kHz 
  motor.loopFOC();

  // Motion control function
  // velocity, position or voltage (defined in motor.controller)
  // this function can be run at much lower frequency than loopFOC() function
  // You can also use motor.move() and set the motor.target in the code
  motor.move(target_voltage);
  
  // communicate with the user
  serialReceiveUserCommand();
}

// utility function enabling serial communication with the user to set the target values
// this function can be implemented in serialEvent function as well
void serialReceiveUserCommand() {
  
  // a string to hold incoming data
  static String received_chars;
  
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the string buffer:
    received_chars += inChar;
    // end of user input
    if (inChar == '\n') {
      
      // change the motor target
      target_voltage = received_chars.toFloat();
      Serial.print("Target voltage: ");
      Serial.println(target_voltage);
      
      // reset the command buffer 
      received_chars = "";
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 5, 2020, 9:24am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/12 "2020-10-05T09:24:57Z")

</div>

I am not sure what is really happening here.

Could you please show us `motor.monitor()` output for this behavior.  
Maybe is it some kind of I2C problem, where sensor stops working.

Also, what happens when the motor stops. Is it locked in the position or you can move it. Does it ever diverge - starts rotating very fast in some direction?

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 5, 2020, 7:48pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/13 "2020-10-05T19:48:51Z")

</div>

I pasted the output of `motor.monitor()` [into a gist](https://gist.github.com/lulezi/508ed419d53f77c7794d5052d7bdf3b6).

When the motor stops, I need quite some force to turn it by hand. One time the motor was jerking back and forth like crazy at `target_voltage = 10.0`, but I couldn’t reproduce that behaviour. Normally, it doesn’t move at all.

Edit: Just to clarify, the changing values in the output above are caused by me turning the motor by hand.

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 5, 2020, 7:51pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/14 "2020-10-05T19:51:29Z")

</div>

I saw that SimpleFOC also works with the ESP32. I’ll give that a try tomorrow. If there’s no difference, I’ll order the motor again to rule out a hardware issue. The sensor is not the problem — I’ve already tried another one.

---

<div class="post-metadata">

### Author: ![Owen\_Williams](https://community.simplefoc.com/user_avatar/community.simplefoc.com/owen_williams/32/105_2.png) [@Owen\_Williams](https://community.simplefoc.com/u/Owen_Williams)
#### Post date: [October 5, 2020, 9:18pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/16 "2020-10-05T21:18:07Z")

</div>

I’ve tried your code on my simplefoc board (which is connected to an esp32). I’m using an as5600 with a turnigy 2804 10ohm gimbal.

The only thing I changed were the motor to match my pins and pole pairs (`BLDCMotor(17,16,4,7,0)`)

It worked.

I have a couple of suggestions. Change:

```auto
float target_voltage = 0.5;

```

Why? For me starting at 2 is already fast for my motor. Yours will be faster because your resistance is lower (6 ohms). Thinking about this - I don’t think this is likely to help as I think closed loop shouldn’t be affected by ‘acceleration’ issues.

The other suggestion is to start again. Get your velocity\_openloop working. Separately get your i2c working. Check that you’ve not accidentally copied over the wrong motor/sensor config or moved a wire. Still good?

Go back to the voltage test you pasted above and set voltage to zero. Now manually rotate motor. You should see second column of `motor.monitor()` data show angle in radians. Does this look ok? Does it change by 6.2 (radians) for one rotation?

You are almost there!

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 6, 2020, 9:24am UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/17 "2020-10-06T09:24:41Z")

</div>

Hey @lulezi

The monitoring log seems fine. For me this kinds of problems are either based on sensor being missaligned ( but this seems to work fine from the video and the monitor output) or on the driver problem (but if open loop works fine then the driver works as well).

Now there are few things that are left:

- I2C communication - but this is very unlikely because if you would have lost the communication you would not reestablish it until the MCU restart, and in your case it seems to be present the whole time
- The second thing could maybe be a problem of commutation, could you please remove the line:

```cpp
  // choose FOC modulation (optional)
  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

```

- The last thing that is left for me is the pole pairs number. If it is wrong that would explain why open loop works and sensor works but the closed loop doesnt. But the problem is that from the pictures it seems that it is indeen 11 pole pairs motor 😃  
So could you please try to run your motor in open loop and see if you set an angle of 6.28 (2PI) radians if the motor will do a full circle. If it does that means the pole pair number is good as well.

---

<div class="post-metadata">

### Author: ![lulezi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/lulezi/32/287_2.png) [@lulezi](https://community.simplefoc.com/u/lulezi)
#### Post date: [October 7, 2020, 4:42pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/18 "2020-10-07T16:42:01Z")

</div>

Thanks for the suggestions @Antun_Skuric and @Owen_Williams — keep’em coming! 🙂

I tried smaller `target_voltage` between 0.1 and 1, no movement.

I started from the beginning, the `velocity_openloop` example worked like a charm — so did the `magnetic_sensor` example.

> Go back to the voltage test you pasted above and set voltage to zero. Now manually rotate motor.

One rotation is pretty much exactly 6.2. Turning it back is 0.

I commented out the `motor.foc_modulation` line — no change.

As mentioned above, one rotation by hand results in a radian of around 6.2. I also tried it the `open_loop_position` example with several values like 6.2, 62, 0, -6.2 leading to the expected rotations.

One more thing I noticed in the `voltage_control` example, after repeating the following procedure a couple of times:

- set the voltage to 0
- turn the motor by hand to a random position
- set the voltage to 1
- motor rotates a couple of degrees

It seems that the motor always stops at the same couple of positions (maybe 3, could be more). It looks like there are «steps» where it just wouldn’t rotate any further. Have you experienced something like this?

I also setup the ESP32 but to no success. Almost exactly the same behaviour — with the exception of a very high pitched sound emitted by the motor when controlled by the ESP32.

---

<div class="post-metadata">

### Author: ![Owen\_Williams](https://community.simplefoc.com/user_avatar/community.simplefoc.com/owen_williams/32/105_2.png) [@Owen\_Williams](https://community.simplefoc.com/u/Owen_Williams)
#### Post date: [October 7, 2020, 5:01pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/19 "2020-10-07T17:01:07Z")

</div>

Can you swap two of your motor cables around? (if it’s a 3 way connector you can flip it 180 degrees) and retry voltage mode.

---

<div class="post-metadata">

### Author: ![Owen\_Williams](https://community.simplefoc.com/user_avatar/community.simplefoc.com/owen_williams/32/105_2.png) [@Owen\_Williams](https://community.simplefoc.com/u/Owen_Williams)
#### Post date: [October 7, 2020, 5:13pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/20 "2020-10-07T17:13:00Z")

</div>

It sounds like the `alignSensor()` method (inside initFOC) is not doing its job of detecting `zero_electric_offset` and/or `sensor_direction` properly.

You could provide these yourself but the values are hard to guess.

```auto
float offset = float(PI / 100.0) * guess; //where guess = somewhere between 0 to 200.
Direction direction = Direction::CW; // or Direction::CCW!
motor.initFOC(offset, direction);

```

if you provide optional `offset`,`direction` then it won’t do the clockwise/anticlockwise calibration on startup to determine `zero_electric_offset` and `sensor_direction` but will use values you provide. If you guess the offset value within about 20 degrees and get the direction correct it should work.

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [October 7, 2020, 6:12pm UTC](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144/21 "2020-10-07T18:12:06Z")

</div>

This is very strange behavior. I am really looking forward to know what might be behind this.

As Owen said, it might be that `alignSensor()` is not doing it’s job. Basically `motor.initFOC()` is not aligning well. Therefore you can try his suggestion and try setting the target voltage to 2 volts and then try imputing `guess` through the serial terminal.  
After a bit of trial and error you should have your motor moving, if that is the problem indeed.

But we are running out of candidates 😃

[Next page](https://community.simplefoc.com/t/motor-doesnt-turn-in-foc-mode/144.md?page=2)
