Controlling Servo With Arduino

The motor can rotate with great precision. If you want to rotate an object at some specific angles or distance, then you use a servo motor. It is just made up of a simple motor which runs through a servo mechanism. If the motor is powered by a DC power supply then it is called DC servo motor, and if it is AC-powered motor then it is called AC servo motor.
Servo motors are rated in kg/cm (kilogram per centimeter) most hobby servo motors are rated at 3kg/cm or 6kg/cm or 12kg/cm. This kg/cm tells you how much weight your servo motor can lift at a particular distance. For example: A 6kg/cm servo motor should be able to lift 6kg if the load is suspended 1cm away from the motor shaft, the greater the distance the lesser the weight carrying capacity. The position of a servo motor is decided by electrical pulse and its circuitry is placed beside the motor.

 

 

How Does A Servo Work?

The servo motor is capable of rotating at a specified angle. It allows for angular and linear positioning. They can be operated using 3.3v and 5v. The servo we will be using is MG90S. It consists of three wires, Brown wire (Ground), Red wire (Vcc) and orange wire (signal pin). It consists of a potentiometer, gear assembly and the controlling circuit. The gear assembly reduces RPM to increase the torque of the motor. The use of servo motors in our daily life is in blinds, robotics and door openings.
Servo motor

Controlling Servo with Arduino

The requirements are 

  1. Arduino UNO
  2. Servo Motor (SG90/MG90s)
  3. Pin to hole jumper wires

Schematics

Circuit diagram

How to Install Servo Library

Before uploading the code install the library for servo motor. Open Arduino IDE, go to tools, click on manage library. The library manager will pop up. Search for Servo library and install it. The figure below may help you. 

Servo library

How To Calibrate Servo to Angle Zero?

Horn positioning

Attach the horn to the gear at top of the servo in any direction. Upload the code below to bring it to angle zero and then reattach the horn in the position as shown in the figure above.

Code for Calibrating servo at angle zero

				
					#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


void setup() {
  myservo.attach(2);  // attaches the servo on GIO2 to the servo object
      myservo.write(0);
}

void loop() {
}
				
			

Rotating at Different angles

				
					#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


void setup() {
  myservo.attach(2);  // attaches the servo on GIO2 to the servo object
}

void loop() {
  int pos;

  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}
				
			

Conclusion

The servo will start its rotation from zero degree and will rotate till 180 degrees and then 180 degrees to zero degree. Change the angles in the for loop or add more for loops to your code and pass different angles. 

Leave a Reply

Your email address will not be published.