Open In App

Servo motor Interfacing and Control using Arduino

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to interface and control servo motors using Arduino Uno R3.

Arduino is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontroller in the Arduino. It provides Arduino IDE to write code & connect the hardware devices like Arduino boards & sensors.

Servo motor:

It is an electric device that is used to control angular rotation. It is a rotary actuator or linear actuator. Servo motors have servomechanism. The servo mechanism helps the servo motor to control and monitor the motion of the motor. The user can control the rotation speed and position (angle) precisely.

Servo motor pins:

A servo motor has 3 pins. 

  1. VCC ( 5 Volts )
  2. GND
  3. Signal ( Control input signal )

Components required:

  1. Servo motor
  2. Arduino Uno R3
  3. Jumper wires

Servo motor configuration:

  1. Connect the Signal pin of the Servo motor to the vacant PWM pin of the Arduino.
  2. Connect the VCC of the servo motor to the 5V pin.
  3. Connect the GND of the servo motor to the GND.
  4. Import the header file to the Arduino code.
  5. Compile and upload the code to the Arduino Uno R3.

Circuit Diagram:

Servo Motor Circuit diagram

Arduino code:

C++




#include <Servo.h>
  
Servo servo1;  // Create object for Servo motor 1
Servo servo2;  // Create object for Servo motor 2
  
int position = 0;     // Variable to store the position
  
void setup()
{
  servo1.attach(3);   // Set PWM pin 3 for Servo motor 1
  servo2.attach(5);   // Set PWM pin 5 for Servo motor 2
}
void loop()
{
  // Rotating Servo motor 1 in Anti clockwise from 0 degree to 180 degree
  for (position = 0; position <= 180; position++) 
  
    servo1.write(position);  // Set position of Servo motor 1
    delay(10);                     
  }
    
  // Rotating Servo motor 1 in clockwise from 180 degree to 0 degree
  for (position = 180; position >= 0; position--) 
  
    servo1.write(position);  // Set position of Servo motor 1
    delay(15);               // Short delay to control the speed
  }
    
  // Rotating Servo motor 2 in clockwise from 0 degree to 180 degree
  for (position = 0; position <= 180; position++) 
  
    servo2.write(position);  // Set position of Servo motor 2
    delay(10);               // Short delay to control the speed      
  }
    
  // Rotating Servo motor 2 in Anti clockwise from 180 degree to 0 degree
  for (position = 180; position >= 0; position--) 
  
    servo2.write(position);  // Set position of Servo motor 2
    delay(15);               // Short delay to control the speed
  }
    
}


Note: The direction of rotation may be varied depends on the model of the servo motor.

Output:

Online Simulation: Click here

Servo motor simulation

Applications:

  1. Robotics arms
  2. Manufacturing automation


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads