小编
Published2025-10-15
This article provides an in-depth guide on how to control a servo motor with Arduino, explaining the code, wiring, and various applications. Whether you're a beginner or an enthusiast looking to expand your skills, this guide offers the insights you need to get your servo motor moving in no time!

Arduino, servo motor, servo control, Arduino code, electronics, DIY projects, robotics, beginner guide, servo motor tutorial, coding for Arduino, Arduino projects
Introduction to Arduino and Servo Motors
When it comes to DIY electronics projects, Arduino has become an essential tool for hobbyists and professionals alike. One of the most exciting components that you can control with Arduino is the servo motor. This small but powerful device allows you to add precise movement to your projects, making it perfect for a wide range of applications—from building robotic arms to creating automated camera sliders.
A servo motor is a type of motor that is used for precise control of angular position. Unlike regular DC motors, which rotate continuously, a servo motor can rotate to specific angles, making it ideal for tasks that require high precision. Servo motors typically have a small shaft that can rotate from 0° to 180°, though some models can rotate even further.
Understanding the Basics of Arduino
Before diving into the code, it’s important to understand how the Arduino works. Arduino is an open-source electronics platform based on simple software and hardware. It consists of a small microcontroller that reads inputs from sensors (like a potentiometer or a temperature sensor), processes the information, and then sends commands to output devices like motors, LEDs, or displays.
Arduino uses its IDE (Integrated Development Environment) to write and upload code to the board. The code is written in a programming language based on C/C++, which is simple enough for beginners yet powerful enough for advanced users.
Why Control a Servo Motor with Arduino?
There are many reasons why you might want to use Arduino to control a servo motor. Some of the most common include:
Precision: Servo motors allow for very accurate control, making them ideal for tasks like controlling the movement of robotic arms or steering mechanisms in autonomous vehicles.
Automation: With Arduino, you can automate the movement of a servo motor based on various inputs, such as temperature or time of day. This is commonly used in applications like automatic camera systems or home automation.
Flexibility: Servo motors are relatively simple to control, and with Arduino, you can modify the motor’s movement based on complex conditions, opening up a world of possibilities for your projects.
To get started, you'll need a few basic components. Here’s what you’ll need:
Arduino board (such as the Arduino Uno or Nano)
Breadboard (optional, depending on your setup)
External power supply (if required by the servo motor)
Arduino IDE installed on your computer
Wiring the Servo Motor to Arduino
The wiring process for connecting a servo motor to an Arduino is straightforward. Here's a simple step-by-step guide:
Connect the Servo Motor’s Power Wire: The servo motor will have a red wire for power. Connect this wire to the 5V pin on the Arduino.
Connect the Servo Motor’s Ground Wire: The servo will also have a black or brown wire for ground. Connect this to the GND pin on the Arduino.
Connect the Servo Motor’s Control Wire: The control wire, typically yellow or white, sends signals from the Arduino to the motor. Connect this to any of the PWM-capable pins on your Arduino (for example, pin 9).
Introduction to Arduino Code for Servo Motors
Once your hardware is set up, it's time to look at the code. The basic principle behind controlling a servo motor with Arduino is to send PWM (Pulse Width Modulation) signals to the servo's control pin. By adjusting the width of these pulses, you can control the position of the motor’s shaft.
In the next section, we'll take a closer look at the specific Arduino code required to get your servo motor moving.
Writing and Understanding the Arduino Code for Servo Motors
The Basic Arduino Code for Controlling a Servo Motor
Here’s a basic example of Arduino code to control a servo motor:
#include // Include the Servo library
Servo myServo; // Create a servo object to control the motor
myServo.attach(9); // Attach the servo to pin 9 on the Arduino
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
Include the Servo Library: The first line includes the Servo.h library, which simplifies controlling a servo motor. This library contains built-in functions that allow us to control servo motors without having to manually deal with PWM signals.
Create a Servo Object: The line Servo myServo; creates an object named myServo that represents the servo motor.
Setup Function: Inside the setup() function, the attach() method is used to link the servo object to a specific pin on the Arduino (in this case, pin 9). This is where the control wire from the servo is connected.
Loop Function: The loop() function repeatedly moves the servo motor to different positions. The write() function sets the motor's position to a specified angle (0, 90, or 180 degrees), and the delay() function pauses the program for a specified amount of time (1000 milliseconds, or 1 second, in this case).
Adjusting Servo Motor Angles
You can easily change the servo’s position by modifying the argument passed to the write() function. The servo motor typically rotates from 0° to 180°. For example, if you wanted the servo to move to 45°, you would change the line:
myServo.write(90); // Move the servo to 90 degrees
myServo.write(45); // Move the servo to 45 degrees
This allows you to control the servo's angle precisely.
Advanced Control: Using Input to Control Servo Movement
You can also enhance the functionality by using input from other sensors or devices to control the servo’s position. For example, if you use a potentiometer (a variable resistor), you can use the value read from the potentiometer to control the servo’s angle in real time.
Here’s an example of how you could control a servo motor using a potentiometer:
int potPin = A0; // The potentiometer is connected to analog pin A0
int potValue = 0; // Variable to store potentiometer value
myServo.attach(9); // Attach the servo to pin 9
potValue = analogRead(potPin); // Read the potentiometer value (0-1023)
potValue = map(potValue, 0, 1023, 0, 180); // Map the potentiometer value to the range of 0-180
myServo.write(potValue); // Move the servo to the new angle
delay(15); // Wait for the servo to reach the position
If your servo isn’t behaving as expected, here are a few common issues to check:
Incorrect Wiring: Ensure that the power, ground, and control wires are connected to the correct pins on the Arduino.
Insufficient Power: Some servo motors require more power than the Arduino can supply through its 5V pin. If the motor isn’t moving properly, try using an external power supply.
Inconsistent Code: Ensure that you’ve correctly written the code and uploaded it to the Arduino. Check for any syntax errors or issues in your logic.
By now, you should have a basic understanding of how to control a servo motor with Arduino. With just a few lines of code, you can easily rotate a servo to specific angles, automate movements, and integrate sensors to create interactive systems. The possibilities are endless, and with Arduino's flexibility, you can apply this knowledge to a variety of exciting projects in robotics, automation, and beyond.
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 Kpower's product specialist to recommend suitable motor or gearbox for your product.