If you are getting into robotics, you might have wondered how to control movements. Most of you might know about a DC motor. You just apply power and it spins. But how to control it precisely. For example, to make a robotic arm we have to move the arm in a controlled manner without making it go round and round like crazy. This is when we use a Servo Motor.

In this article I am going to explain, what a servo motor is and how you can start using it using it with the help of Arduino.

What Is Servo Motor?

9G Micro Servo

Servo is nothing but geared DC motor with a control circuit and feedback mechanism. Unlike normal DC motors which rotate 360°, servo motors just rotate 180°. Not only that but it has more torque and we can also control the movement precisely using PWM signals.

Servo motors come in a wide verity. There are nano servos weighing just 3g used in micro robotics and RC airplanes. And then there are huge industrial servos weighing in kilos with high torque.
Now that you have an idea of what servo is, let’s see how it works.

How Does It Work?

Internal Mechanism of Servo

As you can see in the image above, servo is made from many components, each one is labeled for better understanding.
First there is a DC motor which rotates the shaft. Then there is a control circuit which controls the direction of rotation. It is nothing but a motor drive circuit. A feed back potentiometer is connected to the circuit which gives us the position of the motor shaft. The control pin of this potentiometer is connected to Signal wire of Servo.
This circuit is enclosed in the servo case. On top section there are gears which reduce the speed of motor and increases the torque. and the Output shaft is where we connect the servo control horns.

How To Control Servos?

To control a servo we need to generate PWM signals, PWM stands for Pulse Width Modulation. There are multiple ways we can do that, we can use a signal generator, use a servo controller module or make one using 555 timer IC. Or we can use an arduino which can not only help use control a servo but also let us use it for multiple projects.

To use a servo will need an Arduino Uno, a breadboard, a potentiometer and servo of course. Now if you are using a micro servo as I have, you can directly power it using Arduino but if you want to use a standard servo, you might wanna use a separate power supply. Okay now let’s take a look at the circuit diagram

Circuit Diagram

As you can see in the diagram, I have used a bread board to make the connections clear. Because I am using 9g micro servo, I will power it using 5v from Arduino. Follow the steps carefully:

  • Connect the 5v and Ground pins of Arduino to power rails of the breadboard.
  • Plug the potentiometer on the breadboard and connect its first terminal to +ve rail. Second terminal to pin A0 (analog pin) of Arduino and third pin to ground rail of breadboard.
  • Next notice the pins on servo, there are 3 wires. Red, brown/black and orange/yellow. The Red wire connects to +ve rail of bread board. The brown/black wire connects to ground rail and finally the orange/yellow wire connects to pin no. 3 (digital pin) of Arduino.

With that been done, all the connections are ready. We now need to upload a code on to the board which will help use control the rotation of servo.

Coding:

Open the Arduino IDE and write down the following code:

#include <Servo.h>

Servo myservo;  
int potpin = 0;  
int val;  

void setup() 
{
  Serial.begin(9600);
  myservo.attach(3);  
}

void loop()
 {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180); 
  Serial.println(val);    
  myservo.write(val);                 
  delay(15);                          
}

In the code above, we have first imported a library called <Servo.h>, this library allows use to use readymade functions to control a servo. This makes coding a lot easier.

Servo myservo;  
int potpin = 0;  
int val;

In this part we declare a servo object and name it “myservo”. This could be anything you want. Next we declare a variable “potpin” and assign it to pin 0. Finally a variable “val” is declared which will store the input by potentiometer.

Next we have added a setup function which will run first and set the pins as we intend to use it (input/output), sets serial communication etc.

void setup() 
{
  Serial.begin(9600);
  myservo.attach(3);  
}

Here the first line enables serial communication at a speed of 9600. Next line declares pin 3 to control the servo. (Note that servo can only be controlled by pins labeled as PWM, 3,5,6,9,10 & 11 are PWM pins)

Finally we added a loop function that will keep running a set of code until the board is restarted or powered off.

void loop()
 {
  val = analogRead(potpin);
  val = map(val, 0, 1023, 0, 180); 
  Serial.println(val);    
  myservo.write(val);                 
  delay(15);                          
}

Here the first line reads data from analog pin 0 where we have connected the potentiometer. This data is stored in a variable named “val”. Next line will map the potentiometer readings to servo angle. In next line we print this value on the serial monitor. And the servo write function will send the mapped value to servo signal pin to rotate the shaft. Lastly I have added a delay of 15 milliseconds.

Uploading & Testing

Now we are ready to upload the code on to the board. First plug the Arduino on to the computer and select the port. Next hit the upload icon and wait for a couple of seconds. Once the code is uploaded, open serial monitor from tools. Now you can rotate the potentiometer and you will notice the servo horn following the movements. You can also see the value on serial monitor.

That’s all for this tutorial, now you can control any servo using Arduino. Try controlling multiple servos and maybe different servos to practice. If you are using standard servos or multiple micro servos, you should use a separate power supply. 6-9v should work. In a future tutorial we will see how to make a robotic arm using servos and Arduino.

If you have any doubts or questions, feel free to ask in the comment section below.

4.5 2 votes
Article Rating