View Course Path

Interfacing of Arduino with servo motor – The definitive guide

The next candidate in our series of Arduino projects is the super useful servo motor. In this post, we will interface the servo motor with the Arduino Uno using a couple of methods. And we’ll understand all there is to know about the motor driver ULN2003.

What are the components required for servo motor interfacing with Arduino?

[geoip_detect2_hide_if country=”IN”] [/geoip_detect2_hide_if] [geoip_detect2_show_if country=”IN”] [/geoip_detect2_show_if]

Why should we interface the servo motor with Arduino?

The servo motor is usually needed for electromechanical projects. Or, more commonly, in robotic applications. Whenever you have a requirement where you need to control the angle of rotation of the axis of a motor, you can use the servo motor. This motor allows you to rotate to a certain degree. Quite unlike the DC motors that run in complete loops as long as power is supplied to it.

The servo motor is one of the essential devices in robotics and industrial sectors where precise motions are a necessity.

Understanding the Arduino servo motor project

What is a servo motor?

It’s a motor with a spinning end (shaft) that can be spun up to certain angles with high precision. A servo motor is a rotary machine that converts electrical energy into mechanical energy. This mechanical energy is basically the movement of the output shaft. Servomotors are also known as rotary actuators or linear actuators.

Servo motors can be positioned to a specific angle by applying a control signal.  As long as the control signal exists, the angular position of the shaft remains the same. The level of control in an industrial servomotor is, ofcourse, very different from a little DC servomotor.

Do not confuse it with a stepper motor. Its difference from the stepper motor lies in the fact that the servo keeps a measurement of its angle while it works. Here’s an in-depth discussion on the difference between servomotors and stepper motors.

Servo motor

What are the different parts of a servo motor?

A servo motor mainly consists of four parts:

  • An electric motor: The thing that converts electrical energy to mechanical energy. It is a rotary machine.
  • A control system: It controls the angle of rotation of the axis by applying a control signal.
  • A drive system: It contains gears that can increase or decrease the speed and torque.
  • A potentiometer: It is connected to the central shaft, and helps the control system to monitor the angle in which the motor’s shaft is positioned.

Here’s another image for your reference.

Working of the Arduino servo motor

servo motor

The servo motor consists of a control circuit and a potentiometer that is connected with the shaft. The potentiometer gives access to the control circuit to control the motor and thereby control the current angle of the shaft.

When the shaft is at the desired angle, the motor will switch off. If the current angle is not the required angle, then the motor turns the shaft until it reaches the correct angle. The maximum range of the shaft is 180 degrees. That means that the angle that the shaft turns is at most 180 degrees. The output gear has a mechanical stop, so it is not possible to turn the shaft above 180 degrees.

The distance to be traveled is proportional to the applied power. It uses proportional control.

Proportional control: The motor will run at maximum speed when the shaft needs to turn a considerable distance. Conversely, it will run at minimum speed when the shaft needs to turn a small distance.

How to control the angle of a servo motor?

pulse width modulation in servo motor

You need to use pulse width modulation to control the angle of rotation. We have a complete guide on how to use PWM with an Arduino Uno. The control is done by giving a pulse of a particular duration to the control wire.  There are three types of pulses that a servo recognizes.

A minimum pulse is a pulse needed for the servo to stay at 0 degrees of rotation. A neutral pulse (around 1.5ms) is required to get the shaft in a neutral position. A neutral position is the one from where it can potentially turn equal distances in either direction. A maximum pulse of about 2ms turns the servo to 180 degrees.

The refresh rate of the servo is about 20ms. So if you wish to hold the position, send the same pulse 20ms apart. The motor is going to check after 20ms. If the pulse sent is different from the previous one, the motor will change position accordingly.

How to select a suitable Servo Motor for an Arduino project?

There are two types of servo motors:

  1. Standard/Limited motion servo motor: This servo motor usually rotates up to a maximum of 180 degrees. Hence it finds applications in projects like robotic arms, wing flap control of an airplane, etc.
  2. Continuous motion servo motor: This servo motor rotates continuously. Instead of using the PWM signals for controlling the angle of motion, it uses them to control speed and torque.

We will concern ourselves with a standard servo motor for this article.

The input voltage range for most of the hobby servo motors are ranging from 4.8v to 6.5 v, but the average voltage required is 5V. Voltage is proportional to torque, thus, higher the voltage higher the torque. Almost all servo motors used for projects rotate from 0 degrees to 180 degrees that is a half-circle.

