Arduino Servo Motor Guide in 2019: All you need to Know about Servos

Arduino Servo Motor Guide in 2019

Hey Guys welcome back to the another arduino tutorial and in this “Arduino Servo Motor Guide”, you are going to learn about servo motors and how to interface them with arduino. So Let’s get started to this “Arduino Servo Motor Guide in 2019”.

 

(1)  What is a servo motor? And why do we use them in the first place?

Servo motor is basically an assembly of 4 things : A dc motor, Some gears, Potentiometer(position sensor) and Control circuit.

And this whole assembly gives us the precise control or rotation of the shaft of the motor.

 

NOTE: You can also watch this “Servo Motor Step by Step Guide” video for better understanding or read the written tutorial below:

 

(2) Difference between Servo motor and D.C Motor. (Why use Servo Motor over DC Motor?)

Suppose you want to make a project where precise motor control is needed. And you decided to go with dc motor. Then on operating the dc motor, you will observe some delay during starting and stopping of the motor. Due to which its difficult for you to figure out when to power it on and when to power it off. Whereas with servo motor you can precisely control the rotation or movement of the shaft according to your requirements..

 

(3) How does a Servo Motor works or how the combination of dcmotor, gears, potentiometer and control circuit gives us the precise control?

 

Let me make this simple for you by explaining practically the working of a Servo Motor.

1) Given below is a dc motor. Gears are attached to its shaft as shown and to one of the gears, rotating knob of the potentiometer is connected.

Servomechanism of a servo motor

 

2) We know that potentiometer have 3 terminals, two end terminals and one middle terminal which moves. End terminals of the pot. is connected to 5v and Gnd respectively. Whereas middle terminal goes to the -ve terminal of Op amp or operational Amplifier.

 

Note: Work of the Operational Amplifier is to just compare the voltage difference bw its two terminals i.e, OP-AMP is working as a voltage Comparator here. If there is 5 volts at its +ve terminal and 3 volts at its negative termina than o/p will be 5v – 3v = 2 volt, simple as that.

OP-AMP as a Comparator
OP-AMP as a Comparator

 

3) Now +ve terminal of the opamp is connected to 5v and the o/p terminal of opamp to the dc motor whereas the remaining terminal of dc motor is grounded.

Servomechanism

 

4) Lets suppose the middle terminal of the potentiometer is at far end position(GND) so that voltage at the middle terminal is 0. Then O/P will be 5 v – 0 v = 5 volts. This means there is 5 volts across the dc motor. So dc motor will start rotating and so the gears as well as rotating knob.

 

 

5) And as soon as knob rotates, the voltage difference between the middle terminal of potentiometer and ground inceases and we get some voltage at -ve terminal of the OP-AMP or middle terminal of Potentiometer. Suppose voltage is 3 volts, then the OP-AMP o/p will be 5v – 3v = 2 volts. So now there is 2 volts across the dc motor due to which motor continue to rotate.

 

2 Volts across DC Motor
2 Volts across DC Motor

 

6) And same thing will happen untill the middle terminal of potentiometer reaches the other end point(5Volt) and we get 5 volts at the negative terminal of OP-AMP and thus 0volts at the OP-AMP’s output. That means 0 volt across the dc motor. Due to which motor will stop.

 

Note: So you saw how the whole assembly is working in a feedback loop. This complete feedback mechanism is called Servomechanism.

7) If you want the motor to rotate less, lets suppose according to 3 volts than motor will stop as soon as the difference across OP-AMP’s terminal becomes 3V – 3V =0 volts similarly for 2 v or 1volt. But in each case shaft rotates less than the 5 volt case.

 

(4) What is inside a real Servo Motor ?

This was just to demonstrate you the principal of servo motors. In actual servo motors there is a perfect control circuit in place of OPAMP. And instead of giving direct voltage, a PWM signal is given to control the motor.

 

