Servo Motor with Arduino

In this section, you learn interfacing of servo motor with Arduino, but before this let’s start with servo motor.

A servo motor is an electrical device used to rotate objects at certain specified angles. It’s just a basic motor that runs through the mechanism of the servo.

They are used in numerous applications such as toy cars, robotics, machines etc.

Servo motors are measured in kg / cm (kilogram per centimeter). Servo motors are rated 3kg / cm or 6kg / cm etc. This kg / cm tell you how much weight your servo engine can lift at a given distance. Generally servo motor 9G SG90 is used in projects.

Servo Motor

For example – If the load is suspended 1 cm away from the shaft of the motors, a 3kg / cm Servo motor will be able to lift 3 kg, the greater the distance the less the ability to carry weight. Electrical pulse determines the direction of a servo motor.

COMPONENTS REQUIRED

S.N. COMPONENT QUANTITY
1. Arduino Uno 1
2. Servo Motor SG90 1
3. Jumper wires 3

SERVO MOTOR PINOUT

Servo Motor pinout

WIRE CONFIGURATION

Wire Number Wire Colour Description
1. Brown Ground wire connected to the ground of system
2. Red Powers the motor typically +5V is used
3. Orange PWM signal is given in through this wire to drive the motor

SERVO MOTOR SG90 TECHNICAL FEATURES

    • Operating Voltage is +5V
    • Torque: 2.5kg/cm
    • Operating speed is 0.1s/60°
    • Gear Type: Plastic
    • Rotation : 0°-180°
    • Weight of motor : 9gm
    • Package includes gear horns and screws

HOW SERVO MOTOR WORKS

Servo motors have 3 wires coming out of them. Out of which 2 are used as Power pins (positive and negative) and the third one is used for the signal that is to be sent from the MCU.

All Servo motors operate on the principle of PWM (Pulse Width Modulation), meaning their angle of rotation is controlled by the length of the pulse applied to their Control PIN. Essentially, the servo motor consists of a DC motor powered by a variable resistor (potentiometer – POT) and some gears.

PWM signals of different duty cycles
PWM signals of different duty cycles

Servo motor SG90 can be rotated from 0 to 180 degree, but other servo motors can rotate completely 360 degree depending upon manufacturer. By adding the correct width of the Electrical Pulse to its control pin, this degree of rotation can be controlled.

CIRCUIT DIAGRAM – SERVO MOTOR ARDUINO

To interface servo motor with Arduino make connections like this. PWM supported pins in Arduino Uno are 3, 5, 6, 9, 10, and 11 (~ tilde sign before pin).

Servo motor with Arduino

CONNECTION TABLE

S.N. Arduino Servo Motor
1. +5V VCC (red)
2. GND GND (brown)
3. 9 Signal (orange)

SERVO MOTOR ARDUINO CODE

/*Tutorial: https://pijaeducation.com/servo-motor-with-arduino/
*/
// PWM supported pins in Arduino Uno are 3, 5, 6, 9, 10, and 11.

#define del 15
// include servo library, don't need to install externally already available in arduino IDE
#include <Servo.h>
// create servo object to control a servo
Servo myservo;

void setup() {
  /* Attaches servo variable myservo to pin 9.
    Using myservo you can use servo library inbuilt function, which control servo connected on pin 9
  */
  myservo.attach(9);
  Serial.begin(9600);
}

void loop() {
  // rotate servo motor from 0 to 180 degree
  for (int i = 0 ; i <= 180 ; i++) {
    // set servo motor angle to value i
    myservo.write(i);
    // prints i value to serial monitor
    Serial.println(i);
    delay(del);
  }

  // rotate servo motor from 180 to 0 degree
  for (int i = 180 ; i >= 0 ; i--) {
    myservo.write(i);
    Serial.println(i);
    delay(del);
  }

  // Earlier we attached servo to pin 9, here we can detach it if needed. Not compulsory to do here.
  // myservo.detach();
}

CONNECT MORE SERVO MOTORS

/*Tutorial: https://pijaeducation.com/servo-motor-with-arduino/
*/
// If you want to connect more servo motor then you need to create more servo variables like

#include <Servo.h>
Servo myservo1;
Servo myservo2;

void setup() {
  myservo1.attach(9);
  myservo2.attach(10);
  Serial.begin(9600);
}

OUTPUT

Motor will start rotation from 0 to 180 degree, but these motors are not so accurate, so there are chances the motor can move 15-20 degree less.

Servo motor with Arduino

4.5 6 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x