I recently purchased a brushless DC motor. This motor is pretty much this one

It has a built-in controller very similar to this post:

Brushless DC motor with built in controller

There are five wires: power, ground, PWM, frequency generator, and direction. I'm trying to use a Teensy 4.1 while powering the motor externally with a 24 V power supply.

My code looks like this:

int led = 13;
const int pwmPin = 2;
const int dirPin = 3;
const int fgPin = 4;

void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  // analogWriteResolution(13);
  analogWriteFrequency(pwmPin, 20000);
  // Setup Serial
  while (!Serial);

void loop() {
  // turn motor off for two seconds
  analogWrite(pwmPin, 256);
  digitalWrite(led, HIGH);
  // Prompt user to enter key before starting
  Serial.println("Enter any letter key to start code");
  String entry = "";
  while (entry.length() < 2) {
    if (Serial.available()) {
      char inChar = Serial.read();
      entry += inChar;
  Serial.println("Running script now.");
  // turn motor on and spin 
  digitalWrite(dirPin, HIGH);
  analogWrite(pwmPin, 128);

  // turn motor on and spin in opposite direction
  Serial.println("Turning other direction");
  digitalWrite(led, LOW);
  digitalWrite(dirPin, LOW);
  analogWrite(pwmPin, 128);

I upload the code, open the serial monitor, turn on my power supply, and the motor spins. However, it doesn't appear that my analogWrite() functions are changing the speed, regardless of what duty cycle I use. The wiring is very similar to this brushless motor.

I noted that a high signal on the PWM wire causes the motor to stop, while a low signal causes it to move, so I've been setting the duty cycle in the first analogWrite() function in the loop to 256 see if I can get the motor to stop spinning when I supply power. I also tried setting the duty cycle to zero, but the motor still spins. No matter what duty cycle I try to write, the motor just spins at what I assume is 100% duty cycle.

Does anyone know why this might be the case? All of my wiring is correct, and it's a relatively simple program. I'm fairly new to coding all of this, so I'm hoping it's just a careless error on my part, and any help would be appreciated! Please let me know if you need any more information.

EDIT 1: Just as a side note, I know the controller isn't broken. I managed to control the speed just fine using a bunch of Tinkerforge bricks/bricklets (i.e. the servo brick and io4 bricklet). For my project, I need to use the Teensy, though.

EDIT 2: Added link to motor from actual supplier (Aslong)

  • \$\begingroup\$ Well the motor isn't brushless, but that may not effect anything since you're using a controller anyway. \$\endgroup\$
    – Drew
    Jan 11, 2023 at 19:49
  • \$\begingroup\$ Sending a 256 is asking for trouble. You should keep the input values in the range of 0-255. Your input of 256 is likely rolling back to 0, so on 100%. \$\endgroup\$
    – Drew
    Jan 11, 2023 at 19:55
  • \$\begingroup\$ Try setting the duty cycle to 250. \$\endgroup\$
    – Drew
    Jan 11, 2023 at 19:56
  • \$\begingroup\$ Tried duty cycle at 250, 255, and 1, but with no luck. Motor keeps spinning at the same speed. Thanks though! \$\endgroup\$
    – rian1
    Jan 11, 2023 at 21:00
  • \$\begingroup\$ "I noted that a high signal on the PWM wire causes the motor to stop" - yes, that's what it says in your ('similar') datasheet. When you set the 'analog' output to 255, what voltage/waveform do you see on it? Is it different from when you send a 'high' signal? \$\endgroup\$ Jan 11, 2023 at 21:12

1 Answer 1


Solution! I didn't connect the ground pin on the Teensy to the ground on the external power supply. I just had the Teensy plugged into the USB port. Now everything works fine.

For any others with the problem, I found that the motor doesn't spin when the PWM duty cycle is at 0%, unlike what the data sheets say.

Thanks for everyone's help!

  • \$\begingroup\$ are you saying that you disconnected the ground and it started to work? \$\endgroup\$
    – jsotola
    Jan 11, 2023 at 23:28
  • 1
    \$\begingroup\$ Sounds like he forgot to hook up the signal ground before. \$\endgroup\$
    – Drew
    Jan 12, 2023 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.