How to Interface Servo Motor with Arduino UNO

shape
shape
shape
shape
shape
shape
shape
shape
blog-details
June, 2021

INTRODUCTION

In this tutorial, we are going to learn how to interface a Servo Motor to an Arduino UNO. And we will also learn briefly about its working principles.

THEORY

Servo motor is a tiny and lightweight motor having high output power. It can able to rotate 180 degrees ( 90 degrees on each side). The angular position control is performed by the PWM technique.

It will operate from 4.8V to 6.5V, the higher the voltage higher the torque.

It having a torque of 2.5kg/cm and weight is 9gm.

 

 

The Servo Motor has 3 pins.

i)VCC

ii)GND

iii)Data Pin

 

 

 

REQUIREMENTS

 

Parts Quantity
Arduino UNO 1
A-B cable 1
Servo Motor(SG90) 1
Bread Board 1
Connecting Wires As per required

 

CIRCUIT DIAGRAM

 

 

PROGRAM

For programming this concept we need to understand some basic syntax of Arduino IDE. The first thing is we need to install Arduino. After that, we need to know how to use serial monitor.

First of all, we have to include the library of servo motor.

 

#include <Servo.h>

 

Then, we have to define a variable, which used as a reference for controlling our servo motor throughout the code.

 

Servo servo;

 

After that, go for the void setup() function.

Declare as the servo motor connected to 9 no pin of UNO, after which we will center the servo, turning it to zero degrees.

We also include the serial monitor for watching the position of the servo.

 

void setup()
{
  servo.attach(9); 
  servo.write(pos);
  Serial.begin(9600);
  Serial.println("Lets watch the position"); //Printing purposes
}

 

Now we are all set to move the servo in direction, but we have to define in which angle how much time it will rotate.

For that, we are moving towards the void loop function.

 

void loop() 
{
  // position from 0 to 180 degrees
  for(pos = 0; pos < 180; pos++)  
  {                                  
    servo.write(pos);
    Serial.println(pos); // it will print the position on serial monitor                     
    delay(15);                   
  } 
   // now back from 180 to 0 degrees
  for(pos = 180; pos > 0; pos--)    
  {                                
    servo.write(pos);  
    Serial.println(pos);     
    delay(15);       
  } 
}

 

Final Code

 

 

/*
 Tested by robogenesis. in
*/

#include <Servo.h>

Servo servo;
int pos = 0; // defined position of servo initially at zero

void setup()
{
  servo.attach(9); 
  servo.write(pos);
  Serial.begin(9600);
  Serial.println("Lets watch the position"); //Printing purposes
}

void loop() 
{
  // position from 0 to 180 degrees
  for(pos = 0; pos < 180; pos++)  
  {                                  
    servo.write(pos);
    Serial.println(pos); // it will print the position on serial monitor                     
    delay(15);                   
  } 
   // now back from 180 to 0 degrees
  for(pos = 180; pos > 0; pos--)    
  {                                
    servo.write(pos);  
    Serial.println(pos);     
    delay(15);       
  } 
}

 

Comments

client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client
client

Leave a Reply