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

How to Control a Servo Motor with Arduino: A Step-by-Step Guide

小编

Published2025-10-15

Understanding Servo Motors and Arduino Basics

Introduction to Servo Motors

Servo motors are essential components in the world of robotics and automation. They are widely used in applications where precise control of angular position, velocity, and acceleration is required. From steering mechanisms in robotic vehicles to adjustable camera mounts, servo motors enable a high degree of precision, making them invaluable in various DIY electronics projects.

A servo motor consists of a small motor connected to a feedback device, often a potentiometer. The motor’s movement is regulated by this feedback mechanism, enabling the motor to achieve a specific position. Unlike standard DC motors, which rotate continuously, servo motors can rotate to a specific angle (usually between 0° and 180°) and maintain that position with high accuracy.

Why Use Arduino for Servo Motor Control?

Arduino, an open-source electronics platform, is a favorite among hobbyists, makers, and engineers for its simplicity and versatility. Arduino boards are easy to program, connect, and interact with, making them perfect for controlling a servo motor. Using an Arduino to control a servo motor opens up endless possibilities for creative projects, whether it’s for a robot, automation system, or custom electronics application.

The power of Arduino lies in its simplicity: you can control complex devices like servo motors with minimal coding and wiring. Thanks to the built-in Servo library, controlling a servo motor becomes an accessible and enjoyable task for beginners and experts alike.

Components Required for Controlling a Servo Motor with Arduino

Before diving into the wiring and coding, let’s first take a look at the essential components you’ll need for this project:

Arduino board (Uno, Nano, or Mega): The heart of the project, which will control the servo motor.

Servo motor: The component you’ll be controlling. Popular models include the SG90 or MG995.

Jumper wires: To make the necessary connections between the Arduino and the servo motor.

Breadboard (optional): If you prefer to keep things tidy and organized.

External power source (optional): Some servo motors require more current than the Arduino can supply, so an external power supply may be needed.

Wiring the Servo Motor to Arduino

To get started with controlling a servo motor, you need to wire the motor to the Arduino board. The wiring is straightforward and involves connecting three pins from the servo motor to the Arduino. Here’s how you do it:

Connect the servo's power pin (usually red) to the 5V pin on the Arduino: This will provide the servo with the necessary power to function.

Connect the servo's ground pin (usually black or brown) to the GND pin on the Arduino: This completes the circuit and ensures the servo has a proper ground connection.

Connect the servo’s signal pin (usually yellow or white) to one of the PWM-enabled pins on the Arduino: On the Arduino Uno, pins 9, 10, and 11 are capable of producing PWM signals that the servo motor uses to control its position. We’ll use pin 9 for this example.

Once you’ve completed the wiring, your servo motor is ready to be controlled using the Arduino. Let’s move on to the coding part.

Programming the Arduino to Control the Servo Motor

Introduction to the Arduino IDE and Servo Library

The Arduino Integrated Development Environment (IDE) is the software platform used to write and upload code to the Arduino board. To control a servo motor, you need to write a program (often called a “sketch” in Arduino terminology) that communicates with the servo motor. Fortunately, Arduino has a built-in Servo library, which simplifies the process.

Before we begin writing the code, ensure you have the Arduino IDE installed on your computer and that your Arduino board is properly connected to your computer via USB.

Installing the Servo Library

In most cases, the Servo library comes pre-installed with the Arduino IDE. However, if you’re starting fresh or need to check, here’s how to install it:

Open the Arduino IDE.

Go to Sketch > Include Library > Manage Libraries.

In the Library Manager, type “Servo” into the search bar.

If it’s not installed, click the "Install" button next to the Servo library.

Writing the Code to Control the Servo Motor

Now that we have the hardware set up, let’s move on to writing the Arduino code. Here’s a simple sketch that will rotate the servo motor between 0° and 180°:

#include

Servo myServo; // Create a Servo object

void setup() {

myServo.attach(9); // Attach the servo signal wire to pin 9

}

void loop() {

// Sweep the servo from 0° to 180° and back to 0°

for (int pos = 0; pos <= 180; pos++) {

myServo.write(pos); // Move the servo to 'pos' degrees

delay(15); // Wait for the servo to reach the position

}

for (int pos = 180; pos >= 0; pos--) {

myServo.write(pos); // Move the servo back to 'pos' degrees

delay(15); // Wait for the servo to reach the position

}

}

Understanding the Code

Servo Library: The #include line tells the Arduino IDE to use the Servo library, which simplifies servo control.

Servo Object: Servo myServo; creates a Servo object that will control the motor. You can create multiple servo objects if needed, but in this example, we're only using one.

Attaching the Servo: In the setup() function, we attach the servo to pin 9 using myServo.attach(9);. This tells the Arduino which pin will be controlling the servo’s position.

Sweeping the Servo: In the loop() function, we move the servo motor from 0° to 180° and back again. The myServo.write(pos) command tells the servo to move to the angle specified by pos. We then use delay(15) to give the servo enough time to reach each position before continuing.

Uploading the Code

Once the code is ready, connect your Arduino to the computer using a USB cable, select the correct board and port under Tools > Board and Tools > Port, and click the Upload button in the Arduino IDE. The code will be compiled and uploaded to the Arduino board.

When the upload is complete, the servo motor should begin to sweep back and forth between 0° and 180°, as defined in the code.

Troubleshooting

If your servo motor doesn’t behave as expected, consider the following:

Power Supply: If the servo is not moving or behaves erratically, ensure that your Arduino is providing enough power to the motor. Some servos draw more current than the Arduino board can provide, especially under load. You may need an external power supply.

Wiring: Double-check the wiring connections, particularly the signal pin. Ensure the motor is connected to the correct PWM pin on the Arduino.

Servo Compatibility: Some servo motors might have different voltage requirements. Ensure you are using a 5V or 6V servo motor with an Arduino Uno or similar board.

By following this guide, you now have a functional servo motor setup controlled by Arduino, which can be used in a wide range of projects. You can experiment with different angles, speeds, and even integrate sensors to make the servo motor react to external conditions. The possibilities are endless!

Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.

Update:2025-10-15

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.