Menu Close

Arduino Servo Control

This tutorial is about servo control on Arduino. Arduino has got a library for servo control. This tutorial explains how to control servo by using in-built library of Arduino. It has got two examples of servo control to give a better understanding of servo control.

1. Introduction:

A step by step illustrated basic tutorial for Arduino. This tutorial explains Servo motor control through Arduino. We have included two exmaples in this tutorial

1.1 Servo Motor:

The meaning of servo is feedback. So a servo is an actuator that takes feedback itself and moves precisely. Example of preciseness and feedback can be understood by a daily life example, suppose you pull up/down glass of your car window, You push up/down the power window button and keep on watching the actual position of glass as it reaches to the desired position you releases the button. So this is feedback, we are taking feedback here, if it would be an feedback based system we would have to tell open window by 10%-20%.

Lets come back to servo motor. Servo motor rotate from 0 degree to 180degree. We send the command to servo as it reaches to the commanded value it stops there. The below diagram exhibits how it rotates and its wire interface.

1.2 Servo and Arduino:

Arduino has got a library to control servo. It is – Servo.h.

This library can control above shown servo motors. This library supports up to 12 servos on most Arduino boards and 48 servos on Arduino Mega. It disables analogWrite() for Pin 9 & Pin 10 except Arduino Mega.

This tutorial is made on Original Arduino UNO

2. Required Hardware

S.No.ItemQuantity
1 Arduino UNO 1
2 Breadboard 1
3 Servo Motor 1
4 Preset 10k 1
5 Jumper wire male to male 4
6 Jumper wire male to female 3

3. Building Circuit – 1 

This is first example of servo control. Servo alone is controlled through Arduino. In this examples servo takes 5 motions-

1. 0 degree to 45 degree

2. 46 degree to 90 degree

3. 91 degree to 135 degree

4. 136 degree to 180 degree

5. 180 degree to 0 degree (Back to zero)

After completing every movement it stops there for one second.

To run this practical example make following circuit with the help of above mentioned components.

Schematic Diagram:

Circuit Layout:

4. Programming –

Once we are done with circuit part, here is our programme to this circuit. All of the commands are explained in the comment section. Output video is attached at the last of this tutorial, This example will be clear after watching the video.

You may download this code (Arduino Sketch) from here.

// Servo Control Tutorial#1
// Prepared by Robo India
// www.roboindia.com

// Arduino Servo Library can add up to 12 servo on most of Arduino boards.
#include <Servo.h>  // Includes servo library.
 
Servo servo_1;      // Creating Servo object.
 
int servo_pos = 0;   // Storing servo position (0 degree to 180 degree)
 
void setup() 
{ 
  servo_1.attach(3);  // Attaching servo to Pin No.3 
} 
 
 
void loop() 
{ 

  for(servo_pos = 0; servo_pos <= 45; servo_pos++) // loop to go to 45 degree from 0 degree.
   {                                   // increment of 1 degree in each step
    servo_1.write(servo_pos);          // commanding servo to reach at Servo_pos.
    delay(15);                         // waiting a bit for the servo to reach commanded position. 
   } 
  delay(1000);                         //Delay of 1 second to observe stoppage of servo. 
  for(servo_pos = 46; servo_pos <= 90; servo_pos++)  // loop to go to 90 degree from 46 degree. 
   {                                   
    servo_1.write(servo_pos);              
    delay(15);                        
   } 
  delay(1000);
  for(servo_pos = 91; servo_pos <= 135; servo_pos++)  // loop to go to 135 degree from 91 degree. 
   {                                  
    servo_1.write(servo_pos);              
    delay(15);                        
   } 
  delay(1000);
  for(servo_pos = 136; servo_pos <= 180; servo_pos++) // loop to go to 180 degree from 136 degree. 
   {                                   
    servo_1.write(servo_pos);               
    delay(15);                        
   } 
  delay(1000);  
  for(servo_pos = 180; servo_pos>=1; servo_pos--)     // Loop to go back to 0 degree.  
   {                                
    servo_1.write(servo_pos);               
    delay(15);                       
   } 
  delay(1000);
} 

5. Circuit

This is second example of Servo Control. In this example We are controlling servo through Analog input. Analog input is taken from a potentiometer. And command of motion is given to the attached servo as per the input we are getting from potentiometer.

Make the following circuit-

6. Programming 

Here is programming for example – 2 (above mentioned circuit).

Points to under stand.

1. Analog input is taken through Potentiometer on Pin A0. It will give us value from 0 to 1023.

2. The servo library need value 0 to 180 to give command to servo.

3. So we will convert our input values (0-1023) to the command value for servo (0-180)

4. This converted value is sent to the servo through Pin 3.

The following programme explains all of the command in comment sections.

You may download the codes (Arduino Sketch) from here.

// Servo Control Tutorial#2
// Prepared by Robo India
// www.roboindia.com

// Arduino Servo Library can add up to 12 servo on most of Arduino boards.
#include <Servo.h>  // Includes servo library.
 
Servo servo_1;      // Creating Servo object.
 
int input_pin = A0;  // Analog Input Pin (Potentiometer)
int input_val;       // to store analog Input value (0-1023)
int servo_angle =0;  // Servo angle value (0-180 degree)
 
void setup() 
{ 
  servo_1.attach(3);  // Atteching Arduino's Pin 3 to servo. 
} 
 
void loop() 
{ 
  input_val = analogRead(input_pin);             // To read analog input value (0-1023) 
  servo_angle = map(input_val, 0, 1023, 0, 179); // Converting input value (0-1023) to servo angle (0-180)
  servo_1.write(servo_angle);                    // Commanding servo to reach servo angle 
  delay(15);                                     // waiting for servo to reach commanded angle. 
}  

7. output:

Here is the output of this tutorial. This video contains both of the practicals we have done above.

8. Real life example

Following is one real life example of Servo motion. 5 Axis robotic arm by Robo India. Such robotic arms are highly useful in industries.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com


Leave a Reply