Public post

6.2 Controlling the position of the servo motor with Arduino

· 6. SERVO MOTORS

In this tutorial, you will control the position of a servo using an angle calculated in your sketch. Servos are the easiest way to start making the motion with a microcontroller. Unfortunately, servos do not turn 360 degrees but only 180.

Materials you will need

Component Number
Arduino Uno Rev3 1x
Jumper Wires 4x
Breadboard 400 points 1x
Servo Motor 1x
100 uF Capacitor 1x

Circuit Diagram

Circuit Diagram – Atach 5V and ground to one side of your breadboard from the Arduino.  The servo has three wires coming out of it. One is power (red), one is ground (black), and the third (white) is the control line that will receive information from the Arduino. Plug three male headers into the female ends of the servo wires. Connect the headers to your breadboard so that each pin is in a diferent row. Connect 5V to the red wire, ground to the black wire, and the white wire to pin 9.

When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a 100uf capacitor across power and ground right next to the male headers , you can smooth out any voltage changes that may occur. You can also place a capacitor across the power and ground going into your potentiometer. These are called decoupling capacitors because they reduce, or decouple, changes caused by the components from the rest of the circuit. Be very careful to make sure you are connecting the cathode to ground (that’s the side with a black stripe down the side) and the anode to power. If you put the capacitors in backwards, they can explode.

The Code

#include <Servo.h>

Servo myservo; // create servo object to control a servo
int angle = 0; // variable to store the servo position
void setup()
{
  myservo.attach(9); // attaches the servo on pin 10 to the servo object
}
void loop()
{
  for (angle = 0; angle < 180; angle += 1) 
  //goes from 0 degrees to 180 degrees 
 { // in steps of 1 degree 
 myservo.write(angle); // tell servo to go to position in variable 'angle'
 delay(20); // waits 20ms between servo commands } 
 // goes from 180 degrees to 0 degrees
 for (angle = 180; angle >= 1; angle -= 1) 
  {
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(20); // waits 20ms between servo commands
  }
}

Code Explanation

Import the Servo Libary

To use the servo library, you’ll first need to import it. This makes the additions from the library available to your sketch.

#include <Servo.h>
Create a Servo Object

To refer to the servo, you’re going to need to create a named instance of the servo library in a variable. This is called an object. When you do this, you’re making a unique name that will have all the functions and capabilities that the servo library offers. From this point on in the program, every time you refer to myServo, you’ll be talking to the servo object.

Servo myservo; // create servo object to control a servo
Declare a variable to hold the servo angle

Set up a variable for storing the angle of the servo motor is very important.

int angle = 0; // variable to store the servo position
For Loop to move the Servo Motor

You arrived at the void loop() part of the sketch. Since we start at a zero angle you can use a for loop to go to 180 degrees. In the for loop initialization start with the angle, then a condition is tested. If the tested condition is true the code inside the for loop is executed and the angle is incremented by 1.

// goes from 0 degrees to 180 degrees
for (angle = 0; angle < 180; angle += 1) 
 { 
// tell servo to go to position in variable 'angle' 
myservo.write(angle); 
delay(20); // waits 20ms between servo commands }
Return for loop to original position

To move the servo motor back again you need a different for loop. If the angle of the servo motor is at the max, it will be in an angle of 180 degrees. Therefore, if you want it to go back you need to decrement. The for condition is met when angle is 180 degrees and greater or equal to 1 degree. The code will be executed and the angle will be decremented with 1.

// goes from 180 degrees to 0 degrees
for (angle = 180; angle >= 1; angle -= 1)  { 
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(20); // waits 20ms between servo commands }

Become a member

You just read a free post but there are 2 member-only posts that you don't currently have access to.

Subscribe for $14.99 monthly or $99.99 yearly.

Become a member

Comments

Comments are for members only.
Please become a member or sign in to join the conversation!

You've successfully subscribed to ArduinoPlatform
Welcome! You are now a ArduinoPlatform subscriber.
Welcome back! You've successfully signed in.
Success! You are now a paying member and have access to all content.
Success! Your billing info is updated.
Billing info update failed.