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

How to Control Servo Motor Speed with Arduino: A Beginner’s Guide

小编

Published2025-10-15

In this article, we explore the process of controlling the speed of a servo motor using an Arduino board. Servo motors are widely used in various applications, such as robotics, automation, and modeling. With Arduino, you can easily control the motor’s position, speed, and direction, making it an ideal choice for DIY enthusiasts and engineers. Whether you’re a beginner or looking to refine your knowledge, this guide provides clear instructions and insights on servo motor speed control.

Introduction to Servo Motors and Speed Control Basics

Servo motors are versatile and highly accurate motors used to control the angular position of a system. They are typically used in applications where precision and controlled movement are needed, such as in robotics, automation systems, and remote-controlled models. A servo motor consists of a DC motor, a gear system, and a feedback system that allows the motor to know its position.

While most motors simply spin continuously, servo motors can rotate to a specific angle, and the degree of rotation is controlled via a signal input. Typically, the control signal is provided using Pulse Width Modulation (PWM), where the width of the pulse determines the position of the motor. While controlling the position is crucial in many applications, adjusting the speed at which the servo reaches that position is equally important.

In this article, we’ll focus on how you can use an Arduino board to control the speed of a servo motor. Speed control in servo motors is often implemented by adjusting the time it takes for the motor to move from one position to another. The Arduino makes this task easier by providing precise control over the servo’s position and speed.

Understanding PWM (Pulse Width Modulation) and Its Role

To understand how to control servo motor speed, you need to first understand how PWM works. PWM is a modulation technique where the width of the pulse is varied to control the power supplied to the motor. The pulse width (or duration) controls the angular position of the servo motor, and the frequency of the pulse determines the speed at which the servo motor moves.

When using Arduino to control the servo motor, the servo library automatically uses PWM to send control signals to the motor. However, to control the speed, you must modify the duration between successive movements. Instead of instructing the motor to jump to a position instantaneously, we can introduce a delay, thereby controlling the motor’s speed.

Tools and Components You Will Need

Before we dive into the programming, let’s list the necessary tools and components for the project:

Arduino Board (e.g., Arduino Uno) – This will serve as the brain of the system.

Servo Motor – A standard servo motor like the SG90 or MG996R will work for most applications.

Jumper Wires – To connect the Arduino to the servo motor.

External Power Source (Optional) – If your servo motor requires more current than the Arduino can provide.

Breadboard (Optional) – For easy circuit assembly.

Wiring the Servo Motor to the Arduino

Now, let’s quickly review how to connect the servo motor to the Arduino board:

Signal Pin (PWM Control Pin): Connect the servo motor's signal wire (usually white or yellow) to a PWM-capable pin on the Arduino. Pin 9 is commonly used.

Power Pin: Connect the servo’s power wire (usually red) to the 5V pin on the Arduino or an external 5V power supply if needed.

Ground Pin: Connect the ground wire (usually black or brown) to the Arduino’s ground pin.

Once you have everything connected, you’re ready to start writing the code for speed control.

Arduino Code for Servo Motor Speed Control

To control the speed of the servo motor, we need to adjust the time it takes to move from one position to another. This is achieved by modifying the delay between movements using the Arduino code. Let’s go over the process step by step.

Step 1: Installing the Servo Library

Arduino comes with a built-in Servo library that makes it easy to control a servo motor. To begin, you need to include this library in your sketch (Arduino program).

#include

Step 2: Defining the Servo and Pin

Next, you define the servo object and specify the pin you are using to control the servo. In this case, we’ll use pin 9.

Servo myServo;

int servoPin = 9; // Pin connected to the signal wire of the servo

Step 3: Attaching the Servo to the Pin

In the setup() function, we initialize the servo and attach it to the specified pin. This step allows the Arduino to communicate with the servo motor.

void setup() {

myServo.attach(servoPin);

}

Step 4: Writing the Servo Angle with Speed Control

Now comes the fun part—controlling the speed. To smoothly move the servo from one position to another, you can create a loop that gradually increments the servo angle instead of jumping directly to a target position. The delay() function will help you control the time between each movement, thereby controlling the speed.

Here’s a basic example where the servo motor moves from 0° to 180° and then back to 0°, with a delay to control the speed:

void loop() {

for (int angle = 0; angle <= 180; angle++) { // Move from 0° to 180°

myServo.write(angle); // Command the servo to move to the current angle

delay(15); // Control the speed by adding a delay between movements

}

for (int angle = 180; angle >= 0; angle--) { // Move from 180° back to 0°

myServo.write(angle);

delay(15); // Delay controls the speed again

}

}

In the above code, the servo moves incrementally between 0° and 180°. The delay of 15 milliseconds between each movement causes the servo to move slowly. You can experiment with different delay values to adjust the speed. The larger the delay, the slower the movement.

Step 5: Advanced Speed Control (Using Variables)

You can also make the speed dynamic by introducing a variable to control the delay. For instance, you might want to control the speed based on an input, such as a potentiometer or a sensor. Here’s an example using a potentiometer connected to pin A0:

int potValue; // Variable to store the potentiometer value

int speed; // Variable to store the speed value

void loop() {

potValue = analogRead(A0); // Read the potentiometer value

speed = map(potValue, 0, 1023, 0, 100); // Map the value to a speed range (0-100)

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

myServo.write(angle);

delay(speed); // Control the speed with the potentiometer value

}

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

myServo.write(angle);

delay(speed); // Control the speed with the potentiometer value

}

}

In this version, the speed is controlled by the potentiometer. Turning the potentiometer will change the delay, effectively controlling how fast or slow the servo moves between positions.

Conclusion

By using the Arduino’s built-in Servo library and adjusting the delay between movements, you can easily control the speed of your servo motor. The key to controlling speed lies in the timing between angle changes. Whether you're building a robotic arm, an automated system, or simply experimenting with motors, this simple technique opens up many possibilities for motor control.

In the next section, we’ll cover more advanced techniques, such as smoothing the movement or using external libraries for enhanced control. For now, you’ve got a solid foundation in servo motor speed control using Arduino, and you can now apply it to a variety of exciting projects.

Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.

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.