Jump To Content

LearnHub




Arduino and Servos

Goal:
To move servos with the Arduino to a position, and stay there (without the servo library).

Arduino Controlling the Servo!

Housekeeping Blurb

This tutorial is about moving servos with the Arduino. It is aimed at people that have used the Arduino to make LEDs blink, or something simple. You will NOT learn about PWM in this tutorial. However, at the end of the tutorial, I hope that you will have the ability to move servos in any fashion you wish with the Arduino! Before we begin, I must say that I adapted my code from Tod E. Kurt's code, which was adapted from Daniel's code, which was adapted from Tom Igoe and Jeff Gray's "Temporary Servo Function" code. I'll be using Draganfly DF-06 servos - but you can adapt the code to any servo that you'd wish! Now that the housekeeping is out of the way, let's start!


Draganfly DF-06 Servo

Part 1 - Light Salad
This part will be the prelude to the meat and potatoes part. It is good if you would like a solid background!

First, let's plug in our servos. I always had trouble with this, so here is a straight forward breakdown.

Black -> Ground (0V)
Red -> Power (5V)
White/Yellow -> Control (PWM) * See the next few lines directly below for more info on where to plug it in!
At the top of the Arduino there are several Digital Output pins. Let's plug the data cable (control) of Servo #1 into pin #7, and Servo #2 into pin #6. Let's define this in the code that will be loaded onto the Arduino:


int servo1 = 7;
int servo2 = 6;


While you're at it, we also have to define a few other things, myAngle and pulseWidth. There is one for each servo. The explanation of each will follow.
int myAngle1;
int myAngle2;
int pulseWidth1;
int pulseWidth2;


myAngle is used for defining where you want the servo to be. If you set it to 0, it will be all the way to the right. If you set it to 180, it will be all the way to the left! 90 will be in the middle.
pulseWidth converts the angle to microseconds. You will definitely have to play around with this if you will be using different servos. I used trial and error to get the values to work for the Draganfly servos. I did try this with a Kondo servo, and it worked well! It all depends. Sometimes you may have to tinker with it, sometimes not.
Now, we have to have two methods that we can call in the loop. So, we will call them servoPulse1 and servoPulse2.
Here is the code for each:


*Note: This does not go into the void loop just yet!

void servoPulse1 (int servo1, int myAngle1) {
pulseWidth1 = (myAngle1 * 11) + 500; // Converts angle to microseconds
digitalWrite(servo1, HIGH); // Set servo high (turns it on)
delayMicroseconds(pulseWidth1); // Wait a very very small amount
digitalWrite(servo1, LOW); // Set servo low (turns it off)
delay(20); // Typical Refresh cycle of servo (20 ms)
}

void servoPulse2 (int servo2, int myAngle2) {
pulseWidth2 = (myAngle2 * 11) + 500; // Converts angle to microseconds
digitalWrite(servo2, HIGH); // Set servo high (turns it on)
delayMicroseconds(pulseWidth2); // Wait a very very small amount

digitalWrite(servo2, LOW); // Set servo low (turns it off)
delay(20); // Typical Refresh cycle of servo (20 ms)
}

What happens in this code? We change an angle into a pulse width, turn on the servo for the calculated time pulseWidth, then we turn the servo off!


Part 2 - Meat & Mashed Potatoes
This part is the really good stuff! It all connects in this part.
Next is the void setup(). We just have to put our servo outputs here.

void setup() {
pinMode(servo1, OUTPUT);
pinMode(servo2, OUTPUT);
}


This means that servo1 (defined above as 7) and servo2 (defined above as 6) will be outputs.
Now for the fun part - the loop! This is the main course of the meal, when it comes to the Arduino program. Here is the code, the explanation will follow!
void loop() {
for (myAngle1=0; myAngle1<=180; myAngle1++) {
myAngle1 = 100;
myAngle2 = 90;
servoPulse1(servo1, myAngle1);
servoPulse2(servo2, myAngle2);
}
}


We make a for loop so that the servo will not get caught in the Arduino loop. Slightly confusing, but it works! The way I like to look at for loops, is by looking at them by a while perspective. So, we start off by saying that myAngle1 is 0. Now, we say while myAngle1 is less than or equal to 180, we will increment myAngle (that's what the myAngle1++ means). Okay, now we will set the first servo to 100 degrees. The second one will be set at 90 degrees. We now make sure that the Arduino does move the servo to that angle by calling the servoPulse method we created before.
Pretty neat stuff! It is now very simple for you to move your servo with Arduino. Basically, you just change myAngle1, call the servoPulse method, and your servo will move! I hope that this will help you more!
Personally, my future goals with Arduino and servos is to one day create an interface with Processing to make knobby switches that will move the servos. From there, I may order 2 Arduinos, or somehow expand 1 Arduino, to have 18 servos so that I am able to control my MANOI AT01 humanoid robot.

Please feel free to contact me if you have any further questions!
erin ~at~ robotgrrl.com OR you can message me on learnhub :)

  1. gloriaarze-bravo saidFri, 16 May 2008 19:43:03 -0000 ( Link )

    Excellent, Erin! Thanks for the great lesson.

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

  2. wiredup saidSat, 23 Aug 2008 21:39:26 -0000 ( Link )

    I just got started with arduino. Did one servo control project using a potentiometer. Now I’m going to try the method you describe here to control the servo. Thank you for posting.

    Actions
    Vote
    Current Rating
    1
    Rate Up
    Rate Down
    1 Total Vote

    Post Comments

  3. RobotGrrl saidThu, 09 Oct 2008 22:24:03 -0000 ( Link )

    No problem wired!

    My method doesn’t use the Servo library though as it has never worked for me. Maybe it will work for you, so it’s worth a shot! :D

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

  4. jocool saidMon, 10 Nov 2008 23:51:41 -0000 ( Link )

    i’ve tried your sketche but all i get is a continous motor run… even if i change the myAngle to whenever Angle, it just have an impact of the speed, not the angle to where to stop my servo..

    maybe i’ve missed something (i’ve adapted your sketche for 1 servo (Parallax continuous rotation servo)...

    cheers

    j.

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

  5. jocool saidFri, 05 Dec 2008 18:20:10 -0000 ( Link )

    thank’s for the answer… in fact i understood my mistake while writing my message… but anyway it’s good to hear from you!

    best from montreal!

    Actions
    Vote
    Current Rating
    0
    Rate Up
    Rate Down
    No Votes

    Post Comments

Your Comment
Textile is Enabled (View Reference)