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

How to Connect and Program a Servo Motor with Arduino: A Complete Guide

小编

Published2025-10-15

Introduction to Servo Motors and Arduino

Servo motors are an essential component in the world of electronics and robotics. Whether you're building a robot, an automated door, or simply exploring the exciting world of Arduino, understanding how to control a servo motor is an invaluable skill. In this article, we will explore how to connect and program a servo motor using Arduino, making it easy for hobbyists and beginners to get started.

What is a Servo Motor?

A servo motor is a type of motor that provides precise control over angular position. Unlike regular motors that rotate continuously, servo motors can rotate to a specific angle and hold that position until directed to move again. This makes them ideal for tasks like positioning robotic arms, controlling camera angles, or steering wheels in automated vehicles. Servo motors typically operate on a 180-degree range of motion, but some can rotate beyond that.

These motors use pulse-width modulation (PWM) signals to control their position. PWM allows for fine control of the motor's angle, making it perfect for robotics applications where precise movement is needed.

Why Choose Arduino for Servo Motor Projects?

Arduino is an open-source microcontroller platform designed for building simple to complex electronics projects. It’s beginner-friendly, easy to program, and widely used in the maker community. One of the main reasons to use Arduino for controlling a servo motor is its simplicity and flexibility. With just a few lines of code, you can control the position of your servo motor, and the Arduino IDE (Integrated Development Environment) makes it easy to upload your program to the board.

The Arduino platform provides a Servo library that simplifies the control of servo motors. You won’t need to write complex PWM code; instead, you can use simple functions like write() and writeMicroseconds() to control the motor’s position.

Components You Need

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

Arduino Board (Uno, Nano, or any compatible board)

Servo Motor (standard 3-pin servo with control, power, and ground wires)

Jumper Wires

Breadboard (optional, for easy connections)

External Power Supply (for large servo motors, optional)

Wiring the Servo Motor to Arduino

Connecting a servo motor to your Arduino is simple. The motor typically has three wires: Power (red), Ground (black or brown), and Signal (yellow or orange). Here's how to wire it up:

Power (red): Connect the power wire from the servo to the 5V pin on the Arduino.

Ground (black/brown): Connect the ground wire from the servo to the GND pin on the Arduino.

Signal (yellow/orange): Connect the signal wire from the servo to one of the PWM-enabled pins on the Arduino (for example, pin 9). PWM pins are typically marked with a "~" sign next to the pin number.

If you’re using a large servo motor that requires more power than the Arduino can supply, you may need to use an external power supply. Make sure to connect the ground of the external power supply to the ground of the Arduino.

Testing the Servo Motor

Once you have your servo connected, it’s time to test it with a simple code. This initial test will make the servo move back and forth between 0° and 180°.

Open the Arduino IDE.

Go to File > Examples > Servo > Sweep.

Select the correct board and port from the Tools menu.

Upload the code to your Arduino.

The servo motor should now begin moving from one extreme (0°) to the other (180°), demonstrating the basic functionality of the motor.

Writing Code to Control the Servo Motor

Now that your hardware is set up, let’s dive into the software side of things. You can control a servo motor using the Servo library in Arduino, which provides an easy interface for controlling the motor’s position.

Basic Code Structure

The basic structure of an Arduino sketch to control a servo motor involves:

Including the Servo library at the beginning of the sketch.

Creating a Servo object to represent the motor.

Attaching the servo to the specified pin (this links the servo to the PWM pin you connected it to).

Using the write() function to set the position of the servo.

Let’s write a simple program to make the servo rotate to specific angles.

#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 the servo to 0 degrees

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

}

Explanation of the Code

#include : This line includes the Servo library, which allows us to control the servo motor easily.

Servo myServo;: We create a Servo object called myServo. This object represents the servo motor that we’ll control.

myServo.attach(9);: The attach() function connects the servo to a specific pin on the Arduino (in this case, pin 9).

myServo.write(angle);: This function tells the servo to move to a specific angle. The angle can range from 0° (fully left) to 180° (fully right).

delay(1000);: This line pauses the program for 1000 milliseconds (1 second), allowing the servo to stay at the set position before moving again.

When you upload this code to your Arduino, the servo will repeatedly move back and forth between 0° and 180°.

Advanced Servo Control

You can achieve more sophisticated control by adjusting the speed and the range of movement. If you want to move the servo slowly to a target position, you can use the write() function in combination with gradual steps.

Here’s an example of how to move the servo slowly:

#include

Servo myServo;

int angle = 0;

void setup() {

myServo.attach(9);

}

void loop() {

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

myServo.write(angle); // Move to the current angle

delay(15); // Wait 15 milliseconds to give the servo time to move

}

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

myServo.write(angle); // Move to the current angle

delay(15); // Wait 15 milliseconds to give the servo time to move

}

}

In this example, the servo will slowly move from 0° to 180° and then back to 0° by incrementally changing the angle by 1 degree in each loop iteration. The delay(15) ensures the servo has time to reach each position before moving to the next one.

Conclusion

Connecting and programming a servo motor with Arduino is a rewarding and educational experience that introduces you to the world of robotics and electronics. Whether you're building a robot, a mechanical arm, or simply experimenting, learning how to control a servo motor will unlock countless possibilities. By using simple code and hardware, you can create powerful and interactive projects that respond to user input or perform automated tasks.

In this article, we've covered everything you need to know about connecting your servo to an Arduino, wiring it up correctly, and programming it to perform basic movements. From here, the possibilities are endless, and with a little creativity, you can apply this knowledge to more complex systems and innovations in the world of DIY electronics.

Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.

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.