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

How to Connect an Arduino to a Servo Motor: A Beginners Guide

小编

Published2025-10-15

Introduction to Servo Motors and Arduino

When it comes to learning robotics, automation, or creating interactive electronics projects, the combination of Arduino and a servo motor is one of the most fundamental and exciting pairings. Servo motors are essential components that allow precise control of angular position, and with Arduino, they become even more versatile. This guide will show you how to connect an Arduino to a servo motor, explaining the basic concepts, wiring, and the simple code that will get you started with controlling your servo.

What is a Servo Motor?

A servo motor is a type of motor designed to rotate to a specific angle rather than continuously spin like a regular DC motor. Inside a servo is a small motor, a feedback system (typically a potentiometer), and a control circuit. This feedback loop ensures that the motor’s position can be accurately controlled by the input signal, which is where Arduino comes into play.

Servo motors are commonly used in various applications, including robotics, RC cars, and even camera equipment, thanks to their precise control over position. Unlike DC motors, which need to be paired with additional components for position control, servo motors only require a pulse signal to define their movement.

What is Arduino?

Arduino is an open-source platform used for building electronic projects. At the heart of every Arduino is a microcontroller, which is essentially the "brain" of your project. The Arduino platform comes with easy-to-use software and hardware, making it incredibly popular among hobbyists, educators, and engineers.

By using simple code and connecting it to various components such as sensors, LEDs, and motors, you can bring your ideas to life. One of the most exciting things you can do with Arduino is control motors like the servo motor, which is the focus of this article.

Materials You’ll Need

Before we dive into the step-by-step instructions, here’s a list of the materials you’ll need to connect an Arduino to a servo motor:

Arduino Board (Arduino Uno is commonly used)

Servo Motor (e.g., SG90 or MG90)

Jumper Wires

Breadboard (optional, for easy connections)

External Power Supply (optional, if your servo requires more power than the Arduino can supply)

Arduino IDE installed on your computer

Wiring the Servo Motor to Arduino

The next step in the process is to properly wire the servo motor to your Arduino board. Servo motors typically have three wires:

Power (Red) – Supplies 5V to the motor.

Ground (Black or Brown) – Connects to ground.

Signal (Yellow or Orange) – The PWM signal that controls the position of the motor.

Connecting the Servo Motor

Here’s how to connect the servo motor to your Arduino board:

Connect the Power Pin of the Servo: Attach the red wire from the servo to the 5V pin on the Arduino board.

Connect the Ground Pin of the Servo: Connect the black or brown wire from the servo to the GND (ground) pin on the Arduino.

Connect the Signal Pin of the Servo: The yellow or orange wire from the servo should be connected to one of the PWM-enabled pins on the Arduino. For this example, you can use pin 9.

This simple setup will ensure that your servo motor is correctly wired to the Arduino and ready for control.

Understanding Pulse Width Modulation (PWM)

Pulse Width Modulation (PWM) is a technique used to control the position of the servo motor. The signal sent from the Arduino to the servo motor is a series of pulses, and the duration of these pulses determines the angle of the servo. The PWM signal has a frequency of 50 Hz, meaning there are 50 pulses per second. The width of each pulse (in microseconds) determines the exact position the servo will move to.

A pulse width of 1000 microseconds (1 millisecond) will position the servo at one extreme (0°).

A pulse width of 2000 microseconds (2 milliseconds) will position the servo at the opposite extreme (180°).

Anything in between will move the servo to intermediate angles.

How Servo Motors Work with Arduino

Once the servo is wired to the Arduino, you can use the built-in Servo library in Arduino to easily control its movement. The library simplifies the task of generating the necessary PWM signals, making it incredibly easy to set precise angles.

Now that we’ve covered the basics, let’s move on to writing the code that will control the servo motor.

Writing the Code and Testing Your Servo Motor

In this part, we will write the code necessary to control the servo motor using the Arduino. The code will move the servo to different angles and create a simple movement pattern.

Step 1: Setting Up the Arduino IDE

The first thing you need to do is install the Arduino IDE on your computer. If you haven’t already, download it from the official Arduino website and follow the installation instructions. Once installed, open the IDE and ensure you’ve selected the correct board and port under the "Tools" menu.

Step 2: Writing the Code

The Arduino Servo library makes it incredibly simple to control servo motors. To begin, we need to include the library and define the servo. Then, we will use the write() function to move the servo to various angles.

Here is an example of basic code that controls the servo motor:

#include // Include the Servo library

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

void setup() {

myServo.attach(9); // Pin 9 is the signal pin for the servo

}

void loop() {

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

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

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

delay(1000); // Wait for 1 second

}

Explanation of the Code

#include : This line includes the Servo library, which contains all the functions necessary for controlling a servo motor.

Servo myServo;: This creates a servo object, which will allow you to control the motor.

myServo.attach(9);: This tells the Arduino that the servo motor is connected to pin 9 (the PWM pin we used earlier).

myServo.write(angle);: The write() function is used to set the angle of the servo. In this case, it moves the servo to 0°, 90°, and 180°.

delay(1000);: The delay() function pauses the program for the specified amount of time (in milliseconds). This gives the servo enough time to reach its position before the next command is issued.

Step 3: Uploading the Code

Once you have written the code, it’s time to upload it to your Arduino board. Click the Upload button in the Arduino IDE, and the code will be compiled and sent to your Arduino board. The servo should start moving between the three angles: 0°, 90°, and 180°.

Troubleshooting Tips

Servo Not Moving: If the servo isn’t moving, double-check your wiring. Ensure that the signal wire is connected to the correct PWM pin (pin 9 in our case), and the power and ground wires are secure.

Servo Moving Erratically: If the servo is jittering or moving erratically, it might be receiving unstable power. Servo motors can draw more current than the Arduino can supply, so consider using an external power supply for the servo.

Servo Doesn’t Reach Full Range: If your servo doesn’t reach the expected 0° or 180°, it may be limited by the servo’s design or the PWM signal range. Experiment with values between 500 and 2500 microseconds to see if the servo responds better.

Step 4: Experiment with Advanced Control

Now that you have the basics down, you can start experimenting with more advanced control. For example, you can add a potentiometer to control the angle of the servo dynamically or even create a program that moves the servo continuously.

You can also combine multiple servo motors in a project, using the same principles to control each one individually.

Conclusion

Connecting an Arduino to a servo motor is one of the simplest and most rewarding projects for anyone starting with electronics or robotics. Whether you are creating a simple robotic arm or adding motion to a DIY project, understanding how to control servo motors with Arduino opens up a world of possibilities.

With the basics of wiring, code, and PWM under your belt, you are well on your way to creating more complex and interactive projects. So grab your Arduino, wire up a servo, and start experimenting—your journey into the world of robotics and automation is just beginning!

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.