小编
Published2025-10-15
Introduction to Servo Motors and Arduino
Servo motors are essential components in various electronic projects, from simple toys to complex robotics. They provide precise control over angular movement, unlike DC motors, which only rotate in one direction. Servo motors are particularly useful in projects where you need precise control of the position, such as moving an arm, steering a robot, or adjusting a camera angle. In this guide, we'll explore how to use a servo motor with an Arduino to control its position.

A servo motor is a type of motor that allows for precise control over its rotation. It consists of a small DC motor coupled with a feedback mechanism, which allows it to rotate to a specific angle and hold that position. This makes it ideal for tasks like steering mechanisms, robotic arms, and camera mounts. Servos typically rotate between 0° and 180°, though some models can offer a wider range.
Before we jump into coding, let's talk about the hardware you'll need for this project:
Arduino Board: Any Arduino model like the Arduino Uno, Nano, or Mega will work fine. The most common choice for beginners is the Arduino Uno.
Servo Motor: A standard servo motor will suffice. Popular models include the SG90 and MG995.
Jumper Wires: These will help connect your servo motor to the Arduino board.
External Power Source (optional): If your servo draws too much current, it might be beneficial to power the servo separately.
Breadboard (optional): If you want to make your setup neater, use a breadboard for connections.
How Servo Motors Work with Arduino
To control a servo motor with an Arduino, we use Pulse Width Modulation (PWM). PWM is a method of sending a pulse signal to the motor, where the width of the pulse determines the angle of rotation. The Arduino can generate PWM signals using its built-in Servo library, making it easy to control the servo’s position.
When you send a PWM signal to the servo, it interprets the pulse length as a command to move to a certain angle. For instance, a pulse width of 1 millisecond might move the servo to 0°, while 2 milliseconds could move it to 180°.
Wiring the Servo Motor to Arduino
Now that you have a basic understanding of how servo motors work, let’s go over the wiring. Here’s how to connect your servo motor to the Arduino:
Connect the Servo’s Power Pin (usually Red) to the 5V pin on the Arduino – This supplies power to the motor.
Connect the Servo’s Ground Pin (usually Black or Brown) to one of the GND pins on the Arduino – This completes the circuit.
Connect the Servo’s Control Pin (usually Yellow or Orange) to one of the PWM-capable pins on the Arduino (Pin 9 is a good choice) – This sends the control signals to the motor.
If your servo motor requires more current than the Arduino can provide, you may want to use an external power supply for the servo motor. Make sure to connect the ground of the external power supply to the Arduino ground to maintain a common reference.
Basic Servo Motor Control Using Arduino
To control the servo motor, we’ll use the Servo library that comes pre-installed with the Arduino IDE. This library simplifies the control of servos by providing easy-to-use functions like write() and writeMicroseconds().
Here’s the basic code to get your servo moving:
#include // Include the Servo library
Servo myServo; // Create a Servo object
myServo.attach(9); // Attach the servo control pin to pin 9 on the Arduino
myServo.write(0); // Rotate the servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Rotate the servo to 90 degrees (middle position)
delay(1000); // Wait for 1 second
myServo.write(180); // Rotate the servo to 180 degrees
delay(1000); // Wait for 1 second
In this example, we attach the servo to pin 9 of the Arduino, and then in the loop() function, we command the servo to move to three different positions: 0°, 90°, and 180°. The delay(1000) gives the servo time to reach each position before moving to the next one.
Advanced Techniques for Servo Control with Arduino
Now that you have learned how to control a basic servo with Arduino, let’s explore some more advanced techniques to enhance your projects. We will dive into the finer details of controlling multiple servos, using different types of servo motors, and improving the reliability of your setup.
Controlling Multiple Servos
In many Arduino projects, you'll need to control more than one servo motor. Fortunately, the Servo library allows you to control multiple servos simultaneously. You simply create multiple Servo objects and attach each one to a different PWM pin on the Arduino.
Here’s an example of controlling two servos:
#include // Include the Servo library
Servo servo1; // Create the first Servo object
Servo servo2; // Create the second Servo object
servo1.attach(9); // Attach the first servo to pin 9
servo2.attach(10); // Attach the second servo to pin 10
servo1.write(0); // Move servo 1 to 0 degrees
servo2.write(180); // Move servo 2 to 180 degrees
delay(1000); // Wait for 1 second
servo1.write(90); // Move servo 1 to 90 degrees
servo2.write(90); // Move servo 2 to 90 degrees
delay(1000); // Wait for 1 second
In this example, both servo motors move independently based on the commands sent to each one. You can add as many Servo objects as your Arduino can handle, though keep in mind that some older models might have limited PWM-capable pins.
Using a Potentiometer to Control Servo Position
One of the exciting things about Arduino is that you can interact with physical sensors to control the position of your servo motor. A potentiometer, for example, is a variable resistor that can be used to control the angle of a servo. Here’s how you can wire it up and use it in your project:
Connect the potentiometer’s middle pin to an analog input pin on the Arduino (e.g., A0).
Connect the two outer pins to 5V and GND, respectively.
Now, use the following code to control the servo position based on the potentiometer’s value:
int potValue = 0; // Variable to store potentiometer value
myServo.attach(9); // Attach the servo to pin 9
potValue = analogRead(A0); // Read the potentiometer value (0-1023)
int angle = map(potValue, 0, 1023, 0, 180); // Map the value to a 0-180 range
myServo.write(angle); // Set the servo to the mapped angle
delay(15); // Wait for the servo to reach the position
In this setup, turning the potentiometer will adjust the position of the servo in real-time. The map() function is used to convert the potentiometer’s value (ranging from 0 to 1023) into a corresponding angle (0° to 180°) for the servo.
Enhancing Your Project with Feedback and Accuracy
While servo motors offer good precision, their accuracy can sometimes be affected by mechanical limitations, like backlash or friction. For more complex applications, you may want to implement a feedback system. This could involve using sensors like encoders to provide real-time feedback to the Arduino on the servo's position, ensuring that the servo moves exactly where it needs to.
Additionally, when working with heavier loads or larger servos, make sure to account for factors like power consumption and heat dissipation. Using external power supplies and adding capacitors to stabilize power can significantly improve performance.
Using a servo motor with an Arduino opens up countless possibilities for creating interactive, precise projects. Whether you're building a robot, a mechanical arm, or a camera mount, understanding the basics of servo control can help you create a wide range of exciting electronics projects. As you become more comfortable with Arduino programming, you can explore more advanced techniques, like controlling multiple servos, integrating sensors, and even building your own custom control systems.
Experiment with different servo models, power setups, and sensors to take your projects to the next level. The flexibility of the Arduino platform and the simplicity of servo motors make them a perfect combination for hobbyists, students, and engineers alike.
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.