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:
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 ?
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.
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
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
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 ?
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.
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.