The torque is an essential parameter of the servo motor. The servo motors we linked to in the components required section should suffice for most general applications. They provide a torque of 2.5kg/cm. Meaning, they can lift a weight of 2.5kg when it lies at a distance of 1 cm. Likewise, when we place the substance at 1/2cm, it can pull a load of 5kg and so on.

It is necessary to choose a suitable servo based on the need. For some particular purposes, you might need slow, gradual motion. Or swift and precise for others. Make sure you choose accordingly. The interfacing techniques shall, for most purposes, remain uniform.

Pins of a servo motor

Pin

Color

 Description

1 Brown You can connect the Ground wire connected with the ground pin of the Arduino
2 Red Voltage supply to the motor normally +5V is used
3 Orange PWM signal is given to the Arduino servo via this wire to drive the motor

Features of servo motor

  • Operating Voltage: +5V
  • Torque: 2.5kg/cm
  • Operating speed is 0.1s/60°
  • Gear Type: Plastic
  • Rotation : 0°-180°
  • Weight of motor: 9gm
  • Package contains gear horns and screws

Physical dimensions of the servo motor

There are many types of servo motors. To check the precise dimensions for a servo motor, you need to check with the supplier for a datasheet. However, we have enlisted the approximate sizes for the different classes of servo motors based on their size.

Servo Size

Weight

 Servo Width

Servo Length

Nano <8g 7.5mm 18.5mm
Sub-Micro 8g – 16g 11.5mm 24mm
Micro 17g – 26g 13mm 29mm
Mini 27g – 39g 17mm 32.5mm
Standard 40g – 79g 20mm 38mm
Large 80g & Larger > 20mm > 38mm

Understanding the ULN2003

ULN2003 is a relay driver IC. As you might know, our Arduino Uno and many other microcontrollers/microprocessors do not have large output powers. Basically, we cannot expect an Arduino to drive loads that require a large voltage or current. The general outputs of the Arduino are sufficient to drive basic sensors and the likes. So what do we do when we wish to drive a high power item using an Arduino? That’s where the ULN2003 IC steps in.

The ULN2003IC contains seven Darlington pairs. Darlington pairs are just transistors that are configured to give high output with high levels of currents after the input of a small current. That sounds perfect for our job, doesn’t it?

Hence, a ULN2003 can control seven different relays at a time. It can also drive motors, which, in our case, is quite handy.

 

Where to use a ULN2003?

ULN2003 IC is the most commonly used motor driver IC. It is used when we need to switch a large number of motors or any other devices simultaneously. It can give the constant voltage and current supply to the motor.

Features of ULN2003

  • Collector current: 500mA
  • High output voltage: 50V
  • Useful in many relay driver applications.
  • It consists of output clamp diodes.
  • ULN2003 is compatible with inputs of popular logic types like TTL, etc.

Pins of ULN2003

Pin Number

 Name

Description

1 to 7 Input 1 to Input 7 These seven Input pins are connected to the base of the transistor and +5V is enough to trigger them
8 Ground We can connect the ground pin that with the Arduino ground pin.
9 COM It is the test pin or Voltage suppresser pin
10 to 16 Output 1 to Output 7 These pins are the respective outputs of seven input pins.

Scope of applications of the ULN 2003

It has a wide range of applications. Some of them listed below,

  • Logic buffers.
  • Line drivers.
  • Relay drivers (for driving different loads)
  • Lamp drivers.
  • LED display drivers (display devices)
  • Motor (stepper and DC brushed motor) drivers

Circuit diagram to interface servo motor with Arduino Uno using ULN2003

Working of the circuit

  1. You can connect the Ao analog pin with a center pin of a 10k potentiometer.
  2. Connect the voltage pin of the potentiometer with the Arduino 5volt Vcc pin.
  3. Likewise, you can connect the ground pin of the potentiometer with the Arduino ground pin.
  4. Give 5V voltage supply to the ULN2003 IC via the 9th pin.
  5. Connect the ground pin of the servo motor with the 10th pin of IC.
  6. Give 5v voltage supply to the servo motor via the voltage pin.
  7. Connect the PWM signal pin of the servo motor with the 9the pin of Arduino.

The potentiometer gives the analog input to the Arduino that is stored in a variable to which it is mapped. Using this mapping, we can control the servo meter by adjusting the voltage of the potentiometer. Thus the Arduino computes the input and sends the corresponding PWM signal to the servo motor. The ULN2003 is usually used to control more than one servo meters at a time.

