Using an Arduino to Control a Servo / Throttle

econfly

Member
A radio and receiver are not the best tools for bench testing. You can buy stand-alone servo controllers. You can even buy a function generator (overkill for all but very demanding purposes; I use this one). But there is no need to spend the money when many already have what they need: an Arduino board.

If you don't have one, get one. For under $25 (or a little more if buy a kit) there is plenty of fun and practical utility to be had.

Here is an example setup that can be used to control an ESC (or anything else that accepts PWM servo input).

DP2Q4121_zps9ed0f8db.jpg


The setup is very simple. All you need to add is a connection from the signal and ground output to your ESC/servo control cable.

The code below explains the details. Just put in your desired throttle/servo setting (throttle_set, I have it at 65% in the code). At power-up/reset the code sets the servo to low/off. When you press the button the output is ramped from the low setting to the desired level. When the desired level is reached the LED is turned on. To return to the low/off level just press the button again.

That's it. Feel free to take my example code and use it any way you like. You can program sequences, change the ramping, etc. With trivial programming changes to the code below you can even use the Arduino as an ESC throttle setting tool.

One bit of warning: While you can power this setup from your computer's USB port, don't try to connect power from the Arduino to your ESC's power wire on the servo cable. That is, connect the ground (usually black or brown) and signal (yellow) wires, but leave the power (orange/red) disconnected. If your ESC demands power over the servo connection you should get it from another source. Or, at least check to confirm that the ESC/servo's power demand is within the Arduino and USB specs. For most ESCs you will be fine just leaving the power wire disconnected.


Code:
#include <Servo.h>  // get servo library

// The library outputs a PWM signal at 5 volts with a 15ms period (66.7Hz)
// Most flight controllers output higher frequency, but for testing purposes
// (particularly maintaining fixed throttle) lower frequency PWM is fine

const int throttle_set = 1650 ; // desired throttle (pulse-width in microseconds)
const int throttle_low = 1000 ; // output is in microseconds; 1000 is 1ms -- typical low PWM output

// Typical PWM values range from 1000 (low) to 2000 (high), i.e. 1 to 2 milliseconds.
// Mid-throttle is 1500. 
// Most ESCs arm at 1100 to 1150 and max power output at around 1900-1950.  
// Futaba receivers output a default range of 1100 to 1940.
// DJI NAZA output at off is 940; throttle up depends on IDLE setting and ranges from 1144 (low) to 1211 (high)

Servo throttle ; // structure for servo
const int buttonPin = 2 ; // code below uses a button press to engage/cut throttle
const int ledPin =  13 ; // the LED on the arduino board; lighted when at desired throttle

int level ; // value used to keep track of current throttle level

void setup()
{
 throttle.attach(3) ;  // PWM output is going to PIN 3
 pinMode(ledPin, OUTPUT) ; // LED onboard the arduino will light when desired throttle is reached
 pinMode(buttonPin, INPUT); // A button is used to engage / cut throttle
 level = throttle_low ; // initialize throttle to low
 throttle.writeMicroseconds(level) ; // set throttle
}

// repeated 
void loop()
{
 // is the botton pressed? If so, ramp up throttle to set level or cut to low if already set
 if (digitalRead(buttonPin) == HIGH)
 {
   // if throttle is not low, cut throttle to low, kill LED, and wait to avoid button bounce
   if (level != throttle_low)
   {
     level = throttle_low ;
     throttle.writeMicroseconds(level) ;
     digitalWrite(ledPin, LOW);
     delay(500) ;
   }
   else
   {
     // ramp throttle to indicated value and light LED; adjust increment and delay as desired
     for (level = throttle_low; level < throttle_set; level += 1)
     {
       throttle.writeMicroseconds(level) ;
       delay(5) ;
     }
     level = throttle_set ;
     throttle.writeMicroseconds(level) ;
     digitalWrite(ledPin, HIGH);
   }
 }
}
 
Last edited by a moderator:

Top