Learn to code

ARDUINO & CODES

Previous Post
Introduction into Arduino DC Motor Control with Arduino
Next Post
Control a Servo Motor with a Potentiometer and Arduino

Controlling the position of the servo motor with Arduino

in Arduino Motor Control

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 motion with a microcontroller. Unfortunately, servos do not turn 360 degrees but only 180. 

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 7
Servo Motor Servo Motor × 1
100 uF Capacitor 100 uF Capacitor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

 

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 } for (angle = 180; angle >= 1; angle -= 1) // goes from 180 degrees to 0 degrees
  {
    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 ofers. 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 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.

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
  }
Return for loop to original posistion

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.

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

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu