Home Industry InsightServo
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

Mastering the MG995 Servo Motor with Arduino: A Comprehensive Guide

小编

Published2025-09-16

Introduction to the MG995 Servo Motor

The MG995 servo motor is a popular choice among hobbyists and robotics enthusiasts for its affordability, torque, and reliability. Whether you’re building a robotic arm, a surveillance camera mount, or an automated pet feeder, this servo motor can handle a wide range of applications. In this guide, we’ll dive into how to interface the MG995 with an Arduino Uno, write efficient code, and bring your projects to life.

Why Choose the MG995 Servo Motor?

The MG995 is a high-torque servo motor capable of rotating up to 180 degrees (or 360 degrees in continuous rotation mode). With a stall torque of 10 kg/cm, it’s powerful enough for heavy-duty tasks. Its metal gears ensure durability, while its PWM (Pulse Width Modulation) control makes it compatible with microcontrollers like Arduino.

Components You’ll Need

Arduino Uno or Nano MG995 servo motor Jumper wires Breadboard (optional) External 5V power supply (recommended for high-load applications)

Wiring the MG995 to Arduino

The MG995 has three wires:

Brown (Ground): Connect to Arduino’s GND pin. Red (VCC): Connect to Arduino’s 5V pin or an external power supply. Orange (Signal): Connect to a PWM-enabled digital pin (e.g., Pin 9).

Important: For high-torque applications, use an external power supply to avoid overloading the Arduino’s built-in voltage regulator. Connect the external power’s ground to Arduino’s GND to ensure a common reference.

Basic Arduino Code for MG995

Let’s start with a simple program to rotate the servo from 0 to 180 degrees and back.

```cpp

include

Servo myServo; int servoPin = 9;

void setup() { myServo.attach(servoPin); }

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

Code Explanation: 1. The `Servo.h` library simplifies servo control. 2. `myServo.attach(servoPin)` initializes the servo on Pin 9. 3. The `loop()` function sweeps the servo between 0° and 180° using `myServo.write(pos)`. Upload this code, and your servo should move smoothly! #### Calibration Tips If the servo doesn’t sweep accurately, adjust the pulse width limits using `myServo.attach(servoPin, 500, 2500)`. The default pulse range is 544–2400 microseconds, but the MG995 may respond better to 500–2500. #### Common Issues in Part 1 - Jittery Movement: This often occurs due to power supply noise. Use a capacitor (100–470 µF) across the servo’s power lines. - Overheating: Avoid stalling the servo for extended periods. Stay tuned for Part 2, where we’ll explore advanced projects and troubleshooting! ### Advanced Projects with the MG995 Servo Motor Now that you’ve mastered the basics, let’s tackle more complex applications. #### Project 1: Robotic Arm Control Build a 2-axis robotic arm using two MG995 servos. Components: - 2x MG995 servos - Cardboard or 3D-printed arm segments - Potentiometers (for manual control) Wiring: - Connect each servo’s signal pin to separate PWM pins (e.g., Pins 9 and 10). - Connect potentiometers to analog pins A0 and A1. Code:

cpp

include

Servo servo1, servo2; int pot1 = A0, pot2 = A1;

void setup() { servo1.attach(9); servo2.attach(10); }

void loop() { int angle1 = map(analogRead(pot1), 0, 1023, 0, 180); int angle2 = map(analogRead(pot2), 0, 1023, 0, 180); servo1.write(angle1); servo2.write(angle2); delay(20); }

This code maps potentiometer values to servo angles, allowing real-time control. #### Project 2: Automated Pan-Tilt Mechanism Create a camera mount that follows motion or light. Components: - 2x MG995 servos - Ultrasonic sensor (HC-SR04) or light-dependent resistor (LDR) Code Snippet (Ultrasonic Version):

cpp

include

Servo panServo, tiltServo; int trigPin = 7, echoPin = 6;

void setup() { panServo.attach(9); tiltServo.attach(10); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }

void loop() { long duration = pulseIn(echoPin, HIGH); int distance = duration * 0.034 / 2; int panAngle = map(distance, 2, 200, 0, 180); panServo.write(panAngle); delay(50); }

arduino

include

Servo myservo; // Create a Servo object int pos = 0; // Variable to store the servo position

void setup() { myservo.attach(9); // Attaches the servo on pin 9 to the servo object }

void loop() { for (pos = 0; 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 } } ```

This code snippet demonstrates a simple sweep of the servo motor from 0 to 180 degrees and back. Let's break down the code:

#include: This line includes the Servo library, which provides the necessary functions for controlling servo motors. Servo myservo;: This line creates a Servo object named myservo. This object will be used to control the servo motor connected to the Arduino. int pos = 0;: This line declares an integer variable named pos and initializes it to 0. This variable will store the current position of the servo motor. myservo.attach(9);: This line attaches the servo object to digital pin 9 on the Arduino. This tells the Arduino that the servo motor is connected to this pin. myservo.write(pos);: This line tells the servo motor to move to the position specified by the pos variable. The write() function takes an integer argument representing the desired angle in degrees (0 to 180). delay(15);: This line introduces a delay of 15 milliseconds. This delay allows the servo motor enough time to reach the desired position before the next command is sent. Without this delay, the servo might not be able to keep up with the rapid changes in position.

This basic code provides a foundation for more complex servo control applications. You can modify the for loops to control the speed and range of the servo's motion.

${part2}

Advanced Servo Control Techniques

Beyond the basic sweeping motion, there are several advanced techniques you can use to control the MG995 servo motor with greater precision and flexibility.

Using map() to Convert Input Values: The map() function is a powerful tool for scaling input values from one range to another. For example, you can use it to map analog input from a potentiometer (0-1023) to the servo's angle range (0-180). This allows you to control the servo's position using a potentiometer.

#include 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 (values between 0-1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (values between 0-180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }

Controlling Servo Speed: While the delay() function can be used to slow down the servo's motion, it's not the most efficient approach. A more sophisticated technique involves incrementing the servo's position in smaller steps and introducing a short delay between each step. This allows for smoother and more controlled movements.

#include Servo myservo; int pos = 0; int targetPos = 90; // Example target position void setup() { myservo.attach(9); } void loop() { if (pos targetPos) { pos--; myservo.write(pos); delay(5); // Adjust delay for desired speed } }

Using Serial Communication: Serial communication allows you to control the servo motor from your computer using the Arduino Serial Monitor. This is useful for testing and debugging, as well as for creating interactive control interfaces.

```arduino

include

Servo myservo; int pos = 90; // Initial position

void setup() { myservo.attach(9); Serial.begin(9600); // Initialize serial communication Serial.println("Enter servo position (0-180):"); }

void loop()

Update:2025-09-16

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.