Controlling A Brushless Motor

Once you can get the Mach3 to control the brightness of the LED you can change the map function as shown in post #35. This will produce values that range from 1000 to 2000 (or whatever you have defined for ESC_MOTOR_OFF and ESC_MOTOR_FULL) suitable for the Servo library that will drive the ESC.

You my need to play with thede fines so that the values are correct for your ESC. Electronic Speed Controllers usually need to be 'armed' – see the instructions for your ESC to see what pulse width it wants to get it started.

It may be easier to get the ESC working by testing with the knob.pde example sketch so you can adjust the pulse width using a variable resistor.

ok so would the code look something like this?:

/* * BrushlessMach * * Mach controls a brushless ESC * */ void setup() {

define inPin 2

define PWM_PERIOD 100000 // the number of microseconds for one PWM cycle

define PWM_IDLE_STATE LOW // set this to HIGH or LOW depending on how the

mach3 outputs PWM

define ESC_MOTOR_OFF 1000;

define ESC_MOTOR_FULL 2000;

}

void loop() { long pwmIn = pulseIn( inPin, PWM_IDLE_STATE ); long escOut = map(pwmIn, 0, PWM_PERIOD, 0, 255); analogWrite(ledPin, escOut); }

I know i need the "void setup" and "void loop" function thing, just not sure what goes in each. :-/

and just for my own reference we are taking a duty cycle of 0-100% and converting it to 10-20%

Almost

The #defines go above the setup and loop functions. Also, no semicolons should be used after a #define

Here is the revised sketch:

/*
* BrushlessMach
*
* Mach controls a brushless ESC
*
*/


#define inPin  2     // pwm input on this pin
#define ledPin 13    // LED connected to digital pin 13

#define PWM_PERIOD  100000 // the number of microseconds for one PWM cycle
#define PWM_IDLE_STATE  LOW   // set this to HIGH or LOW depending on how the mach3 outputs PWM

#define ESC_MOTOR_OFF 1000
#define ESC_MOTOR_FULL 2000

void setup()
{

}

void loop()
{
  long  pwmIn = pulseIn( inPin,  PWM_IDLE_STATE );
  long escOut = map(pwmIn, 0, PWM_PERIOD, 0, 255);
  analogWrite(ledPin, escOut);  
}

Note that it does not handle the extreme cases where there is no transition (0% and 100%) so test this with values between this range.

for my own reference we are taking a duty cycle of 0-100% and converting it to 10-20%

Okay if it helps you to look at it that way but its not strictly correct.
The ESC responds to the width of the high pulse and (within limits) does not care about the actual duty cycle. The method used in this simple sketch also ignores the input duty cycle and just monitors the HIGH (or LOW) duration which is why it does not handle 0% and 100% duty cycles.

Got my arduino!!

ok i uploaded this to it, got the led to blink randomly. if i have the pwm freq set to 10hz, it blinks kinda slowly. i increased the PWMbasefreq to 100 in mach3 and that made it blink much quicker. i realize we are not trying to make an led blink, but make it change brightness. brightness didnt change. :( but it did something! nothing really changes when i in/decrease spindle speed.

i played with the HIGH/LOW thing, works on LOW but nothing happens on HIGH.

try adding some serial output so you can see what is happening in the sketch.

Modify setup and loop as follows

void setup()
{
   Serial.begin(9600); // enable serial output
}

void loop()
{
  long  pwmIn = pulseIn( inPin,  PWM_IDLE_STATE );
  long escOut = map(pwmIn, 0, PWM_PERIOD, 0, 255);
  analogWrite(ledPin, escOut);  
  // add the following for serial output 
  Serial.println(pwmIn);
  delay(1000);
}

Set the PWM frequency in mach3 to 10 hz and view the serial output. You should see the values change corresponding to the spindle speed.

seems kinda random :-/

where there is a 0, the led is off, and blinks when there is a value.

values appear whether i in/decrease spindle speed

double check that the PWM signal is connected to pin 2.

is the PWM output designed for 5 volt devices (e.g. TTL level)

pin 2: check

5v breakout board from parallel port (powered from atx psu)

ok, if i take a meter and check the volts out of the breakout board i get .01-.1v depending on the rpm set in mach. im guessing thats where the problem is. gonna recheck my wiring, BOB jumpers and see what happens.

ok got 0-5v

bad connection :-[

the numbers increase when i go from max speed to min speed, led lights at lower speeds

edit, changed it from low to high, now in the right direction, led doesnt dim but tunrs off at ~ 1/2 speed

Do the numbers on the serial output appear to follow changes in PWM settings?

perhaps add another line to show the mapped values. The latter number should vary between 0 and around 255 as the PWM setting is changed

void loop()
{
  long  pwmIn = pulseIn( inPin,  PWM_IDLE_STATE );
  long escOut = map(pwmIn, 0, PWM_PERIOD, 0, 255);
  analogWrite(ledPin, escOut);
  // add the following for serial output
  Serial.print(pwmIn);
  Serial.print(", ");
  Serial.print(escOut, DEC);
  delay(1000);
}

i started at full speed and decreased to 1% speed

led turns off past the 151.... value

i know some rc motors have a reverse feature as well, so you could back a car up. how can you control that using the servo lib?

i know some rc motors have a reverse feature as well, so you could back a car up. how can you control that using the servo lib?

If the motor/controller is designed to be bi-directional then a servo pulse width of 1.5msec would be stop, 1msec full reverse and 2.0msec would be full forward speed.

Lefty

Hi, everyone. This time I'was also trying to control a brushless motor with Arduino, but I stuck in the very beginning. I started with powering the controller (emax ESC 25A). But when I just connected the red wire to the red on a battery and a black with a black on a battery, I got much sparks.. So I think the ESC behaves itself as a zero resistance and I get the short-circuit... Can you explane me, is it normal and what am I doing wrong in this case? Thanks in advance.

It is normal for sparks to fly when plugging in the battery to the ESC. The high current draw only lasts a very short time, then there is almost zero load until you give the ESC throttle commands. Someone once mentioned that the high current draw on powerup was charging the capacitors. I do not know for sure, but it has enough buzz words in it to make me happy. :)

Oh, that's great, thanks ;). Now everything works fine. I was a bit frightened, cause never had any sparks before.