Sunday, June 10, 2012

Arduino - Control ESC/Motor Tutorial



Summary
This is a tutorial of how to control an electronic speed control (ESC)and brushless motor using an Arduino.

Description
This tutorial is about controlling a brushless motor, the type commonly used in RC hobbies, and controlling the motor using an Arduino.  The goal is to use the Arduino Servo library, give an input from 0 to 180, and see the motor move based on our input.

Before we begin
Hooking up the Arduino to an RC hobby ESC is always the same.  The difference is that you will need to know how to arm your ESC.  This means you'll need to look for the manual that tells you how to arm your ESC.

When reading your ESC manual, you'll most likely notice that the literature/manual was written for people who have other RC equipment.  My manuals have always talked about the need to adjust the throttle in order to trigger an event like arming or programming modes.  We will be substituting the throttle with the input we give the Arduino.

Safety (always)
"You'll shoot your eye out" - A Christmas Story
Experimenting with the combined equipment of an Arduino, ESC, motor, and computer can cause harm to you or your equipment under certain situations.  Here's some items to consider for some of the components involved.

Motor
Make sure there is nothing attached to your motor (i.e. no props).  Don't test with props attached.  I jumped out of my chair, swearing, the first time the motor ran because I didn't expect it to work.  In custom trial and error, you never know what's going to happen.

Secure the motor to something while experimenting.  You don't want the motor jumping around when it unexpectedly comes to life.  If you need an ideal example, please how Ray29erau has his motor secured in his youtube video.

Protect Your USB ports
While an Arduino may be relatively cheap, let me save you a USB port.  No matter what you are thinking of doing, don't connect the positive line of your ESC to your Arduino pin labeled 5v.   If you connect your battery to the ESC, connect your Arduino to your computer by USB, and connect the gnd, signal, AND 5V into the Arduino..... you will probably damage (lose, fry, kill, etc) whatever USB port your Arduino is plugged into on your computer.   There's a good possibility you could lose the Arduino board too.  For a full explaination, please see "10 ways to Destroy an Arduino".... more specifically it's "Method #5".

Battery
General battery safety is recommended.  I'm using a lithium polymer (LiPo) battery.  If you want to know more on LiPo's, check out this page.  If you have a question or doubt about something you are doing with a LiPo battery, find someone who knows about the safe handling/charging of battery/LiPo's and ask.  For example, I bought mine from an awesome local hobby shop.  If I had questions about what I bought and the answers weren't included with the manual or couldn't be found online then I would ask the people that sold the gear to me.

D.) Other
This is the other category.  This is all meant to be fun.  When experimenting, there may be times you feel uncomfortable or unsafe about doing something in a project.  At times like these, find a person, forum, or group (Arduino forums linked) to ask and make your situation safe at all times.
 
Steps
Control the ESC/Motor using an Arduino
  1. Connect the ESC to motor.
  2. Connect the ESC to Arduino - pay attention or you will kill a USB port/Arduino (Protect Your USB Ports).
  3. Connect the Arduino to you computer.
  4. Upload your sketch to the Arduino.
  5. Use the Serial Monitor to arm the ESC
  6. Done!!! Do other stuff....


Begin
1. Connect the ESC to motor.

Simply enough, connect the ESC to the motor (Figure 1.1).  Notice that my yellow wire is in the middle.  Both the black and the red wires are on the outside.  If your ESC doesn't have colored wires (as seen on another ESC Figure 1.2), don't worry.  Getting the outside wires mixed up would only make the motor spin in the wrong direction.  There would be no harm in getting the outside wires wrong.  If you find your motor is spinning really fast when you think it should be stopped...  you only need to reverse the polarity between the ESC and motor.  


Figure 1.1 - Motor connected to ESC

Figure 1.2 - Two ESC's next to each other

2. Connect the ESC to Arduino.

Connect the Arduino's pin labeled "GND" to the ESC (Figure 2.1).  For this project, I am using pin 9 for the signal, yours may be different.  Connect the Arduino's pin labeled "9" to the ESC's white signal line (Figure 2.2).  That's it for step two.
As discussed in above in the safety section under "Protect Your USB Ports", DO NOT connect the Arduino pin labeled 5V to the ESC. (Figure 2.3)


Figure 2.1 - Arduino GND to ESC

Figure 2.2 - Arduino pin #9 to ESC

