Servo motor control and interfacing with Arduino

Servo motor control using Arduino Uno R3

Movement of objects can be controlled by controlling motion of motors. similarly in robotics, servo motor control mechanism is very useful to control position of robotics. In this article I am going to write about servo motor control using Arduino Uno R3. you will learn following knowledge after reading this article :




  • What is servo motor?
  • Do we need servo motor controllers?
  • Circuit diagram of servo motor interfacing with arduino.
  • Code for position control of servo motor.

What is servo motor :

Servo motor is position controlled motor. It can easily control physical movement of objects due to its position controlled feature. Servo motor can not move continuously.  servo motors rotate in angel ranging from 0 to 180 degree. Servo motor have many applications in robotics and industry for position based motion control system. Unlike other motors, servo motor is very easy to interface with Ardino or any other microcontroller due to its built-in controllers.

Do we need servo motor controller :

As I already mentioned above, servo motors are very easy to interface with Arduino because It have built-in motor controllers. So we need only three pins to interface servo motor with Arduino. power pin, ground pin and pulse signal pin.I will talk about  signal pin in later part of this article. Let’s talk more about servo motor built-in controller. Servo motor consists of following parts or in other words complete servo motor package consists of following components :

  • Gears is used to control position in degrees
  • Output shaft is used to connect with physical objects
  • Potentiometer for position feed back
  • pulse to voltage converter for feedback voltage to potentiometer
  • H bridge to control the position of motor clock wise or anti clock wise
  • Comparator make with amplifier

Servo motor can not move with continuous motion unless feedback potentiometer is connected. H bridge is used to rotate motor either in clock wise or ani clock wise direction. So the main advantage od servo motor is that it doesn’t require any interfacing circuit. But the main disadvantage of servo motor is its lower speed and low power limitation as compared to other motors.

How servo motor works :

Servo motor respond to change in duration of pulses. A pulse of duration 1 mili second cause the servo motor to move one end and duration of 2 mili second cause the motor to move other end.  So we can calculate servo motor position by varying the duration of pulses to servo with the help of Arduino Uno R3.  Arduino IDE have built-in library for servo motor control. So you don’t need to write lengthy code for servo motor. You just have to call function used in servo.h library.




How to interface servo motor with Arduino :

Servo motor control using Arduino Uno R3 can be easily done by interfacing servo motor with Arduino. Servo motor have three input pins as shown in circuits below. Power supply pins and pulse signal pin. Interfacing diagram is shown below :

servo motor interfacing with Arduino Uno R3

servo motor interfacing with Arduino Uno R3

5volt and ground pin of servo  is connected with 5 volt and ground pin of Arduino. Pulse pin is connect with pin number of Arduino Uno R3. Check following circuit diagram for more clear view :

servo motor interfacing

servo motor interfacing

Code for servo motor control :

For example you want to control the position of servo motor with specific or calculated angle. Following code is written for calculated angle position control of servo motor.

#include<Servo.h>

Servo myloop; // create servo object to control a servo
int angle = 0; // variable to store the servo position
void setup()
{
myloop.attach(10); // attached the servo on pin 9 to the servo object
}

void loop()
{
for(angle = 0; angle < 180; angle += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myloop.write(angle); // tell servo to go to position in variable 'angle'
delay(20); // waits 20ms between servo commands
}
for(angle = 180; angle >= 1; angle -= 1) // goes from 180 degrees to 0 degrees
{
myloop.write(angle); // move servo in opposite direction
delay(20); // waits 20ms between servo commands
}
}

Servo.h in start of code is a header file of servo motor library. I have made necessary comments in code for your understanding purpose. Share this article with your friends. Thanks

4 Comments

  1. Huda July 20, 2015
  2. Huda July 20, 2015
  3. jonah ajeibi February 2, 2016
  4. jonah ajeibi February 2, 2016

Add Comment

Subscribe to our blog to get updates in your email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 704 other subscribers