How to control servomotor with arduino

                               Control Servomotor with Arduino



                        In this post you Learn how to control Servomotor with Arduino how to attached servomotor with arduino, coding, and how to control the angles of servo and by using all of these informations how to make a Robotic arm for any Dof( degree of freedom )  6 Dof , 5 Dof, 4 Dof, 3Dof and others. You also able to make you Robot automate means pic and place robotic arm you can control many Servo Motors by using One Arduin uno.

What is Servo motor                

                  A Servomotor is a closed loop control system and it is also known as rotatory actuator that allows a precise linear or angular control position, it ues feedback system because of servo closed loop mechanism to control the next motion or position of the motor.

How Servo motors work

                            All the servo motors have INPUT in the form of Electrical signals and OUTPUT is in the form of analog rotatory shaft. When commanded a position to the servo it it consumes some power and rotates to the specific angle and then servomotor rests. whatever the angle instruct to it by controller it quickly turned to it. Because of it high performance it uses alternative to the stepper motor.

Stepper motor is built-in outputs steps that makes the inherent ability for position controlling system. But the thing that limits the performance of steppers is their lack of feedback as the stepper motor can only drive a load that is well within its capacity, otherwise missed steps under load may lead to positioning errors and the system may have to be restarted or recalibrated. 

For optimizing the performance of servomotors the encoders are introduced with it. The encoder and controller of a servomotor are an additional cost but they make the better performance of speed, accuracy and power of motor for overall system relative to the capacity of the basic motor.
It has many applications in the field of robotics automation, aeroplanes, CNC machines.

servomotor-using-arduino it can rotates 0 to 180 degree.

Using Servomotorwith Arduino

          Servomotors are very easy to use with arduino. if you have basic knowledge of arduino and if you know that how to connect and rotate 1 servomotor with arduino and how to change its angle in different angles and the position of servomotor using with arduino here you learn all of those things.

I am sure that after reading this artical you are able to control not only one  but a lot of servomotors with one arduino and also you are able to command many servomotors with different angles you want. In collage or universities students mostly used servomotor in Robotics, some are used to make robotic arms that have only program to do one work only like automation for example it pic something from one side and placed it into other side and go to its original position and this process running infinite time in loops that an example of automation companies or the time you set in loops means if you set in loop 500 so your robot do the same work for 500 times and then stop until you reset it. After reading this artical you are able to do the both things.

One more method for controlling servomotor with Arduino is control by potentiometer in this program your servomotor and potentiometer is mapped with one another in arduino programming. It means if you rotate your potentiometer forward the servo is also rotating forward and if you rotates you potentiometer backward the servo is also go backward position this things you learn in my next post Control Servo Motor with Potentiometer Arduino.

Attach Arduino with Servomotor


  step#1: Circuit       




when you buy a servomotor you look 3 wires attach with servomotor is RED, Black, and white normally DC motors have 2 wires so don't get confused with three wires of servomotor the black wire is attached with GROUND and the red is attached with 5 volt and the white one is attached with PWM pins or analog pins A0 to A5.

This is must that you should connect your Servomotor with pwm pins or Analog pins because we give the angle to servo motor we used analogWrite command and in Arduino the PWM and analog pins can be used for analogWrite command if you use normal digital pins then your project or your servomotor can't work or start so its necessary to ues the Pwm or Analog pins.
If you want more detalis about PWM click this link    what-is-pwm-how-to-generate-it.


Connections Servomotor With Arduino

  • Black wire (Ground).
  • Red wire  (5 volt).
  • Gray wire (pwm pin 9). 

step#2: Include library.

You have to write command for starting the liberary of servo. Library is already given in Arduino software you have only write this have to do this step because after this step the Arduino code compile otherwise the error come and your motor dont got the command.

Next declare and describe the PWM pin or analog pin in which you attach your white wire of servo with Arduino for rotation of servo moter. As i told you above that it must be analog pin or Pwm pin otherwise your servo can't work here i am using the Pwm pin number 9.
    int servopin=9;

After deciding and describing the Pin number and name you have to tell the servo library that whats your servo motor name which is attached with pin number 9. So you write that Servo and after this you can write the name of servo which is attach with pin number 9. you can make any name that you want here i am using nameservo.

           Servo nameservo;

 step# 3: setup( ):

After defining the name of our servo now we also tell that our servo whose name is nameservo is
attached to whose pin this is also necessary to write this command because of this command our
controller knows that physically our servo pin connect to which pin of Arduino. you can also write this command with pin number you use as i use (9) or the name you describe as i describe servopin.


step#4: loop.

Here the time came that i told above that you can make you robot automate that can do one thing to yo for example if you want from your robotic arm to pic a thing and placed it to other side these are all done in loop if you want to do this infinite times or some turns like 100 times or more then 100 times but i am doing infinite times .

it means yor servo starts from 0 degree.


it means servo moves at 45 degree then come to zero because of loop.if you want to make several degree you can command it again like.


 now it move 45 to 120 degree.


and finally it moves to 180 degree and after 180 it goes to 0 degree due to loop and this process run infinite times. you can attach several servomotors and give them different tasks different degrees and different works for your Robotic arm all are done in loop if you want to make infinite or to sometimes.  


                             Servomoter with Arduino Code


                                                  #include <Servo.h>

                                                  Servo nameservo;

                                                  int servopin = 9;

                                                  void setup(){



                                                  void loop(){










 For more servomotors all you can change by adding only new servo pin int servopin2= 6; and the name of servomotor Servo  nameservo2; and attaching of servo and the work of loop that you want by these steps you can control many servos with only 1 arduino and make interesting project or robots.  you can also control you angle by loops by making a little change in program.

You have successfully completed one more Arduino "How to" tutorial and you learned how to control Servo Motor with arduino.
I hope you liked this, if you have any query let me know in the comments. 

There will be more of them, so make sure to click Follow button!

Hope you enjoyed the lesson, keep looking at my profile for more arduino lessons soon. 

Next Post »