Figure 2.3 - DONT DO THIS
Figure 2.3 - DONT DO THIS

3. Connect the Arduino to your computer.
Connect the Arduino to your computer (Figure 3.1). It's super-easy.

Figure 3.1

4. Upload your sketch the Arduino.
Open up your Arduino IDE. Click here to open up the reference page (it has the Arduino sketch).  Copy the sketch on the reference page and paste it into your own sketch.  Upload the sketch to your Arduino (Ctrl + U).


5. Use the Serial Monitor to arm the ESC.

In the Arduino IDE, select "Tools" then select "Serial Monitor".  At this point, plug your battery into the ESC.  Your ESC will probably be different that mine.  This is where it's all about trial and error now.

Your ESC will be different but mine always gives a series of beeps.  According to the manual, the series of beeps tells me that the the ESC is on.  At this point, the manual (mine is found here) says the following:
"The  H . K i n g   ESC will not arm unless you
move your throttle stick to the lowest position. When
the H.King ESC receivesthe low throttle command, it
will arm and play a double tone through the motor,
indicating it is armed and now ready to run." 
Great.  I have no idea what this correlates to in the values from 0 to 180.  That's okay.  I did my trial and errors by typing numbers between 0 and 180 into the Arduino IDE Serial Monitor.  I tried the lows (0, 5, 10, 15, 20), the middle (90), and the high (170, 175, 180).  What I found was that sending "10" will arm the ESC and it will start spinning when I enter (send) "55" or higher from the Serial Monitor (Figure 5.1). Figure 5.1 shows what happens when I type "10" and then "55" into the Serial Monitor.

Be careful when doing trial and error with the motor.  When figuring out the ranges, high and low could be backwards.  For example, high might be "0" and low might be "180".  The first time my motor spun was when I sent 180, coincidentally I also found out that this was the high end of the range.

Figure 5.1
6. Done!!!  Do other stuff....

If you have figured out how to make your motor spin.  Congrats! It's a fun learning experience.  Most likely, you are trying to get your motor spinning for some other project and this was one of the first steps.  Good luck!








Arduino - Control ESC/Motor (Arduino Code)


Summary
This is only the Arduino sketch for the tutorial "Arduino - Control ESC/Motor Tutorial".  This is a tutorial of how to control an electronic speed control (ESC)and brushless motor using an Arduino.


/*
*  This code is in the public domain.
*  (Do whatever you want with it.)
*/

// Need the Servo library
#include <Servo.h>

// This is our motor.
Servo myMotor;

// This is the final output
// written to the motor.
String incomingString;


// Set everything up
void setup()
{
  // Put the motor to Arduino pin #9
  myMotor.attach(9);

  // Required for I/O from Serial monitor
  Serial.begin(9600);
  // Print a startup message
  Serial.println("initializing");
}


void loop()
{
  // If there is incoming value
  if(Serial.available() > 0)
  {
    // read the value
    char ch = Serial.read();
 
    /*
    *  If ch isn't a newline
    *  (linefeed) character,
    *  we will add the character
    *  to the incomingString
    */
    if (ch != 10){
      // Print out the value received
      // so that we can see what is
      // happening
      Serial.print("I have received: ");
      Serial.print(ch, DEC);
      Serial.print('\n');
   
      // Add the character to
      // the incomingString
      incomingString += ch;
    }
    // received a newline (linefeed) character
    // this means we are done making a string
    else
    {
      // print the incoming string
      Serial.println("I am printing the entire string");
      Serial.println(incomingString);
   
      // Convert the string to an integer
      int val = incomingString.toInt();
   
      // print the integer
      Serial.println("Printing the value: ");
      Serial.println(val);
   
      /*
      *  We only want to write an integer between
      *  0 and 180 to the motor.
      */
      if (val > -1 && val < 181)
     {
       // Print confirmation that the
       // value is between 0 and 180
       Serial.println("Value is between 0 and 180");
       // Write to Servo
       myMotor.write(val);
     }
     // The value is not between 0 and 180.
     // We do not want write this value to
     // the motor.
     else
     {
       Serial.println("Value is NOT between 0 and 180");
     
       // IT'S a TRAP!
       Serial.println("Error with the input");
     }
   
      // Reset the value of the incomingString
      incomingString = "";
    }
  }
}