Jump To Content

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 :)
gloriaarze-bravo
  • Authority 423
Post Body
gloriaarze-bravo said:

Excellent, Erin! Thanks for the great lesson.

  • Quote
  • Posted 7 months ago.
wiredup
  • Authority 7
Post Body
wiredup said:

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.

  • Quote
  • Posted 4 months ago.
RobotGrrl
  • Authority 256
Post Body
RobotGrrl said:

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

  • Quote
  • Posted 3 months ago.
jocool
  • Authority 15
Post Body
jocool said:

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.

  • Quote
  • Posted about 1 month ago.
robodude666
  • Authority 5
Post Body
robodude666 said in response to:
jocool
jocool’s post:
Citation Body

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.

A continuous rotation servo does not have a potentiometer or stop inside of it, so the servo doesn’t “know” what angle or position it is on. All you can do with continuous rotation servos is control speed by PWMing the power, or control direction. If you need continous rotation and angle detection, then you’ll need to get a regular DC motor and an encoder. Try to get a motor with a built in encoder, or encoder attachment if angle is very important in your project. There are optical encoders which are cheaper, but they require a colorful disc on the wheel which adds errors.

  • Quote
  • Posted about 1 month ago.
jocool
  • Authority 15
Post Body
jocool said:

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!

  • Quote
  • Posted about 1 month ago.
  • Your comment will be modifiable for 10 minutes after posted.

Page Author

Avatar
RobotGrrl
Name
RobotGrrl

From Here You Can…

Information

  • 1132 Views
  • 6 Comments
  • Ratings Likes 4 Negative 0

Most Recent Related Content

Published In…