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 Uno: A Step-by-Step Guide

小编

Published2025-10-15

Understanding the Basics of Servo Motors and Arduino Uno

Introduction to Servo Motors and Arduino Uno

Servo motors are widely used in robotics, automation, and various other applications where precise movement control is needed. They are different from standard DC motors because they can be positioned at specific angles, providing excellent precision for tasks such as controlling robotic arms, steering wheels, or camera gimbals.

The Arduino Uno is one of the most popular microcontroller boards used in DIY electronics projects. It’s an open-source platform based on simple hardware and software, perfect for those who are just getting started with electronics. The Arduino Uno makes it easy to interface with various sensors and actuators, such as servo motors, using just a few lines of code.

In this tutorial, we will go through the process of connecting a servo motor to an Arduino Uno and controlling its movement. Whether you're looking to build a basic robot, create a mechanical arm, or automate any device, this guide will provide all the steps needed to get you up and running.

Components You’ll Need

Before diving into the wiring and coding, let’s first list the components you’ll need for this project:

Arduino Uno – The microcontroller that will control the servo.

Servo Motor – A standard servo motor like the SG90 or MG995.

Breadboard – Used for connecting wires.

Jumper Wires – For making the connections.

External Power Supply – Depending on your servo motor, you may need an external power supply to avoid overloading the Arduino board.

Resistors (optional) – Depending on your setup, you might need resistors for protection or voltage adjustment.

Wiring the Servo Motor to Arduino Uno

Now let’s look at how to wire the servo motor to the Arduino Uno. A typical servo motor has three wires:

Power (usually red) – Provides power to the motor.

Ground (usually black or brown) – Connects to the ground of the Arduino.

Signal (usually yellow or orange) – This wire receives control signals from the Arduino to move the motor.

Step-by-Step Wiring:

Servo Power Wire: Connect the red power wire of the servo to the 5V pin on the Arduino Uno. If you're using a higher power servo (like the MG995), you might want to connect the servo’s power to an external power supply (e.g., 5V or 6V) to avoid drawing too much current from the Arduino.

Servo Ground Wire: Connect the black or brown ground wire from the servo to the GND pin on the Arduino.

Servo Signal Wire: Connect the yellow or orange signal wire to one of the digital I/O pins on the Arduino. For this example, we will use pin 9.

Power Considerations

While the Arduino Uno can power smaller servos (like the SG90) directly from the 5V pin, larger servos may draw more current than the Arduino can provide. If you’re using a more powerful servo, make sure to provide an external power source to prevent overloading the Arduino’s onboard voltage regulator. In such cases, ensure that the ground of the external power supply is also connected to the GND pin on the Arduino to have a common reference for the signal.

Servo Motor Basics: How It Works

Servo motors are driven by a control signal that dictates the position of the motor's shaft. The motor’s shaft can rotate from 0° to 180° (for most hobby servos). The position of the shaft is determined by the pulse width modulation (PWM) signal sent from the Arduino to the servo’s signal pin.

PWM is a method of controlling the amount of power supplied to the motor by varying the width of the pulse. By adjusting the length of the high pulse within each cycle, the servo motor can be directed to a specific position.

For example:

A 1.5ms pulse will position the motor at 90° (neutral).

A 1ms pulse will move it to 0°.

A 2ms pulse will move it to 180°.

Understanding this is crucial when programming the Arduino to control the servo motor.

Programming the Arduino to Control the Servo Motor

Introduction to the Arduino IDE

The Arduino IDE (Integrated Development Environment) is the software used to write and upload code to the Arduino Uno. It comes with built-in libraries for controlling various hardware components, including servo motors. In this part of the guide, we’ll walk through the process of writing a program to control the servo motor using the Arduino IDE.

Installing the Servo Library

To make the process easier, we will use the Servo library, which is pre-installed in the Arduino IDE. This library simplifies controlling the servo motor by handling the low-level details of PWM generation.

Steps to Install the Servo Library:

Open the Arduino IDE.

Go to Sketch > Include Library > Servo. The Servo library should be listed under the “Contributed” section if it is already installed.

Now that the library is available, we can start writing the code.

Writing the Arduino Code

The code to control a servo motor is quite simple. The Servo library provides an easy-to-use function for attaching the servo to a specific pin and then moving it to a desired angle. Here’s a basic program to control the servo:

#include

Servo myServo; // Create a servo object to control the servo motor

void setup() {

myServo.attach(9); // Pin 9 is connected to the signal wire of the servo

}

void loop() {

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

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

}

Explanation of the Code:

#include : This line includes the Servo library, which provides the necessary functions to control a servo motor.

Servo myServo;: This creates an instance of the Servo class, which we will use to control the servo.

myServo.attach(9);: This line tells the Arduino that the servo motor is connected to pin 9.

myServo.write(angle);: This function moves the servo to a specific angle. The angle can be anywhere from 0 to 180 degrees.

delay(1000);: The delay function pauses the program for 1000 milliseconds (1 second). This gives the servo enough time to reach the desired position.

Uploading the Code to Arduino

Once the code is written, you can upload it to the Arduino Uno. To do this:

Connect the Arduino Uno to your computer using a USB cable.

In the Arduino IDE, select Tools > Board > Arduino Uno.

Select Tools > Port > the correct COM port (this should match the port your Arduino is connected to).

Click the Upload button in the IDE (the arrow icon).

The code will compile and upload to the Arduino, and the servo motor should start moving between the three positions: 0°, 90°, and 180°. You should see the servo rotate to each position, pause for a second, and then rotate to the next position in the loop.

Customizing the Servo Motion

You can modify the code to make the servo perform different actions. For example, if you want the servo to rotate back and forth between two positions, simply adjust the angles in the myServo.write() function. You could also experiment with different delay() times to control how quickly the servo moves between positions.

Another fun project is to use sensors, such as a potentiometer or a joystick, to control the position of the servo in real-time.

By following these steps, you can easily connect and control a servo motor using the Arduino Uno. This is just the beginning! With the knowledge gained from this guide, you can explore more advanced projects and expand your understanding of electronics and robotics.

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.