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

Connecting a Servo Motor to Arduino Uno: A Step-by-Step Guide

小编

Published2025-10-15

Introduction to Servo Motors and Arduino Uno

When it comes to building robotics or automating a specific task, the servo motor is one of the most versatile components you can use. Whether you're constructing a robotic arm, a mini drone, or simply controlling a rotating object, the servo motor offers precise control over its movement. However, connecting and controlling a servo motor requires a basic understanding of how it works, and the Arduino Uno is the perfect platform to get started.

The Arduino Uno is one of the most popular microcontrollers in the world, beloved by beginners and seasoned makers alike for its simplicity and flexibility. In this guide, we’ll walk through the step-by-step process of connecting a servo motor to an Arduino Uno board, writing the necessary code, and finally, controlling the motor’s movement with ease.

What is a Servo Motor?

A servo motor is a type of motor that is capable of rotating to a specific position within a defined range. Unlike traditional DC motors, which rotate continuously, servo motors can be controlled with high precision. A typical servo motor has a control mechanism that allows it to rotate between 0° and 180°, though there are some servos that allow for a broader range of motion.

Components Needed

Before we dive into the wiring and coding, let’s gather the essential components for this project:

Arduino Uno: This will be the brain of your project, controlling the servo motor’s movement.

Servo Motor: A standard hobby servo motor (like the SG90) will work perfectly for this tutorial.

Jumper Wires: To make the necessary connections between the servo and Arduino.

Breadboard (optional): While not necessary, a breadboard helps keep the project organized.

Power Source: Ensure your Arduino Uno has power via USB or an external power adapter.

Wiring the Servo Motor to Arduino Uno

Connecting the servo motor to the Arduino is straightforward. Here’s a simple breakdown of the connections:

Servo Motor Wiring:

Red wire (VCC): Connect this to the 5V pin on the Arduino board. This will power the servo.

Brown wire (Ground): Connect this to the GND pin on the Arduino to complete the circuit.

Orange or Yellow wire (Signal): This is the most important connection. It transmits control signals from the Arduino to the servo, determining its position. Connect this to Pin 9 (or any other PWM-enabled pin) on the Arduino.

What is PWM (Pulse Width Modulation)?

The key to controlling the position of the servo motor is PWM, which is a technique used to simulate an analog output using digital signals. With PWM, the Arduino sends a square wave of varying pulse widths to the signal pin of the servo motor, which determines how far the motor turns. By changing the width of the pulse, you change the servo's position.

Writing the Code and Controlling the Servo Motor

Now that we have our servo motor wired to the Arduino Uno, the next step is to write the code that will control its movement. Fortunately, the Arduino IDE (Integrated Development Environment) makes this process relatively simple. Here’s how to get started.

Step 1: Setting Up the Arduino IDE

If you haven’t already, download and install the Arduino IDE on your computer from the official website. Once installed, open the IDE, and make sure the correct board is selected under the Tools menu. Choose Arduino Uno and the correct port for your device.

Step 2: Importing the Servo Library

Arduino makes controlling a servo motor incredibly easy with its Servo library, which provides functions for setting the position of the servo motor. To start, you’ll need to include this library in your code. This is done by simply adding the following line at the top of your sketch (program):

#include

Step 3: Defining the Servo Object

Next, we need to create a Servo object to represent the motor in the code. This allows us to use specific functions to control the motor’s position. Declare the servo object like this:

Servo myServo;

Step 4: Setting Up the Pin and Servo Movement

In the setup() function, we need to define the pin we connected the signal wire to and attach it to the servo object. This will allow us to control the servo through that pin.

void setup() {

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

}

Now we’re ready to move the servo! In the loop() function, we will set the servo’s position at different angles. The write() function is used to specify the angle of the servo motor, and it accepts values from 0° to 180°. Let’s write a simple code that moves the servo motor back and forth:

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

}

Step 5: Uploading the Code to the Arduino

With the code ready, connect your Arduino Uno to your computer using a USB cable. Once the code is ready, click the Upload button in the Arduino IDE. The IDE will compile the code and upload it to your Arduino board.

After uploading, you should see your servo motor slowly rotating back and forth from 0° to 180° every second. This basic functionality is just the beginning! By modifying the angles and the timing, you can create more complex movements for your project.

Step 6: Experimenting with Different Movements

While the basic code simply moves the servo from 0° to 180°, you can explore more advanced movements. For example, you can gradually change the position to create smooth, continuous motion, or you can create loops to control multiple servos at once.

void loop() {

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

myServo.write(pos);

delay(15);

}

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

myServo.write(pos);

delay(15);

}

}

This modified code gradually moves the servo from 0° to 180° and back, providing a smoother transition.

By connecting a servo motor to your Arduino Uno, you’ve just unlocked a whole new world of robotics and automation. Whether you're building an intricate project or just experimenting with simple movements, servo motors are an essential part of many electronics projects.

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.