Skip to content

Interfacing and controlling a servo motor with Arduino & How it works?

    There are so many types of motors, so If you need a motor that determines positioning precisely the servo motor is the best choice.

    In this tutorial, you will learn how to interface a servo motor with Arduino which is used in many robotics applications.

    In addition, you will learn everything you need to know about servo motors, how it works, the pinout, the datasheet, and of course the code at the end of this tutorial. 

    But before diving in, Let’s talk about it a little bit.

    What Is The Servo Motor?

    A servo motor is a precise type of motor that controls the movement into a specific position, this motor has a built-in motor driver, so it is work as a closed-loop system to control the speed and the direction in a very accurate way by using the negative feedback from this system.

    Servo Motor Internal Structure - ROBOSANS
    Servo Motor Internal Structure

    There is a DC motor Inside the servo motor, and the output shaft is connected the this DC motor through the gears, the output shaft is used to drive the servo horn which is also connected to a potentiometer, both the DC motor and the potentiometer are connected with a control unit

    How Does A Servo Motor Work - Servo Motor Closed Loop System - ROBOSANS
    Servo Motor Closed Loop System

    The control unit has an error amplifier which is used to compare the targeted motor position to their actual position from the feedback that came from the potentiometer.

    The control unit will adjust the motor position to the targeted position by responding to the error that comes from the potentiometer, this mechanism is called servomechanism.

    How Does A Servo Motor Work?

    The servo motor mainly uses an input control signal to adjust the shaft of the motor. Servo motors are used in industrial and hobbyist applications, industrial servo motors are usually AC motors but we won’t talk about them today.

    The Hobbyist servo motors are primarily DC motors that can be controlled by analog and digital signals, and there are 2 types of them that are commonly used with Arduino applications, which we will talk about them in detail:

    • Conventional Servo Motor (SG90 as an example)
    • Continuous Rotation Servo Motor (FS90R as an example) 

    Conventional Servo Motor (SG90)

    The conventional servo motor used a PWM signal at (20ms) to control the motor, this signal should be (50Hz).  

    Conventional Servo Motor Working - Timing Diagram - ROBOSANS
    Conventional Servo Motor Working – Timing Diagram

    The position (angle) of the servo motor will be determined by the width of the pulse which is between (1-2ms) for the SG90 module which we will use in this tutorial, but always take a look at the specification of your modules, maybe the values it will be different.

    These cases will show you the relationship between the position (angle) and the pulse width:

    • If the pulse width is 1.5ms, the servo motor will be rotated to (90 degrees)
    • If the pulse width is 1ms or less, the servo motor will be rotated to (0 degrees)
    • If the pulse width is 2ms or more, less, the servo motor will be rotated to (180 degrees)

    You can control the motor position at any angle you want between (0-180 degrees)  by adjusting the pulse width which is between (1-2ms).

    Continuous Rotation Servo Motor (FS90R) 

    The continuous rotation servo motor used the same PWM signal that was used in the conventional but the motor will be worked in a different way.

    Continuous Rotation Servo Motor Working - Timing Diagram - ROBOSANS
    Continuous Rotation Servo Motor Working – Timing Diagram

    This motor rotates continuously, so the pulse width will be used to control the speed and the direction rotation of the motor which is between (1-2ms) for the FS90R module, and you can also take a look at the specification of your modules, maybe the values it will be different.

    These cases will show you the relationship between the speed and the rotation direction with the pulse width:

    • If the pulse width is 1.5ms, the servo motor will stop rotating.
    • If the pulse width is 1ms or less, the servo motor will be rotated at its full speed counter-clockwise (CCW).
    • If the pulse width is 2ms or more, less, the servo motor will be rotated at its full speed clockwise (CW).  

    In the (1-1.5ms) period the motor will be rotated counter-clockwise (CCW) and the shorter the pulse width the faster the motor rotation will be.  

    In the (1.5-2ms) period the motor will be rotated clockwise (CW) and the longer the pulse width the faster the motor rotation will be.  

    Servo Motor Pinout

    Let’s take a look at the servo motor pinout in this picture down below which have 3 pins.

    Servo Motor Pinout - ROBOSANS
    Servo Motor Pinout

    GND: This pin is common ground for the motor and the Arduino.

    5V: This pin is 5v which power the servo motor.

    Control: This pin is used as an input to control the servo motor.

    Servo Motor Datasheet

    Do you want to learn more technical details, you can read the servo motor datasheet.

    SG90 Servo Motor Datasheet

    The file down below is the SG90 servo motor datasheet you can download it.

    Servo Motor With Arduino

    Now Let’s connect a servo motor with Arduino but before that, you need to know that we have two connections today for two examples built-in with Arduino IDE, One of them requires a potentiometer, and the other doesn’t, and the codes are different:

    • Interfacing servo motor with Arduino – for Sweep examples.
    • Interfacing servo motor with Arduino with a potentiometer – for Knob examples.

    Parts Requirement:

    • Arduino Uno
    • Servo motor (SG90)
    • breadboard
    • Hook-up wires
    • 10k ohm potentiometer

    Interfacing Servo Motor With Arduino

    To interface the servo motor with Arduino we need to connect the power supply (5V) with the (5V) pin in the servo motor, then connect the (GND) for the power supply and servo motor with the common (GND) in the Arduino, and finally, we need to connect the control pin for servo motor with a digital pin number (9) in the Arduino.

    This connection will use it with (Sweep Example) with their code.

    You can take a look at this picture down below to see the connection in detail.

    Interfacing and controlling a servo motor with Arduino - ROBOSANS
    Interfacing and controlling a servo motor with Arduino

    Interfacing Servo Motor With Arduino With Potentiometer

    To interface the servo motor with Arduino with potentiometer we need to connect the power supply (5V) with the (5V) pin in the servo motor, then connect the (GND) for the power supply and servo motor with the common (GND) in the Arduino, also connect the control pin for servo motor with a digital pin number (9) in the Arduino, and finally, we need to connect the first and third pin in the potentiometer with the GND and 5V in the Arduino, and the middle pin in the potentiometer to the A0 analog input pin in the Arduino. 

    This connection will use it with (Knob Example) with their code.

    You can take a look at this picture down below to see the connection in detail.

    Interfacing Servo Motor With Arduino With Potentiometer
    Interfacing Servo Motor With Arduino With Potentiometer

    Servo Motor Fritzing

    This is the servo motor fritzing file which includes interfacing the servo motor with Arduino with and without a potentiometer so you can download it.

    Servo Motor Arduino Code

    Finally, Let’s write the servo motor Arduino code, we will use one of the two examples that are built-in with Arduino IDE depending on whether interfacing the servo motor with Arduino is with a potentiometer or without it.

    Servo Motor Arduino Code – Sweep

    Before writing the code you need to choose the (Sweep Example) from the Arduino IDE, by clicking File->Examples->Servo->Sweep.

    Then write the code down below or copy it directly, we will use it with the first connection we talked about it in this tutorial.

    This code will make the servo motor rotate from (0-180 degrees) and then from (180-0 degrees) continuously.

    #include <Servo.h>
    
    Servo servo_motor; // make a servo motor as an object to control it
    #define SERVO_PIN 8 // connect the control servo motor pin
    int pos_servo = 0; //make a variable to save the position of the servo motor as a degrees
    void setup() {
    
      servo_motor.attach(SERVO_PIN);
    
    }
    
    //This loop will make the servo motor rotate form 0-180 degrees then from 180-0 degrees continuously
    
    void loop() {
    
      // make the servo motor rotate form 0-180 degrees
      for (pos_servo = 0; pos_servo <= 180; pos_servo += 1) {
        servo_motor.write(pos_servo);              
        delay(15);                      
      }
    
      // make the servo motor rotate form 180-0 degrees
      for (pos_servo = 180; pos_servo >= 0; pos_servo -= 1) {
        servo_motor.write(pos_servo);            
        delay(15);                      
      }
    }

    Servo Motor Arduino Code – Knob

    You need to choose the (Knob Example) from the Arduino IDE, by clicking File->Examples->Servo->Knob.

    Then write the code down below or copy it directly, we will use it with the second connection we talked about it in this tutorial.

    This code will make you able to control the servo motor positioning 

    directly from the potentiometer at any angle you want from 0 to 180 degrees

    #include <Servo.h>
    
    Servo servo_motor; // make a servo motor as an object to control it
    #define POT_PIN 0  // connect the potentiometer with the analog pin
    #define SERVO_PIN 8 // connect the control servo motor pin
    
    int var;   // make a variable to read the analog pin value
    
    void setup() {
    
      servo_motor.attach(SERVO_PIN);  
    
    }
    
    // this loop will make the potentiometer control the angle of the motor
    
    void loop() {
    
      var = analogRead(POT_PIN);            
      var = map(var, 0, 1023, 0, 180);    
      servo_motor.write(var);                  
      delay(15);                          
    
    }

    Frequently Asked Questions About Servo Motor

    What Is A Servo Motor Used For?

    Servo motors are used in many industrial and hobbyist applications that need precise positioning: robotics, rudders, in-line manufacturing, the aerospace industry, and much more.

    Is A Servo Motor AC Or DC?

    There are two types of servo motors AC and DC, and each of these types is used in different applications. industrial servo motors are usually AC motors, and hobbyist servo motors are primarily DC motors.

    How Much Voltage Does A Servo Motor Need?

    The answer is it depends on your project, but generally speaking, the 5V is enough for most hobbyist projects.

    Can Servo Motors Run Continuously?

    Not all of them, most of the conventional servo motors like the (SG90) module are able to rotate just for 180 or 270-degree range, but there are continuous rotation servos like the (FS90R) module which can rotate continuously.

    How Servo Motor Is Connected To Arduino?

    To connect the servo motor with Arduino without a potentiometer we need to connect the power supply (5V) with the (5V) pin in the servo motor, then connect the (GND) for the power supply and servo motor with the common (GND) in the Arduino, also connect the control pin for servo motor with a digital pin number (9) in the Arduino. And with a potentiometer, it is like the previous connection but we need to connect the first and third pins in the potentiometer with the GND and 5V in the Arduino, and the middle pin in the potentiometer to the A0 analog input pin in the Arduino. 

    At the end of this tutorial, we need to thank you, we hope you find everything you need about interfacing servo motor with Arduino.

    ROBOSANS is the best place to start your learning journey with a brief Arduino tutorial and to the point.

     do you want to learn more? read these articles down below.

    How to control 28BYJ-48 stepper motor with Arduino by ULN2003 Driver

    Interfacing L298N Motor Driver With Arduino & DC Motor

    Sources:

    arduino.cc

    wikipedia.org