Control ESC-BLDC Motor Via Serial Monitor

Hi, I need a program for controlling ESC-BLDC Motor via the Serial Monitor of the Arduino IDE. So far, what I have done is controlling the motor to move forward (FROM MIN TO MAX SPEED) or to move backward, I mean CCW (FROM MIN TO MAX SPEED).

The problem is: I can barely understand this annoying ESC. I have seen several codes saying it controls the speed of the ESC-bldc motor via the serial monitor, and when I upload it to my arduino, it doesn't f move. lol. When I upload the codes below, it works. What I have noticed is that the codes that are not working either uses analogWrite
or object.write(value_here). While the code that is working (see below), it uses object.writeMicroseconds

I'm pretty confused here.
Anyway, my main objective why I'm asking for a sample program is to: make my ESC-bldc motor be controlled via serial monitor, in a way you press W and S (W for CW, and S for CCW or reverse). Just like the normal RC-cars. When you press W continuously, the motor from min. speed will be up to max speed. Please help. If you can provide a sample code any sample code that is similar to my objective, it would be a big help for me. Thanks a lot. I hope someone will reply.

G

#include <Servo.h>

Servo bldc_motor; //creates a "servo" object (the ESC and motor)


void setup()
{
bldc_motor.attach (9); //attach the motor to pin 9
Serial.begin(9600);
}

void loop()
{
bldc_motor.writeMicroseconds(1500); //provides a "neutral" pulse. The ESC won't start without this.
Serial.println("BLDC motor test..");
Serial.println("Starting in...");
Serial.println("");

for (int i = 4; i >= 0; i --) //countdown timer
{
Serial.print(i+1);
Serial.println("");
delay (1000);
}


while(1)
{
Serial.println("FORWARD.");

for (int i = 1500; i <= 2000; i++)
{
int speed_value = map (i, 1500, 2000, 0, 100); //maps the signal pulse to a percentage
Serial.println(speed_value);
Serial.print('%');
bldc_motor.writeMicroseconds (i); //actually write the value to the motor
delay(10);// small delay so you can see what's happening
}

bldc_motor.writeMicroseconds (1000); //full reverse. The ESC will automatically brake the motor.
delay (500); //small delay to let things happen
bldc_motor.writeMicroseconds (1500); //give a neutral signal
Serial.print("MOTOR stopped");
delay (3000); //keep it that way for a while


Serial.print("BACKWARD");

for (int i = 1500; i >= 1000; i --)
{
int speed_value = map (i, 1500, 1000, 0, 100); //maps the signal pulse to a percentage
Serial.print (speed_value); //prints the percentage to the LCD
Serial.print ('%'); //notice the single quotation marks. I've just sent a char value, not a string because it's only 1 character
Serial.println("");

bldc_motor.writeMicroseconds (i); //actually write the value to the motor
delay(10);// small delay so you can see what's happening
}

bldc_motor.writeMicroseconds (1500);
Serial.print ("motor stopped");
delay (3000);
}



}

Anyway, my main objective why I'm asking for a sample program is to: make my ESC-bldc motor be controlled via serial monitor, in a way you press W and S (W for CW, and S for CCW or reverse).

What you want to do will not really be supported by the serial monitor (the serial monitor requires each individual command to be entered and sent). You will probably need to write a program for your computer that has the keyboard interface actions you want. Below is some serial monitor test code that may be somewhat similar to what you desire for keyboard control.

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a 
// to decrease) and enter to change servo position 
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position 
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

void loop()
{
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    if(readString.indexOf('x') >0) { 
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos); 
    }
  }
  readString=""; //empty for next input
}

Hello sir Zoomkat, I really appreciate your reply. Might test your code some other day or when I wake up, I need some rest for now, it's 4AM here. Anyway, I got my codes working before I saw your reply:

It can be controlled using putty and pressing W or S for CW/CCW. However, what I want to do is that whenever the user stops pressing either W or S, the speed should decrement slowly until to its neutral position. Also, when I press W or S, I have to press such key for a while (like 2 seconds) before the motor starts spinning. I think it is because of the incremental time, I mean putty outputs 12% or n% repeatedly first and then the motor will start.

Thanks.

/* This program is modified by:
 Glenn Von C. Posadas
 admin@glennvon.com
 glenn.von.c.posadas@accenture.com
 
 1/21/2015
 2:16 AM
 
 To do:
 - When the user stops pressing the 'W' or 'S' button,
 the counter should begin to decrease.
 */

#include <Servo.h> //Notice how you can use the standard servo library!

Servo bldc_motor; //creates a "servo" object (the ESC and motor)
int command;
int forward = 1500; //value for speed in forward
int backward = 1500;

void setup()
{
  bldc_motor.attach (9); //attach the motor to pin 9
  Serial.begin(9600);
  bldc_motor.writeMicroseconds(1500); //provides a "neutral" pulse. The ESC won't start without this.
  Serial.println("BLDC motor test..");
  Serial.println("Starting in...");
  Serial.println("");

  for (int i = 4; i >= 0; i --) //countdown timer
  {
    Serial.print(i+1);
    Serial.println("");
    delay (1000);
  }
}

void loop()
{
  if(Serial.available()>0)
  {

    command=Serial.read();

    if(command==119)
    {
      Serial.println("FORWARD.");

      forward++;    
      backward = 1500; // reset the backward variable  
      int speed_value = map (forward, 1500, 2000, 0, 100); //maps the signal pulse to a percentage
      Serial.println(speed_value);
      Serial.print('%');
      bldc_motor.writeMicroseconds (forward); //actually write the value to the motor
      delay(10);// small delay so you can see what's happening

    }

    if(command==115)
    {
      Serial.println("BACKWARD.");
      //    for (int i = 1500; i >= 1000; i --)
      backward--;
      forward = 1500; // reset the forward if the user press the backward lol
      int speed_value = map (backward, 1500, 1000, 0, 100); //maps the signal pulse to a percentage
      Serial.print (speed_value); //prints the percentage to the LCD
      Serial.print ('%'); //notice the single quotation marks. I've just sent a char value, not a string because it's only 1 character
      Serial.println("");

      bldc_motor.writeMicroseconds (backward); //actually write the value to the motor
      delay(10);// small delay so you can see what's happening
    }

    if(command==98)
    {
      Serial.println("BREAK!");

      bldc_motor.writeMicroseconds (1000); //full reverse. The ESC will automatically brake the motor.
      delay (500); //small delay to let things happen
      bldc_motor.writeMicroseconds (1500); //give a neutral signal
      Serial.print("MOTOR stopped");

    }


  }      


}

could you give an idea about how to choose a right ESC for a specified BLDC configuration here i am dealing with high torque BLDC of 200KV or Higher in order to drive a person on an electric skateboard

You will need to gear down any motor for that application, and you need to decide
the max power too. You'll have to derate a RC BLDC if its not under strong forced
air cooling (they are designed to sit behind a propeller rememeber!).

Choosing the ESC follows after choosing the motor. But you need to consider that
RC ESCs are normally sensorless and cannot generate full torque at stationary becuase
they rely on the motor spinning to give back-EMF feedback for commutation.