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

How to Code and Run a Servo Motor with Arduino: A Step-by-Step Guide

小编

Published2025-10-15

In this comprehensive guide, learn how to code and run a servo motor using Arduino. With easy-to-follow instructions, this article provides practical tips for beginners and enthusiasts looking to control servos with their Arduino boards for a wide range of projects.

Arduino, servo motor, coding, motor control, electronics, Arduino tutorial, beginners, servos, robotics, automation, DIY projects

Introduction to Servo Motors and Arduino

What is a Servo Motor?

A servo motor is a type of motor that can precisely control the rotation of an object. Unlike regular motors, which rotate continuously, a servo motor can rotate to a specific position and hold that position with high accuracy. This makes servo motors perfect for applications in robotics, automation, model aircraft, and more. Servo motors generally have a control mechanism that receives an input signal, converting it into the desired position of the motor shaft.

The main advantage of servo motors is their ability to offer precise control over rotational movement. When connected to an Arduino, servos can be controlled using simple code to move to specific angles.

Why Use Arduino with a Servo Motor?

Arduino is a powerful microcontroller platform used in electronics projects. It’s beginner-friendly, open-source, and versatile, making it a great tool for hobbyists, students, and professionals. Connecting a servo motor to an Arduino allows you to control the motor’s position with ease, all through a few lines of code. It opens up possibilities for creating motion-driven projects such as robotic arms, automated doors, or even fun gadgets for your home.

In this article, we’ll walk you through the steps of coding and running a servo motor using Arduino, so you can use this knowledge in your own projects.

Materials You Will Need

Before we dive into coding, here’s a quick list of materials you’ll need for this project:

Arduino Board (any model will work, but Arduino Uno is often used for beginners)

Servo Motor (e.g., SG90, MG996R, or any standard 3-pin servo)

Jumper Wires

Breadboard (optional)

Power Source for your Arduino (usually a USB cable or battery pack)

Understanding the Wiring Setup

The servo motor typically has three wires:

Power (usually red): Supplies power to the motor (typically 5V or 3.3V depending on your servo's voltage rating).

Ground (usually black or brown): Provides the return path for the electrical current.

Signal (usually yellow or orange): Sends the control signal from the Arduino to the motor.

When connecting the servo to the Arduino:

Connect the Power wire of the servo to the 5V pin on the Arduino.

Connect the Ground wire to the GND pin on the Arduino.

Connect the Signal wire to a PWM-enabled digital pin (for example, pin 9).

This simple wiring will be enough to control the servo’s movements through Arduino code.

Writing the Code and Running the Servo Motor

Setting Up the Arduino IDE

To program the Arduino board, you’ll need the Arduino IDE (Integrated Development Environment), which is available for free. You can download it from the official Arduino website. Once installed, connect your Arduino board to your computer using a USB cable and open the Arduino IDE. Ensure that your board type and port are correctly selected in the "Tools" menu.

The Servo library in the Arduino IDE simplifies controlling servo motors. You don’t need to manually write complex code to generate PWM signals. The library does all the heavy lifting for you.

Basic Arduino Servo Code

Let’s break down the code to control the servo motor. The basic structure involves:

Including the Servo library.

Creating a Servo object.

Attaching the servo to the specific pin.

Moving the servo to the desired angle.

Here’s a simple example:

#include // Include the Servo library

Servo myServo; // Create a Servo object

void setup() {

myServo.attach(9); // Attach the servo to pin 9 on the Arduino

}

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

}

Explanation of the Code

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

Servo myServo; – This creates an object called myServo that will control the servo motor.

myServo.attach(9); – The attach function connects the servo motor to pin 9 on the Arduino board. You can change the pin number to match the pin you’re using.

myServo.write(0); – This moves the servo to a specific angle. In this case, 0 degrees (the motor's starting position).

delay(1000); – The delay function pauses the program for a specified amount of time (1000 milliseconds or 1 second in this case).

The loop will continuously move the servo between three positions: 0, 90, and 180 degrees, with a 1-second pause between each movement.

Testing and Debugging

After uploading the code to the Arduino board via the IDE, the servo motor should start moving to different angles. If the motor doesn’t move or behaves erratically, check the following:

Ensure that the wiring is correct and secure.

Verify that your power source provides enough current to the servo.

Double-check that the Arduino is properly connected to your computer and the right port is selected.

Advanced Servo Control: Speed and Precision

In the basic example above, the servo moves instantly between angles. However, you can also control the speed of the servo’s movement by gradually changing the position over time.

Here’s an example of moving the servo with controlled speed:

#include // Include the Servo library

Servo myServo;

void setup() {

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

}

void loop() {

for (int pos = 0; pos <= 180; pos++) { // Gradually move from 0 to 180 degrees

myServo.write(pos); // Set the servo to the current position

delay(15); // Small delay to slow down the movement

}

for (int pos = 180; pos >= 0; pos--) { // Gradually move back from 180 to 0 degrees

myServo.write(pos); // Set the servo to the current position

delay(15); // Small delay to slow down the movement

}

}

In this code, the for loop gradually moves the servo from 0 to 180 degrees, and then back to 0. The delay(15) adds a small pause between each position, which slows down the movement, allowing you to control how fast the servo moves.

Conclusion

In this article, we’ve explored how to code and run a servo motor using Arduino. We covered the basics of servo motors, explained the wiring setup, and walked you through writing the code to control the servo. With the tools and knowledge you’ve gained here, you can start integrating servos into your own DIY projects, from simple models to more advanced automation systems.

In the next part, we’ll delve deeper into advanced servo control techniques, including using multiple servos, adding sensor inputs, and exploring creative project ideas that involve servo motors. Stay tuned for more on this exciting topic!

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.