Tuesday, April 13, 2010

Control a motor from the arduino board with a speed controller.

I connected the speed controller (ESC) to the arduino board and I connected the black cable of the receiver connection to the GND pin of the arduino and the white cable to the Digital PWM pin number 3.

I upload a sketch that I found on this post from the Arduino Forum to the board to test the motor, but nothing happening.
After some research I discovered that is necessary adjust the minim pulse width value. From the operating instructions that came with the speed controller I find that the stop position must be within the range 800 us to 1700 us and the full-throttle position must be at least 350 us than the stop position.
I make another sketch to find the the minimum pulse width value that start the motor and closer to the 0 angle. After several attempts I found that the nearest value is 836 us.
Finally, I made the sketch that is shown below, which starts the  ESC and keeps the motor running in a loop for 30 seconds and then stop for 10 seconds.

#include <Servo.h>

Servo escservo;

long lasttime = millis();

void setup() {
  delay(1000);
  Serial.begin(9600);

  // If you connected now the battery to the ESC, send
  // the 's' character. If not send another character.
  Serial.println("Wait for your input! [s] to init...");
  while(Serial.available() <= 0) delay(1000);
  Serial.println("Start the system...");
  int incomingByte = Serial.read();
  
  // Change the min pulse width value until match the
  // lowest value needed to start the motor.
  // From the ESC operation instructions the stop position
  // must be within the range 800 us to 1700 us.
  // The value 836 us is the lowest value.
  escservo.attach(3, 836, 2400);
  
  // arm the ESC
  escservo.write(0);
  delay(5000);
  
  // If you sent the character 's'
  if(incomingByte == 115) {
    Serial.println("Traverses all angles upward and downward.");
    cycle();
    delay(1000);
  }
}

void cycle() {
  int angle;
  // Danger: Try first with a value lower than 179, eg 160
  // Gradually increase to 180.
  for(angle = 0; angle <= 160; angle++) {
    escservo.write(angle);
    delay(15);
  }
  // Decrease the angle from 180 to 0.
  for(angle = 160; angle >= 0; angle--) {
    escservo.write(angle);
    delay(15);
  }
}

void loop() {
  long duration = millis() - lasttime;

  // If the duration is great than 30 seconds,
  // stop the engine for 10 seconds.
  if(duration >= 30000) {
    escservo.write(0);
    delay(10000);
    lasttime = millis();
  }
  // Put the motor to rotate in the angle 5
  escservo.write(5);
  delay(500);
}

4 comments:

  1. Hello friend, sorry my terrible English. I'm testing some time ago and a quadcopter with all the codes I use, including his only one engine is in a loop. When it begins to sketch all the engines start normally, but the signs of entering "+" just the engine 4 receives and has its speed changed. Could tell me why the friend and what I do to solve this big problem?

    I appreciate your help!

    ReplyDelete
  2. Sorry, but only now, after a long time I noticed this comment. But the code above only works for one engine. For four engines you should use the code from this post http://blog.mobileapes.com/2010/04/control-all-four-motors-from-arduino.html. Note that the code written for the operation of the engine has to take into account the specifications of the ESC's. You can also find more information on this site http://www.instructables.com/id/Quadrotor/step23/Code/.

    ReplyDelete
  3. How can I modify the code to keep it always on?
    or control it's speed using a potentiometer

    ReplyDelete
  4. helo

    i have brushless motor esc and arduino i would like to control motor with potentiometer but i am totaly newbie in programing can someone help me and post code for that kind of application?

    ReplyDelete