How to control my BLDC motors with integrated driver

Hello all,

I am new to the forum and also new to arduino projects.
I have bought 2 BLDC motors with integrated driver ( motor info )
that have the following wiring:


I tested the motors an when red and black wires are connected the motor runs.

Now I am havving dificulty to control this and I am not finding many examples with integrated drivers.
I have 2 questions:

  1. How do I control the CW/CCW wire.
    • when I connect the white wire from the engine to GND the motor direction changes. When the white wire is NOT connected it does not change when 5v is applied it does not change.
      I want to control the direction from my program in the Mega2560.
      I tried:
  #define DIR_PIN 11

void setup() {
   pinMode(DIR_PIN, OUTPUT); digitalWrite(DIR_PIN, HIGH);   

// the loop function runs over and over again forever
void loop() {
  delay(1000);
  digitalWrite(DIR_PIN, LOW);
  delay(1000);
  digitalWrite(DIR_PIN, HIGH);
}

But this seesm to have no effect. What am I doing wrong ?

  1. How do I control the speed.
    The speed should be controlled with PWM over the blue wire and the frequency should be between 15kHz and 25kHz so I tried this:
#include <PWM.h>

  int PWM_out_pin = 11;
  int timer = 1;
  int increase = 1;
  byte motorspeed = 0;
  int32_t frequency = 20000; //frequency (in Hz)
  bool success = false;

// the setup function runs once when you press reset or power the board
void setup() {
    //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 

  //sets the frequency for the specified pin
  success = SetPinFrequencySafe(PWM_out_pin, frequency);

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PWM_out_pin, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  if(success) {

    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000/timer);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000/timer);       // wait for a second
  
    motorspeed = (timer - 1) * 50;
    pwmWrite(PWM_out_pin, motorspeed);
    
    timer = timer + increase;
    
    if ( timer == 6 ) {
    increase = -1;
    }
    if ( timer == 1 ) {
    increase = 1;
    }
  }
}

When I start the motor by giving it power it runs. As soon as the program runs the motor stops as expected because the first speed would be 0. When I try this without the frequency change there is no effect. But after it stops nothing changes anymore.

This is my first time with PWM please help.

I wonder where the 5 volts comes from? Is the -5 volts connected to the same ground as the power to the motor?
Paul

For power I have a 24v power source which is connected to the motor. I also have a stepdown to 5v connected to this power source. So all power is coming from the same source.
I tried the CW/CCW unconnected and I tried connecting it to 5v. Both made no change on the motor. Connecting the CW/CCW wire to GND made the motor change direction

Are ALL the power, motor and Arduino grounds connected together? Are you using a breadboard anywhere in the layout?
Paul

Hi @reiniergielen,

It sounds like when the direction pin is unattached, the motor turns one direction, and when attached to ground, it turns the other. If you just use a relay connected from that pin to ground with the input attached to your Arduino, you should be able to control its direction just fine.

Correct, and that would certainly be a solution. I never thought about that as I assume that I would be able to control a 0-5v wire directly from the arduino. But the best suggestion so far.

In the test of CW/CCW described above there is no arduino involved, that is manual. Now you mention it, the test controlling the direction with the arduino the arduino is power with a USB. So the answer would be no the arduino does not ( at the moment ) have the same power source.
The stepdown is intended to power the arduino so I should probably fix that and do another test.

Well, IF you have all the grounds connected to a common point, then there is no reason the +5 to the white wire will make the motor turn one way and connecting that same wire to the common ground would not make it turn the opposite way.
Any digital pin on your Arduino would work the same way. A high would turn one way, a low, the opposite. No relay needed.
Paul

Hi,

Yes, @Paul_KD7HB is right. You shouldn't need a relay. The CW/CCW wire needs a minimum of 2V to change directions, so technically this would work with a 3.3v device also.

Credits to @Paul_KD7HB I connected the arduino to be powered by the stepdown ( as intended in the final design ) and tested again. The test program is now reversing the motor direction as it should do in this test.
Lesson learned, don't rush the power and ground connection to get a test done quickly. Thanks.

Do you also have a suggestion for my sceond question ?

Glad you got it going. No advice on #2, never used it.
Paul

Hi,

You could use analogWrite() instead of the PWM library. It's much simpler.
Example(Arduino PWM Tutorial - Arduino Project Hub):

int led_pin = 6;
void setup() {
  //Declaring LED pin as output
  pinMode(led_pin, OUTPUT);
}
void loop() {
  //Fading the LED
  for(int i=0; i<255; i++){
    analogWrite(led_pin, i);
    delay(5);
  }
  for(int i=255; i>0; i--){
    analogWrite(led_pin, i);
    delay(5);
  }
}

And how about the frequency ?

The deafult is: Mega 2 - 13, 44 - 46 490 Hz (pins 4 and 13: 980 Hz)
And the motor spec wants between 15kHz and 25kHz

Or did I read this wrong: image
Is it between 15Hz and 25kHz then the default would be fine.

Hi,

Yes, it needs 15khz - 25khz. I just checked and analogWrite() frequency cannot be changed. Im sorry, but I can't answer your second question. This might help:

Again credits to @Paul_KD7HB fixing the grounding issue also improved the PWM issue. The motor now stops and starts again. It feels like it is running very raw, but al least it is no longer dead.
Some tweaking of frequency and speed in the test program might fix that.

Yes it can, but you need direct register manipulation or a suitable library to do this.

I checked Arduino official analogWrite reference page and it said that analogWrite’s frequency cannot be changed. It did say that you could a library instead though for changing the frequency.