# Motor keeps spinning in whatever direction you spin it in

**URL:** https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675
**Category:** hardware support
**Tags:** teensy
**Created:** [July 31, 2023, 7:59pm UTC](https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675 "2023-07-31T19:59:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![literally-anything](https://community.simplefoc.com/user_avatar/community.simplefoc.com/literally-anything/32/5234_2.png) [@literally-anything](https://community.simplefoc.com/u/literally-anything)
#### Post date: [July 31, 2023, 7:59pm UTC](https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675/1 "2023-07-31T19:59:52Z")

</div>

Hello,  
I’ve been trying to get this library working on a teensy 4.1 with a SimpleFOC Mini and an AS5147U magnetic sensor, but i’ve been experiencing a few issues:

At first the motor was making a high pitch squealing sound; the only thing that fixed this was manually setting the PWM frequency of each pin with `analogWriteFrequency` to 25000 like in [src/drivers/hardware\_specific/teensy/teensy\_mcu.h](https://github.com/simplefoc/Arduino-FOC/blob/05954cb9691534dd478407505d810a68f1673a5e/src/drivers/hardware_specific/teensy/teensy_mcu.h).

When I tried to use torque control, the motor would do nothing at all no matter what value I passed to `move()`. If I manually moved the motor, it would keep moving in that direction (the torque can be at 0). While the motor was moving, I could change its direction by setting the torque, but if I stopped it, I had to manually restart it. [video](https://imgur.com/GcpXoJj)

This is my code:

```cpp
#include <SimpleFOC.h>

// init BLDC motor
BLDCMotor motor = BLDCMotor(7, 5.6, 160);
// init driver
BLDCDriver3PWM driver = BLDCDriver3PWM(3, 4, 5, 2);
// init encoder
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);

// target variable
float target = 0;

// commander interface
Commander command = Commander(Serial);
void onTarget(char* cmd){ command.scalar(& target, cmd); }

void setup() {
  delay(2000);
  Serial.begin(115200);
  SimpleFOCDebug::enable(&Serial);

  analogWriteFrequency(3, 25000);
  analogWriteFrequency(4, 25000);
  analogWriteFrequency(5, 25000);
  
  sensor.clock_speed = 10000000;
  sensor.init();

  // link the motor to the sensor
  motor.linkSensor(&sensor);

  // power supply voltage
  // default 12V
  driver.voltage_power_supply = 12;

  driver.pwm_frequency = 25000;

  driver.init();
  // link the motor to the driver
  motor.linkDriver(&driver);

  // set control loop to be used
  motor.controller = MotionControlType::torque;
  
  //default voltage_power_supply
  motor.voltage_limit = 2;
  
  // initialize motor
  motor.init();

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

  // add target command T
  command.add('T', onTarget, "target");

  motor.useMonitoring(Serial);
  motor.monitor_variables = _MON_TARGET | _MON_VEL | _MON_ANGLE;

  Serial.println("Motor ready.");
  _delay(5000);
}

void loop() {
  motor.monitor();

  // iterative FOC function
  motor.loopFOC();

  // function calculating the outer position loop and setting the target position 
  motor.move(target);

  // user communication
  command.run();
}

```

I also tried `velocity_openloop`, which worked well, and `velocity`, which had similar issues as `torque`.

---

<div class="post-metadata">

### Author: ![dekutree64](https://community.simplefoc.com/user_avatar/community.simplefoc.com/dekutree64/32/3398_2.png) [@dekutree64](https://community.simplefoc.com/u/dekutree64)
#### Post date: [July 31, 2023, 8:34pm UTC](https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675/2 "2023-07-31T20:34:07Z")

</div>

Most likely the kv passed to the BLDCMotor constructor needs tuning. Add a command to change motor.KV\_rating and fiddle it up and down until it acts right. This is using voltage-based current limiting to apply a constant torque regardless of speed, so an un-loaded motor should accelerate to full speed even if the target is fairly small. But target 0 should still stop moving.

---

<div class="post-metadata">

### Author: ![David\_Gonzalez](https://community.simplefoc.com/user_avatar/community.simplefoc.com/david_gonzalez/32/665_2.png) [@David\_Gonzalez](https://community.simplefoc.com/u/David_Gonzalez)
#### Post date: [July 31, 2023, 11:11pm UTC](https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675/3 "2023-07-31T23:11:09Z")

</div>

Are you sure the sensor is working as expected? you could print the sensor value without any of the motor and driver code to verify its functionality.

---

<div class="post-metadata">

### Author: ![literally-anything](https://community.simplefoc.com/user_avatar/community.simplefoc.com/literally-anything/32/5234_2.png) [@literally-anything](https://community.simplefoc.com/u/literally-anything)
#### Post date: [August 1, 2023, 2:51am UTC](https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675/4 "2023-08-01T02:51:50Z")

</div>

This solved it! I had previously just trusted the manufacturer’s 160 KV rating, but when I calculated it, it was more like 335. Thanks!

---

<div class="post-metadata">

### Author: ![literally-anything](https://community.simplefoc.com/user_avatar/community.simplefoc.com/literally-anything/32/5234_2.png) [@literally-anything](https://community.simplefoc.com/u/literally-anything)
#### Post date: [August 1, 2023, 2:52am UTC](https://community.simplefoc.com/t/motor-keeps-spinning-in-whatever-direction-you-spin-it-in/3675/5 "2023-08-01T02:52:35Z")

</div>

Ive printed it out using the monitor, but it looks correct. The position goes up by a bit over 6 radians every rotation.
