The servomotor is an electric motor which is fundamentally characterized by its extreme controllability: it allows precise adjustment of angular position, acceleration and speed. To achieve these results, in addition to the servomotor itself, a rotor position sensor is also part of the basic architecture of the device. This sensor also called rotary encoder or motor feedback system is able to accurately detect the position of the motor shaft at a given moment. Most of the servo motors available can rotate between 0 degrees to 180 degrees precisely. Here we will drive servo motor with Arduino using Servo.h Library in its IDE.

1. CONCEPTS

What is a Servo Motor?

Inside the servo, there is a motor, a series of gears that reduce the speed of the motor, a control circuit and a potentiometer. The motor and potentiometer are connected to the control circuit and the combination of these three elements defines a closed-loop feedback system. The circuit and the motor are powered by a stabilized direct voltage, generally of value between 4.8 V and 6.0 V, although many motors are able to accept power inputs up to 7.2 V.

tower pro sg90 servo motor
Figure 1: Tower Pro SG90 Servo Motor

To run the engine you must send a digital signal to the control circuit. This activates the motor which, through a series of gears, is connected to the potentiometer. The position of the potentiometer shaft indicates a measurement of the position of the servo motor shaft. When the potentiometer reaches the desired position, the control circuit turns off the motor.

It is easy to deduce that servomotors are generally designed to perform a partial rotation rather than set a continuous rotary motion, as in the case of a continuous or stepping motor. Although it is possible to configure an R/C servo to rotate continuously, the fundamental use of a servo is to achieve an accurate position of the motor shaft, with movements in the range between 90 ° and 180 ‘. Although this movement does not seem considerable, it can be more than enough to manoeuvre a robot, to raise and lower the legs, to rotate a sensor that has to examine what surrounds them and much more. The precise rotation of a corner by a servo in response to certain digital signals represents one of the most used features in all fields of robotics.

Specifications of SG90 Servo Motor

  • Operating Voltage: 4.8V to 7.2V
  • Torque: 1.6 kg/cm to 2.5 kg/cm
  • Operating speed: 0.1s/60°
  • Rotation : 0°-180°
  • Operating Temperature: -30°C to 60°C

Working of a Servo Motor

The PWM signal consists of a series of ON and OFF pulses. The pulses must follow each other with a frequency of 50 Hz or width of 20ms. This width determines the angular position of the servo. With a pulse of 1.5ms, the servo positions itself in an intermediate position (90 degrees), with 1ms pulse the servo rotates completely on one side (0 degrees) and with 2ms long-pulse the servo rotates on the opposite side (180 degrees).
Figure 2 below shows pulses of different duration and the angular position that the servo consequently takes

Pulse Width Signal PWM time interval required for a servo motor to work
Figure 2: Different duration and the angular position that the servo consequently takes according to the width of Pulse

Controlling Servo Motor with Arduino

To control Servo motor with Arduino we can use Servo.h library which can control 12 Servo motors on most Arduino boards and up to 48 Motors on Arduino Mega. Kindly note that using Servo.h library will disable analogWrite()/PWM functionality on pin 9 and 10 (except Arduino Mega), whether or not you attach any servo on these two pins. Moreover on Arduino Mega 12 servo motors can be controlled without interfering with PWM functionality of any pin. But if you go beyond 12 servos, PWM on pin 11 and 12 will be disabled.

Please note that servos draw significant power, so if you need to drive more than one or two, you’ll apparently need to power them from a separate supply (not the 5V pin on your Arduino). Be sure to connect the grounds of the Arduino and external power supply together.

2. COMPONENTS

3. CONNECTIONS

Figure Circuit Drive Servo Motor with Arduino SG90 Servo Code
Figure Circuit Drive Servo Motor with Arduino SG90 Servo Code

Figure 3: Connections; SG90 Servo Motor with Arduino

4. PROGRAMS

Program 1: Sweep Servo motor from 0 to 180 degrees and vice versa.

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM:SWEEP SERVO TO AND FRO
  Sweep Servo motor from 0 to 180 Degree and vice versa.

  Connections:
  Arduino       Servo
  9             Signal(Orange/Yellow)
  5V            VCC (Red)
  GND           GND (Brown)
*/

#include<Servo.h>     //include Servo.h header file
Servo myServo;        //give a name to your Servo
void setup() {
  myServo.attach(9);  //attach servo to pin 9
}

void loop() {
  myServo.write(0);   //rotate servo to 0 degrees
  delay(2000);        //wait for some time
  myServo.write(180); //rotate servo to 180 degrees
  delay(2000);        //wait for some time
}

Program 2: This program rotates servo motor to a specific angle entered on the Serial Monitor.

/*UNCIA ROBOTICS | www.unciarobotics.com
  PROGRAM:ROTATE SERVO MOTOR TO A SPECIFIC ANGLE
  This program rotates servo motor to a specific angle entered
  on the Serial Monitor.

  Connections:
  Arduino       Servo
  9             Signal(Orange/Yellow)
  5V            VCC (Red)
  GND           GND (Brown)

  IMPORTANT : On Serial monitor Select "No line ending".
*/

#include<Servo.h>           //include Servo.h library
Servo myServo;              //give a name to your Servo
void setup() {
  myServo.attach(9);        //attach Servo to pin 9
  Serial.begin(9600);       //Start Serial communication
}

void loop() {
  if (Serial.available())   //if Serial data is available
  { unsigned long angle = Serial.parseInt();//read till timeout
    if (angle <= 180 && angle >= 0) //angle is between limits?
    {
      Serial.println(angle); //print value of the angle
      myServo.write(angle);  //write the angle on Servo motor
    }

    else if (angle > 180 || angle < 0)
    {
      Serial.println("Invalid");//otherwise print Invalid
    }
  }
}

Important: Select “No line ending” option on Serial Monitor as shown in Figure 4 below. otherwise, the servo will not work properly.

Figure 4: Arduino Serial Monitor

5. FUNCTIONS & LIBRARIES USED



6. FAQs

What is the use of Servo Motors in Robotics Projects?

Servo drives play a very important role for many projects, from robotic to smart home systems. Everything related to the movement traditionally requires special knowledge and creating a full-fledged correctly working drive is not an easy task. But with the help of servomotor, it is possible in many cases to simplify the task, so the servo is constantly used even in entry-level projects. In robotics projects, the servo is often used for the simplest mechanical actions, for example:

  • Turn the range finder or other sensors a certain angle to measure the distance in a narrow sector of the robot’s field of view.
  • Take a small footstep, limb or head movement.
  • To create robotic manipulators.
  • To implement the steering mechanism.
  • Open or close a door, shutter or another object.
  • Of course, the scope of the servo in real projects is much wider, but the above examples are the most popular schemes.
Still, having doubts?

Ask your questions on how to interface servo motor with Arduino in the comments section below or Contact Us.

Leave a Reply