Tuesday, November 10, 2015

Using Servo Motor with Arduino

if you dont know how to simulate Arduino in Proteus Simulator click here to know or visit my previous post about using arduino in proteus. 

Arduino is an open source development board which is very good research and learning platform for technical and non technical person. It is very easy and comfortable to use in most of the situation from school project to the professional job. So it gives us knowledge about electronics and help develop idea of all age students. Learning arduino is awesome with enough hardware but sometimes we can not have all the component and arduino to research and design a project due to money and many thing so there is a solution of this problem.. We can design and code and test the system even if we dont have enough hardware. Yes we can solve it with computer having arduino IDE and proteus simulator.

WANT TO LEARN ABOUT ARDUINO AND ARDUINO BASED PROJECT,CLICK HERE..
So i tried here to present an idea about the same thing that how can we use arduino in proteus to design and code system.click here


The following tutorial helps you use servo motor with Arduino.

Servo motor and its application

Servo motor is an electronics motor which can only rotate up to 180 degree only.
So the main difference between Servo motor and ordinary motor is that ordinary motor can rotate around 360 degree but the Servo cannot. 
Servo has three wires and they are positive power supply, ground and pulse. In our project pulse is connected to 9 number pin of digital i/o line of arduino. 
Servo motor is used in embedded system for mechanical motion and robotics. Some of the common examples are robot, robotic hands, door opening and closing system, camera movements for searching robot etc. 

Circuit diagram 


Connect the devices as shown above. 

  1. red or positive wire is connected to 5v of the arduino.
  2. black or ground wire is connected to the gnd of the arduino,
  3. another wire called pulse is connected to pin number 9 of the arduino. 

Code of project

Now write the following code in your arduino IDE or copy the code and paste it on your IDE. 


#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() 
{
  myservo.attach(9); 
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) 
  {
    myservo.write(pos);              
    delay(15);                       
  }
  for (pos = 180; pos >= 0; pos -= 1)
  {
    myservo.write(pos);              
    delay(15);                      
  }
}

Code explanation and output explanation

The following code is explained line by line. 
  • The first line   #include <Servo.h>    includes the library file named as servo.h  .
  • Servo myservo;  this line creates the servo object called myservo  .
  • myservo.attach(9);    this line tells that servo pulse pin is attached to pin no 9 of arduino. 
  •    for (pos = 0; pos <= 180; pos += 1) 
      {
        myservo.write(pos);              
        delay(15);                       
      }        the above lines are used to rotate servo from 0 degree to 180 degree. 
  • for (pos = 180; pos >= 0; pos -= 1)
      {
        myservo.write(pos);              
        delay(15);                      
      }         the above lines are used to rotate servo from 180 degree to 0 degree that is in opposite direction. 

Servo motor manual Control using potentiometer

  • Connect a potentiometer as shown in figure below
                           Connct one of the pin to vcc, another end to gnd and middle or separate pin to A0                                (analog in) of arduino

  • upload the following code to your arduino 
  • turn your pot around 
  • then you will get the result in which your servo will turn whenever you turn your pot in both direction. 
#include <Servo.h>

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

int potpin =A0;                            // 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 value of the potentiometer (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);                                       // waits for the servo to get there
}



if you dont know how to simulate Arduino in Proteus Simulator click here to know or visit my previous post about using arduino in proteus. 

No comments:

Post a Comment