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 Beginners Guide

小编

Published2025-10-15

Learn how to control a servo motor with Arduino in this detailed guide. Whether you're a beginner or looking to expand your skills, we'll walk you through the process step by step, from basic components to programming techniques.

Arduino, servo motor, control, beginner, guide, programming, electronics, robotics, DIY projects

Introduction to Servo Motors and Arduino Basics

Understanding Servo Motors

Servo motors are versatile and widely used in electronics and robotics projects. Unlike regular motors, which rotate continuously, servo motors have the ability to rotate to a specific position, typically between 0 and 180 degrees, depending on the model. This makes them ideal for projects that require precise movement, such as robotic arms, camera gimbals, and automated doors.

A servo motor consists of a small DC motor, a potentiometer for position feedback, and a control circuit that interprets signals. Servo motors usually come with three main wires:

Power (VCC) – Usually 5V, depending on the motor.

Ground (GND) – Common ground for the circuit.

Signal (Control Pin) – This is the most critical wire, as it receives the PWM (Pulse Width Modulation) signal from the controller (in this case, Arduino) to determine the position of the motor shaft.

Why Arduino?

Arduino is a popular open-source microcontroller platform that provides a simple way to interact with electronics. With its easy-to-use environment, Arduino allows you to control various devices, including lights, motors, and sensors, through simple programming.

For beginners, Arduino is the perfect choice because it eliminates much of the complexity involved in microcontroller programming and hardware interfacing. All you need is a basic understanding of how to connect components, write some code, and you're ready to go.

Materials You’ll Need

Before diving into the Arduino code and wiring, you’ll need a few basic components:

Arduino board (Arduino Uno is recommended for beginners)

Servo motor

Breadboard (optional for prototyping)

Jumper wires

Power supply (if necessary for your servo motor)

Arduino IDE (software to write and upload code)

Wiring the Servo Motor to the Arduino

Now, let’s get hands-on with the wiring. Here's how to connect the servo motor to the Arduino board:

Servo Motor to Arduino:

Connect the VCC wire of the servo to the 5V pin on the Arduino.

Connect the GND wire of the servo to the GND pin on the Arduino.

Connect the Signal wire (usually yellow or white) to one of the PWM-enabled pins on the Arduino. In this case, we’ll use pin 9.

Power Considerations:

If your servo motor draws more power than the Arduino can supply (such as larger motors), consider using an external power source for the servo. In that case, make sure the grounds of both the Arduino and the external power supply are connected.

Once everything is wired up, you're ready to write the code to control the motor.

Programming the Arduino to Control the Servo Motor

Setting Up the Arduino IDE

To begin, you'll need to download and install the Arduino IDE (Integrated Development Environment) on your computer. The IDE allows you to write and upload code to the Arduino board. It's available for Windows, macOS, and Linux.

Once you have the Arduino IDE installed, launch the program and ensure your board and port are correctly set:

Select the correct Board under Tools > Board.

Select the correct Port under Tools > Port.

Writing Your First Servo Motor Control Code

To control the servo motor, we’ll use the Servo library that comes pre-installed with the Arduino IDE. This library simplifies the process of sending PWM signals to the servo motor.

Here’s a basic example of how to control the servo motor’s position:

#include // Include the Servo library

Servo myServo; // Create a servo object

void setup() {

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

}

void loop() {

myServo.write(0); // Move servo to 0 degrees

delay(1000); // Wait for 1 second

myServo.write(90); // Move servo to 90 degrees

delay(1000); // Wait for 1 second

myServo.write(180); // Move servo to 180 degrees

delay(1000); // Wait for 1 second

}

Explanation of the Code:

#include : This line includes the Servo library, which allows you to control a servo motor using easy-to-understand functions.

Servo myServo;: This creates a servo object called myServo, which we will use to control the servo motor.

myServo.attach(9);: This command attaches the servo motor to pin 9 on the Arduino. This tells the Arduino which pin will send the PWM signal to the motor.

myServo.write(0);: The write() function is used to set the servo position. It takes a value from 0 to 180, where 0 is the minimum position and 180 is the maximum.

delay(1000);: This function pauses the program for 1000 milliseconds (or 1 second), allowing the servo time to reach its new position.

How PWM Works for Servo Motors

Pulse Width Modulation (PWM) is a technique used to control the speed or position of motors by varying the width of the pulses sent to the motor. For servo motors, PWM is used to control the angle of the motor’s shaft.

A typical PWM signal for a servo consists of a series of pulses with varying width:

A short pulse (around 1 millisecond) positions the motor at 0 degrees.

A longer pulse (around 1.5 milliseconds) places the motor at 90 degrees.

An even longer pulse (around 2 milliseconds) places the motor at 180 degrees.

Fine-Tuning the Servo Motor’s Movement

While the above example works fine for basic control, you might want to fine-tune the servo’s movement to make it smoother or more responsive. You can modify the delay times or even create a gradual movement by varying the angle in small increments:

void loop() {

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

myServo.write(pos); // Move 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 servo to 'pos' degrees

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

}

}

This code slowly moves the servo from 0 degrees to 180 degrees and back to 0 degrees, one degree at a time.

Conclusion

Controlling a servo motor with Arduino is an excellent way to learn the basics of microcontroller programming and robotics. By understanding the fundamentals of PWM signals and leveraging the power of the Arduino IDE, you can create a variety of projects that require precise movement, such as robotic arms, automated systems, or even simple toys.

In this guide, we've covered the basic setup, wiring, and programming required to control a servo motor with an Arduino. As you gain more experience, you can experiment with more advanced techniques, like using multiple servos or integrating sensors to make your projects even more dynamic.

Stay tuned for the next part of this guide, where we will explore additional tips and tricks for controlling multiple servos and incorporating them into more complex projects!

End of Part 1

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.