Contents

Interfacing MG995 55G Metal Gear Servo with Arduino

TowerPro MG995 55G Metal Gear Servo Features

Servo motor is a kind of motor that can rotate with high accuracy. If you want to rotate an object to a certain angle and distance, you can use servo motors. These motors can be commonly used in remote control toys, helicopters, RC planes, robots, etc. Servo motors are rated in Kg/cm. A motor with 1kg/cm torque is capable of holding a 1kg weight at a radial distance of 1cm. For example, 6Kg/cm means that this motor can move a 6 kg object that is 1cm away from its shaft.

The key features are:

  • Torque: 8.5Kgf.cm at 4.8V, 10Kgf.cm at 6V 
  • Speed: 0.2s/60º at 4.8V and 0.16s/60º at 6V
  • Operating voltage: 4.8 to 7.2V
  • Frequency: 50 Hz

You can download the datasheet of this module here.

TowerPro MG995 55G Metal Gear Servo Pinout

This Module has 3 pins:

  • PWM: Motor control signal – Orange
  • VCC: Motor power supply – 4.8V to 7.2V- Red
  • GND: Ground – Brown

You can see the pinout of this motor in the image below.

Required Material

Hardware component

Arduino UNO R3 × 1
TowerPro MG995 55G Metal Gear Servo × 4
16 Channel 12-bit PWM/Servo Driver × 1
Male to Female Jumper Wire × 1

Software Apps

Arduino IDE
Note

For this tutorial, you can prepare a desired number up to 16 servo motors.

In this tutorial, we have used 4 servo motors.

Interfacing TowerPro MG995 55G Metal Gear Servo with Arduino

Step 1: Circuit

The following circuit show how you should connect Arduino to MG995 motor. Connect wires accordingly.

Warning

Be careful not to use power supply greater than the motor voltage, since the operating voltage is 5-6V.

Step 2: Library

Go to Library manager and search for Servo and then install Adafruit PWM Servo Driver Library as shown below.

Tip

If you need more help with installing a library on Arduino, read this tutorial: How to Install an Arduino Library

Step 3: Code

Upload the following code to your Arduino.

   /*
  TowerPro-MG995-Servo-Motor
  modified on 21 Dec 2020
  by Amir Mohammad Shojaee @ Electropeak
  
Home
based on Arduino - Adafruit library */ #include <Wire.h> #include <Adafruit_PWMServoDriver.h> // called this way, it uses the default address 0x40 Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(); #define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096) #define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096) #define USMIN 600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150 #define USMAX 2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600 #define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates // our servo # counter uint8_t servonum = 0; void setup() { Serial.begin(9600); Serial.println("4 channel Servo test!"); pwm.begin(); pwm.setOscillatorFrequency(27000000); pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates delay(10); } // You can use this function if you'd like to set the pulse length in seconds // e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. It's not precise! void setServoPulse(uint8_t n, double pulse) { double pulselength; pulselength = 1000000; // 1,000,000 us per second pulselength /= SERVO_FREQ; // Analog servos run at ~60 Hz updates Serial.print(pulselength); Serial.println(" us per period"); pulselength /= 4096; // 12 bits of resolution Serial.print(pulselength); Serial.println(" us per bit"); pulse *= 1000000; // convert input seconds to us pulse /= pulselength; Serial.println(pulse); pwm.setPWM(n, 0, pulse); } void loop() { // Drive each servo one at a time using setPWM() Serial.println(servonum); for (uint16_t pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) { pwm.setPWM(servonum, 0, pulselen); } delay(500); for (uint16_t pulselen = SERVOMAX; pulselen > SERVOMIN; pulselen--) { pwm.setPWM(servonum, 0, pulselen); } delay(500); servonum++; if (servonum > 3) servonum = 0; // Testing the first 4 servo channels }

At the beginning of this code, two required libraries are included. The following is the data related to the servo motor. Then, in the main loop of code, each stepper motor rotates 180 degrees clockwise and 180 degrees counter-clockwise, respectively. Just be careful if you use more or less than 4 servo motors, change the number in the last line of code.

The output is as follows. When each servo motors rotate, its number appears on the Serial Monitor.

Liked What You See?​
Get Updates And Learn From The Best​

Leave a Reply

Your email address will not be published. Required fields are marked *