Jump to content
Sign in to follow this  
DanielPHuber

Servo programming

Recommended Posts

Hi,

I tried my hand for the first time on a servo and my experience may be helpful to other people.

 

Servos are controlled by the length of a pulse. This pulse is repeated ca. every 20mS. The pulse length can vary coarsly between 1 and 2 mS. The pulse length determines the rotation angle. If the servo starts up, it goes to a middle position, corresponding to a ca. 1.5 mS pulse. For shorter pulses the servo turns in one direction, for longer pulses to the other. The range is limited, mostly to +/- 90 degree, the accurate values depend on the servo, as does the min/max widths. Mine had a range of a bit more than 180 degree for a min pulse width of 680 uS and a max width of 2.62 mS. However, you should not use these values but allow for some safety margin. Otherwise the motor may just not reach (e.g. due to slip or play) the position and the motor tries hard and gets hot.

 

Most servos have 3 wires that are color coded from bright to dark, the color may change. The brightest is the signal pin where the pulses are fed. The middle one, mostly red, goes to Plus and the darkest one (brown, black..) goes to ground.

 

Energia comes with a helpful server library. There an object: "Servo" is declared. You initiated the server by the method "attach". It comes in 2 varieties  (assuming we declared an object "myservo"):

 

myservo.attach(pinNo);

myservo.attach(pinNo,minVal,maxVal);

 

where pinNo means the launchpad pin number of the pin used to control the servo. min/maxVal are the minimal/maximal pulse width you want to use. This is not clearly described in the library and confused me bit. In further commands you can confortably deal with degrees beween 0 and 180. The library will transform the degree values into pulse widths, where 0 degree corresponds to the minimal pulse width and 180 degree to the max you specified. If you do not specify min/max values, some default values are used. In may case the lower default value could not be reached and the motor got hot. Therefore, it is better to determine correct values and specify these, using the second form of "attach".

 

After initialization the servo goes to the 90 degree position. To send it to the 0 degree positio you would write:

myservo.write(0);

My servo did not respond quit linear. To send it to the 90 degree position I had to say 83 degree. 

 

Here is a small test program:

 



// test for 9g Servo 
// 1. Button press -> 0 degree
// 2. Button press -> 90 degree. Due due nonlinearity we need 83 degree
// 3. Button press -> 180 degree.
// 4. Button press -> 90  degree.


#define sig P1_4  // signal for servo
#define but P1_3  // button
#define minWidth 770  // min pulse width in uSec of servo (680 allowed)
#define maxWidth 2595 // max pulse width in uSec of servo (2620 allowed)
#include <Servo.h>


Servo myservo;


void setup()
{
   myservo.attach(sig,minWidth,maxWidth);
   pinMode(but, INPUT_PULLUP);
}


void loop()
{  
   while (digitalRead(but));
   myservo.write(0); delay(500);
   while (digitalRead(but));  
   myservo.write(83);delay(500);
   while (digitalRead(but));  
   myservo.write(180);delay(500);
   while (digitalRead(but));
   myservo.write(83); delay(500);
}


 

Note that due to nonlinearity of the servo I need to specify 83 degree for a 90 degree rotation.

 

For test purposes you may power the servo directly from the launchpad (provided you do not have a monster servo with a hugh current consumption). In the enclosed picture I control the servo from launchpad pin P1_4 and power it directly from the launchpad.

 

Have fun, Daniel

post-7543-0-30105400-1457427574_thumb.jpg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×