Circuit diagram to interface single servo motor with Arduino Uno without the ULN2003

Following are the steps to connect a servo motor to the Arduino without ULN2003

  1. The servo motor has a female connector with three pins. The black-colored pin is usually a ground pin. You can connect this to the Arduino GND pin.
  2. Connect the red pin with the  5V Vcc pin on the Arduino.
  3. Furthermore, you can connect the remaining pin on the servo connector to a digital pin on the Arduino that is the PWM signal pin.

Code for controlling a single DC motor with an Arduino Uno using ULN2003 and a potentiometer

Arduino program

The Arduino program is usually done in the Arduino IDE.  You can learn all about installing and getting acquainted with the Arduino IDE here.

As we saw in our How to program an Arduino guide, the Arduino code contains two functions as void setup() and void loop(). In void setup(), we can make all pin mode declarations. And in void loop(), we write the functions and the computation process, and it will run infinitely. These programs are usually done with embedded C. Servo interfacing requires the  Servo.h header file, which is a special header file that provides different functions pertaining to the servo motor. You can learn how to install a library in the Arduino IDE here. Some of the functions used in this program are:

  1. Servo myservo
    • It creates an object named myservo of the class Servo.
  2. myservo.attach(pin)
    • This function connects the servo variable to a pin.
    • The argument is the servo pin.
  3.  myservo.write(angle)
    • This function assigns a value to the servo that controls the position of the shaft.
    • Angle can take values between 0 to 180.
  4. map(value, fromLow, fromHigh, toLow, to high)
    • We can use this function to map a number from one range to another range.
    • This means that “value” has a value between the values, “fromLow” to “fromHigh” converts the value to their equivalent values in the range of “toLow” to “toHigh”“fromLow” gets mapped to “toLow”.

Code

/* Controlling a servo position using a potentiometer  */

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer

int val; // variable to read the value from the analog pin



void setup() {

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}



void loop() {

val = analogRead(potpin);

// reads the potentiometer input (value between 0 and 1023)

val = map(val, 0, 1023, 0, 180);

// scale it to use it with the servo (value between 0 and 180)

myservo.write(val); // sets the servo position according to the scaled value

delay(15);

}

Code for controlling a single DC motor with an Arduino Uno without a ULN2003 and a potentiometer

#include <Servo.h> 

int servoPin = 9;

Servo servo;  

int angle = 0;   // servo position in degrees 

void setup() 
{ 
  servo.attach(servoPin); 
} 

void loop() 
{ 
  for(angle = 0; angle < 180; angle++)  
  {                                  
    servo.write(angle);               
    delay(15);                   
  } 

  for(angle = 180; angle > 0; angle--)    
  {                                
    servo.write(angle);           
    delay(15);       
  } 

}

Common errors and troubleshooting

If you are powering up the Arduino Uno using a USB and sourcing the servo’s 5V supply from the on-board Vout, then you might face some errors. The power coming from the USB source can, in some cases, be insufficient, causing the servo to reset. In this case, add a 470uF electrolytic capacitor between the GND and 5V pins of the Arduino.

Servo motors have three pins- power pin, ground pin, and a signal pin. The power pin is red, and you can connect it with 5V Vcc pin on the Arduino. The ground pin is typically black or brown and connected with one terminal of ULN2003 IC (10 -16).

To protect your Arduino board from damage, you will need some driver IC to perform this function. Here we have used ULN2003 IC to drive the servo motor. The PWM signal pin is yellow or orange, and you can connect it with the D9 pin on the Arduino.

Applications of servo motor

We can use servo motors  in various applications such as

  • Robotics
  • CNC machinery or automated manufacturing
  • Radio-controlled airplanes
  • Elevators and Rudders.
  • A camera’s zooming mechanism also uses servo motors.
  • Lift doors
  • Radio-controlled cars
  • Animatronics.

9 thoughts on “Interfacing of Arduino with servo motor – The definitive guide

  1. Dear,
    More informative for beginners. I suggest u use tinkercad simulator, it makes more perfect.if u have plan to conduct online training in lockdown period I wanna join with u.

  2. i have not posted any comment before. Would like to but site says I have already posted. This is first time I see this excellent article .wolud like to download pdf.

    1. I guess that’s because your first comment was still in the pipeline. No worries, I have replied to your earlier comment.

  3. Is this available in pdf format for download?
    Excellent write up.Keep it up.
    Could you send it by mail?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.