Home Arduino Code Learn how to position a servo using degrees instead of control timing on the #Arduino
Learn how to position a servo using degrees instead of control timing on the #Arduino

Learn how to position a servo using degrees instead of control timing on the #Arduino

1

So this is actually a pretty common request whenever I teach a robotics class, and introduce the Servo for folks to start playing with in their robotic Arduino circuits. I have created several implementations of this type of system, but on the Arduino and Versalino this is my favorite way so far.

By designing a function to automatically map degrees to raw PWM output we can get multiple calibrated servos running in the same robotics project which opens up a whole slew of interesting new things to do with your Servos.

#include
#include //this version of code is for the Versalino

Servo myServo;
byte myServoCalibration[2] = {10,215};
//you can change the above calibration code to
//match your servos range

void setup()
{
myServo.attach(BUSA.P2);
//this attaches the servo to S2 on the Sense & Move
//and assumes that you have the Sense & Move on BUSA
}

void loop()
{
//now we sweep from -90 degrees to 90 degrees, under the assumption that
//the servo calibration puts us between 0 and 180 degrees motion.
//We consider 0 to be -90 degrees, and 180 to be 90 degrees.
for(int servoangle=-90; servoangle <=90; servoangle++) { WriteDegreesToServo(myServo, myServoCalibration, servoangle); delay(15); }}//in this example we pass a reference to our calibration array along with the servo //and the position itself void WriteDegreesToServo(Servo servoInQuestion, byte* ServoCalibration, int myDegrees) { //note that we are using the Arduino map function to change our calibrated data //into a range from -90 to 90 degrees. servoInQuestion.write(map(myDegrees, -90, 90, ServoCalibration[0], ServoCalibration[1])); }

In the code above we set our servo up, and we add our calibration data for the 0 and 180 position of the servo in question (I used values that are pretty safely in the range of most servos, but you should check yours and modify as needed). The reason for 0 being the middle position is to help us with centering devices in later guides, but you can change the mapping in the WriteDegrees function to whatever you want.

We then do a for loop that takes us from -90 degrees to 90 degrees, and pass our angle into our WriteDegreesToServo function, which then uses the Arduino Map() function to change from the -90 to 90 degree input, to a scaled command that matches the calibration data you provided for your servo.

The only thing you have to change to use this on a regular Arduino Uno, Mega, Etc is to change the pin assignment on your servo as follows:

void setup()
{
myServo.attach(11);//set to your PWM servo pin
//this attaches the servo to pin 11 on your Arduino
}

There you have it! You can now direct your servos to a particular angle. Let me know if you come up with any improvements, or if there are any questions!

Joseph Dattilo Writer, Electrical Engineer, CEO and founder of Virtuabotix LLC, and completely crazy in every way.

Comment(1)

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *