Jump to content

Recommended Posts


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"):





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:


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()
   pinMode(but, INPUT_PULLUP);

void loop()
   while (digitalRead(but));
   myservo.write(0); delay(500);
   while (digitalRead(but));  
   while (digitalRead(but));  
   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


Link to post
Share on other sites
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...