In the example shown above there was total three wires which you could control:  +ve terminal of opamp, and the end terminals of the potentiometer. Or in other words +ve terminal (one end of Pot.), Gnd terminal(other end of Pot.) and Control terminal (OP-AMP’s +ve terminal).

3 Wire terminal of ServoMechanism
3 Wire terminals of ServoMechanism

 

And this is the case of an actual servo motor where we have 3 same wires:  +ve terminal, Gnd terminal and Control terminal.

Terminals of a Servo Motor
Terminals of a Servo Motor

 

Note: The maximum rotation of a typical Servo Motor is limited to 180 degrees.

 

(5) How to drive Servo Motors?

As I have already mentioned: servo motors are controlled using PWM pulses, the frequency of this Pwm signal must be around 50hz. And the width of the pulse determines the angular position of the servo motor.

So if you want angular position to be 180 degree than pulse duration must be around 2.5ms., for 90 degree 1.5 ms and for 0 degree 0.5 ms. For angular position other than 0, 90 and 180 use pulse duration accordingly.

 

Pulse width for respective degree position
Pulse width for respective degree position

 

Note: Please keep in mind that the voltage required to run servo must be between 4 to 6 volts

 

Read Also: Beginners Guide to Ultrasonic Sensors before this Arduino Servo Motor Guide.

 

(6) How to Drive Servo Motors using Arduino?

Servo Motor can be run using arduino by using simple digitalwrite commands which will generate pwm on the selected digital i/o pin and here’s the code for the same:

 

PWM pin Code to run Servo Motor using Arduino:

#define servo 9

void setup() {
 pinMode(servo, OUTPUT);

}

void loop() {
  digitalWrite(servo, HIGH);
  delayMicroseconds(550);

  digitalWrite(servo, LOW);
  delayMicroseconds(19450);
  
 

}

 

 

(7) How to Drive Servo Motor using Servo Library?

Now there’s a more convenient way of running servo motor which is by using Servo Motor Library. Using this library you can easily define the degree to which you want your servo motor to rotate to. And by using the servo motor library its very easy to run more than 1 servo motor using Arduino. So now lets try to run the servo motor using this servo motor library.

 

A) Driving Servo Motor to a particular degree position using Arduino Servo Library:

#include <Servo.h>

Servo servo1;


void setup() {
  
  servo1.attach(6);
  
  
}

void loop() {
  servo1.write(180);
  
}

 

A) Driving Servo Motor in a loop using Arduino Servo Library:

#include <Servo.h>

Servo servo1;


void setup() {
  
  servo1.attach(9);
  
  
}

void loop() {
  servo1.write(0);
  delay(1000); 

  servo1.write(90);
  delay(1000);

  servo1.write(180);
  delay(1000);
 
  servo1.write(90);
  delay(1000);

}

 

C) Driving Two Servo Motors in a loop using Arduino Servo Library:

#include <Servo.h>

Servo servo1;
Servo servo2;

void setup() {
  
  servo1.attach(6);
  servo2.attach(9);
  

}

void loop() {
  servo1.write(0);
  delay(1000);
  servo2.write(0);
  delay(1000);

  servo1.write(90);
  delay(1000);
  servo2.write(90);
  delay(1000);

  servo1.write(180);
  delay(1000);
  servo2.write(180);
  delay(1000);

  servo1.write(90);
  delay(1000);
  servo2.write(90);
  delay(1000);

}

 

In the figure given below two servo motors are connected to 1 Arduino.

Driving two servo motors using Arduino
Driving two servo motors using Arduino

 

NOTE: You can run as many as you want but make sure if you are running more than 2 servo motors than don’t use Arduino to supply power to the motors. Or don’t connect the power pin of servo motors to Arduino. So always use a separate 6v battery to power it. Because Arduino cannot provide enough current to more than 2 motors.

So this was your “Arduino Servo Motor Guide in 2019”.

No Comments

Leave a Reply

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

%d bloggers like this: