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

How to Use Servo Motor with Arduino Uno: A Beginners Guide

小编

Published2025-10-15

In this guide, we will explore how to connect and control a servo motor using an Arduino Uno. Perfect for beginners, this tutorial covers the essential components, wiring, coding, and tips to get your servo motor running with your Arduino project.

Servo motor, Arduino Uno, beginner guide, servo motor control, Arduino project, robotics, electronics tutorial, servo motor wiring

Understanding the Basics of Servo Motors and Arduino Uno

If you’ve ever been curious about how robotic arms or automated systems work, you’ve likely encountered servo motors. These compact, powerful motors allow precise control over rotation, making them ideal for applications like controlling the position of objects or even building robotic joints. In this section, we’ll dive into what a servo motor is, how it works, and how to use it with your Arduino Uno.

What is a Servo Motor?

A servo motor is an actuator that rotates an object to a specific angle. Unlike regular motors that keep rotating until turned off, servo motors are controlled by sending signals, usually through PWM (Pulse Width Modulation), that determine the exact angle of rotation.

Servo motors consist of three main components:

Motor: This is the core component responsible for turning the arm or shaft.

Control Circuit: It receives PWM signals and interprets them to determine the exact position of the motor shaft.

Potentiometer: This is used for feedback, ensuring the motor knows its exact position.

The most commonly used servo motors in hobby electronics are standard servos, which typically rotate between 0° and 180°. Other types, such as continuous rotation servos, can spin freely in either direction, much like a regular motor.

Why Use Servo Motors with Arduino?

Arduino is a fantastic platform for beginners and hobbyists to learn and experiment with electronics and programming. The ability to control a servo motor is one of the most popular and useful applications of Arduino, especially in projects like robotics, model cars, and automated systems. The simplicity of using PWM signals to control the angle of the servo motor makes it an excellent starting point for anyone learning about hardware control.

Arduino Uno, a popular microcontroller board, can easily generate PWM signals, which makes it compatible with most standard servo motors. By using Arduino, you can program the servo to move to specific positions, perform complex movements, and even react to sensor inputs or user commands.

Essential Components for the Project

Before we get into the coding part, let’s first discuss the essential components you’ll need for this project:

Arduino Uno Board: The microcontroller that will control the servo motor.

Servo Motor: A standard hobby servo motor (e.g., SG90 or MG90).

Jumper Wires: For making connections between the servo motor and the Arduino board.

Breadboard (Optional): If you plan to use additional components such as sensors or external power supplies.

External Power Supply (Optional): Depending on the servo motor's power requirements, you might need an external power supply, especially if the servo requires more current than the Arduino can supply through its 5V pin.

Wiring the Servo Motor to the Arduino Uno

The wiring process is simple, as the servo motor has three wires: Power (usually red), Ground (usually black or brown), and Signal (usually yellow or white). Here's how to connect it:

Power (Red): Connect this wire to the 5V pin on the Arduino.

Ground (Black/Brown): Connect this to one of the GND pins on the Arduino.

Signal (Yellow/White): Connect this to a PWM-capable pin on the Arduino (usually pins 9, 10, or 11). For this example, we’ll use pin 9.

Once you’ve made these connections, the servo motor will receive power and ground from the Arduino, while the signal pin will allow you to control its movement.

Setting Up the Arduino IDE for Servo Motor Control

Before you can control the servo motor, you’ll need to set up your Arduino IDE (Integrated Development Environment). Arduino IDE is where you write and upload code to your Arduino board. If you haven’t already installed the Arduino IDE, download and install it from the official Arduino website.

Once installed, follow these steps to prepare your project:

Open Arduino IDE and connect your Arduino Uno to your computer via USB.

Go to Tools > Board > Arduino Uno to select the correct board.

Go to Tools > Port and select the port to which your Arduino is connected.

Now, you're ready to write the code to control the servo motor!

Writing the Code to Control the Servo Motor

Now that you’ve connected the servo motor to your Arduino, let’s get into the fun part—writing the code that will make the motor move!

Using the Servo Library

Luckily, Arduino has a built-in library specifically designed for controlling servo motors. This makes it much easier to interact with the motor. To use the Servo library, follow these steps:

Open your Arduino IDE and create a new sketch (File > New).

At the top of your sketch, include the Servo library by typing:

#include

This line tells the Arduino IDE to include the library that handles all of the low-level details of controlling the servo motor.

Defining the Servo Object

Next, you’ll create a servo object that will control the motor. This is done by defining a new object using the Servo class:

Servo myServo;

Here, myServo is the name of the servo object. You can name it whatever you like, but keeping it simple makes your code easier to read.

Setting up the Servo Motor in the setup() Function

In the setup() function, you’ll initialize the servo motor and tell it which pin it's connected to. Add the following code to the setup() function:

void setup() {

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

}

The attach() function tells Arduino that the servo is connected to pin 9, so it knows where to send the PWM signals.

Controlling the Servo in the loop() Function

Now comes the part where you’ll control the servo’s movement. In the loop() function, you can tell the servo to move to specific angles. Here's an example of a simple program that moves the servo from 0° to 180° and back every 2 seconds:

void loop() {

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

delay(2000); // Wait for 2 seconds

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

delay(2000); // Wait for 2 seconds

}

In this code:

myServo.write(angle) moves the servo to the specified angle (0 to 180 degrees).

delay(2000) pauses the program for 2000 milliseconds (or 2 seconds) to allow the motor to reach the target position.

Testing the Code

After uploading the code to your Arduino Uno, your servo should start moving between 0° and 180° every 2 seconds. If everything is set up correctly, you’ll see the servo motor slowly rotate from one position to another.

Conclusion: Expanding Your Servo Projects

Congratulations! You’ve successfully controlled a servo motor using an Arduino Uno. This is just the beginning—servo motors can be used in a wide variety of applications. You could create a robotic arm, an automated camera mount, or even integrate multiple servos to make a more complex robotic system.

Experiment with different angles, speeds, and even sensors (like a potentiometer or ultrasonic sensor) to make your projects more dynamic and responsive. The possibilities are endless!

In the next part of this guide, we’ll dive deeper into using multiple servos simultaneously and explore more advanced control techniques. Stay tuned for the next step in your Arduino journey!

Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.

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.