How to use Servo Motor with Arduino- Code & Working Explained

Servo Motor is one of the most widely used motors in toys, CNC machines, 3D printers for precise control. By the end of this post, you will have a clear understanding of servo motors, how they work and how to use a Servo Motor with Arduino.

What are the different parts of Servo?

Servo motor is composed of 4 important parts: A dc motor, Some Gears, a Potentiometer(position sensor), and a 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:

[arve url=”https://youtu.be/1RWEJFZ9bdo” maxwidth=”555″ aspect_ratio=”4:3″ /]

How Servo is different from DC Motor?

Suppose you want to make a project where precise motor control is needed. And you decide to go with a dc motor. Then on operating the dc motor, you will observe some delay during starting and stopping of the motor.

Due to this, it’s difficult for you to figure out when to power it on and when to power it off.

Whereas with a servo motor you can precisely control the rotation or movement of the shaft according to your requirements.

How does a Servo Motor work?

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

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.

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

Note: The 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 are 5 volts at its +ve terminal and 3 volts at its negative terminal then o/p will be 5v – 3v = 2 volts, simple as that.

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 the dc motor is grounded.

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

And as soon as the knob rotates, the voltage difference between the middle terminal of the potentiometer and ground increases.

We get some voltage at the -ve terminal of the OP-AMP or middle terminal of the Potentiometer. Suppose voltage is 3 volts, then the OP-AMP o/p will be 5v – 3v = 2 volts. So now there are 2 volts across the dc motor due to which the motor continues to rotate.

And the same thing will happen until the middle terminal of the potentiometer reaches the other endpoint(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 volts 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.

If you want the motor to rotate less, let’s 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.

Servo Motor PWM control

In 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 a Servo motor, there are three wires to control:  +ve terminal of the 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).

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

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

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 the 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.

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

How to use Servo Motors with Arduino?

Servo Motor can be run using Arduin by using simple digital write commands that generates Pulses 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);

}```

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);

}```

B) 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.

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”.