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

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

小编

Published2025-10-15

Servo motors are incredibly useful in various DIY projects and robotic applications. This article takes you through a detailed, step-by-step guide on how to connect a servo motor to an Arduino. Whether you’re a beginner or an advanced maker, this guide will help you easily understand the process and get your projects moving in no time!

Servo motor, Arduino, connect servo motor to Arduino, DIY robotics, servo motor control, Arduino projects, robotics tutorial, servo motor wiring.

Introduction to Servo Motors and Arduino

What is a Servo Motor?

A servo motor is a type of electric motor that allows precise control of angular position, velocity, and acceleration. Unlike regular motors that rotate continuously, a servo motor can rotate to specific positions within a defined range, often from 0° to 180°. This makes it ideal for tasks like moving robot arms, controlling camera gimbals, or even steering mechanisms in RC vehicles.

Servo motors are controlled using PWM (Pulse Width Modulation) signals, where the duration of the pulse dictates the motor’s position. This precise control over rotation makes servo motors a favorite among hobbyists, engineers, and roboticists.

Why Use Arduino with Servo Motors?

Arduino is an open-source microcontroller platform that is easy to use and versatile, making it perfect for beginners and advanced users alike. It’s widely used in DIY projects, prototyping, and educational applications. The combination of Arduino and a servo motor is powerful because it allows you to automate tasks that require controlled movement.

Arduino offers a simple way to generate PWM signals, which makes controlling a servo motor easier. You don’t need to worry about the complexities of motor control algorithms or advanced circuitry. All you need is a few lines of code and some basic wiring.

The Components You’ll Need

Before diving into the process of connecting a servo motor to an Arduino, you’ll need the following components:

Arduino Board – Any Arduino board, such as Arduino Uno, Arduino Nano, or Arduino Mega, will work.

Servo Motor – A typical hobby servo motor (like the SG90 or MG90S) is sufficient for most applications.

Jumper Wires – These will be used to make the necessary connections.

External Power Source (Optional) – If you are using a high-power servo motor or controlling multiple motors, you may need an external power supply.

Wiring the Servo Motor to Arduino

Now that you have the necessary components, let's get into how to physically connect the servo motor to your Arduino board.

Identify the Servo Motor Pins

Most hobby servo motors have three wires:

Red: This is the power pin (VCC).

Brown or Black: This is the ground pin (GND).

Yellow or Orange: This is the signal pin (PWM).

Connect the Servo to Arduino

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

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

Signal (PWM): Connect the yellow/orange wire from the servo to one of the digital PWM pins on the Arduino (typically, pins 9, 10, or 11 are used).

Optional: Use an External Power Source

If your servo motor is power-hungry (such as larger or high-torque servos), you should avoid powering it directly from the Arduino, as it may damage the board. In this case, use an external 5V power supply. Just make sure the grounds of both the Arduino and the power source are connected.

Understanding PWM Control

The key to controlling the position of a servo motor is through Pulse Width Modulation (PWM). In simple terms, PWM is a signal that is "on" and "off" rapidly. The amount of time the signal is "on" determines the position of the servo motor.

High Pulse Width (around 2ms) will move the servo to its maximum position (usually 180 degrees).

Low Pulse Width (around 1ms) will move the servo to its minimum position (usually 0 degrees).

A 1.5ms pulse generally centers the servo at 90 degrees.

In the next part, we’ll explore how to control the servo motor using Arduino programming.

Programming the Arduino to Control the Servo Motor

Setting Up the Arduino IDE

To begin controlling your servo motor, you’ll need to program your Arduino. First, make sure you have the Arduino IDE (Integrated Development Environment) installed on your computer. You can download it from the official Arduino website. The IDE is where you will write, compile, and upload your code to the Arduino.

Install the Servo Library

Arduino has a built-in Servo library that simplifies the task of controlling servo motors. To use this library, you don’t need to install anything separately, as it is included in the IDE by default.

Open the Servo Library

In the Arduino IDE, go to Sketch > Include Library > Servo to include the library in your code. This will allow you to easily control the servo motor without manually generating PWM signals.

Writing the Code

Now, let's write some basic code to move the servo motor to different positions. Open a new sketch in the Arduino IDE and copy the following code:

#include // Include the Servo library

Servo myServo; // Create a Servo object

void setup() {

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

}

void loop() {

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

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

}

Breaking Down the Code:

#include : This line includes the Servo library, which makes it easy to control servo motors.

Servo myServo;: Creates a Servo object called myServo.

myServo.attach(9);: This connects the servo motor to the digital pin 9 on the Arduino.

myServo.write(0);: Moves the servo to 0 degrees.

delay(1000);: Pauses the program for 1 second, allowing the servo to reach its position before continuing.

myServo.write(90);: Moves the servo to the middle position (90 degrees).

myServo.write(180);: Moves the servo to 180 degrees.

Uploading the Code

Once your code is ready, connect your Arduino board to your computer via USB and select the correct board and port in the Arduino IDE. Click on the Upload button (right arrow) in the IDE, and the code will be uploaded to the Arduino.

Once uploaded, your servo motor should start moving between 0°, 90°, and 180° with 1-second pauses in between.

Experimenting with Servo Control

You can experiment with different angles by changing the numbers inside the myServo.write() function. For example:

myServo.write(45); will move the servo to 45 degrees.

myServo.write(135); will move it to 135 degrees.

You can also use delay() to control the speed of the servo’s movements. However, keep in mind that servo motors typically move fairly quickly, so extremely fast movements might cause wear over time.

Advanced Control: Using Multiple Servos

If you’re working on a project that requires more than one servo motor, it’s easy to control multiple servos with Arduino. You can create additional Servo objects for each motor and attach them to different pins on the Arduino. For example:

#include

Servo myServo1; // First servo object

Servo myServo2; // Second servo object

void setup() {

myServo1.attach(9); // First servo on pin 9

myServo2.attach(10); // Second servo on pin 10

}

void loop() {

myServo1.write(0);

myServo2.write(180);

delay(1000);

myServo1.write(90);

myServo2.write(90);

delay(1000);

myServo1.write(180);

myServo2.write(0);

delay(1000);

}

This code will control two servos simultaneously, moving them between 0°, 90°, and 180°.

With the knowledge from this guide, you’re now ready to integrate servo motors into your Arduino projects and explore endless possibilities for creative automation and robotics. Whether you’re building a robotic arm, a moving robot, or an automated camera, Arduino and servos are your gateway to making your ideas come to life!

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.