小编
Published2025-10-15
Servo motors are essential components in various robotic and automation projects. This article explores how to use servo motors with the Arduino Uno, including step-by-step code instructions, circuit setup, and project ideas, making it easy for beginners to master this popular combination in electronics.

Arduino, servo motor, Arduino Uno, servo motor code, robotics, automation, Arduino programming, electronics, DIY robotics, servo motor control
Introduction to Servo Motors and Arduino Uno
Servo motors are electromechanical devices that are widely used in robotics, automation, and remote-controlled applications. Unlike standard DC motors, a servo motor is designed to rotate to a specific angle. This precision is achieved through the use of a feedback mechanism inside the motor that ensures accurate positioning.
Servo motors typically come in two types: continuous rotation and positioning. Positioning servos, which are the focus of this article, allow for precise control over the angle of rotation. This makes them ideal for applications such as controlling the movement of robotic arms, steering mechanisms in RC cars, and even camera gimbals.
Why Choose the Arduino Uno?
The Arduino Uno is one of the most popular microcontrollers for DIY electronics projects. With its open-source nature, user-friendly design, and large community, Arduino has become a go-to platform for hobbyists, students, and professionals alike. The Arduino Uno can easily interface with various sensors, motors, and other components, including servo motors.
The Arduino Uno is equipped with digital input/output pins and Pulse Width Modulation (PWM) support, making it an excellent choice for controlling servo motors. PWM allows the Arduino to send precise signals to the servo motor, controlling its movement accurately.
The Basics of Servo Motor Control with Arduino
When working with a servo motor, you typically need to send a signal in the form of a PWM (Pulse Width Modulation) signal to the motor. This signal controls the motor’s angle of rotation. The duration of the pulse determines the position of the servo, typically ranging from 0° to 180° for most standard servos.
The servo motor uses a potentiometer or an internal feedback system to determine its position and adjust itself to match the signal sent by the Arduino.
To begin controlling a servo motor with the Arduino Uno, you’ll need a few basic components:
Breadboard (optional, depending on your circuit setup)
External power supply (if needed, depending on the servo motor's power requirements)
Now that we understand the basics, let’s move on to writing the code to control the servo motor.
Arduino Code for Controlling a Servo Motor
Here’s a simple example to get started with controlling a servo motor using the Arduino Uno.
#include // Include the Servo library
Servo myServo; // Create a Servo object
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 (middle position)
delay(1000); // Wait for 1 second
myServo.write(180); // Move the servo to 180 degrees (maximum position)
delay(1000); // Wait for 1 second
The Servo.h library is used, which simplifies the process of controlling the servo.
The myServo object is created to represent the servo motor.
The myServo.attach(9) command tells the Arduino to use pin 9 to send the PWM signal to the servo.
The myServo.write() function is used to move the servo to specific angles (0, 90, 180 degrees in this case).
The delay() function ensures there’s a pause between movements.
Servo.h Library: This library handles all the low-level details of controlling a servo, allowing you to focus on higher-level tasks, like specifying angles.
Servo.attach(): This tells the Arduino which pin the servo is connected to. In this example, we’re using pin 9.
Servo.write(): This method tells the servo what angle to move to. The servo motor can rotate from 0° to 180°.
delay(): This function pauses the program for a set amount of time, allowing the servo to complete its movement before the next action.
Some servo motors require more power than the Arduino can provide through its 5V pin. In such cases, an external power supply is recommended. This can be a 5V battery pack or a regulated 5V power adapter. Be sure to connect the GND (Ground) of both the Arduino and the external power supply to avoid potential issues.
Expanding the Project: Servo Motor Control Using a Potentiometer
Let’s expand the project by adding a potentiometer. A potentiometer allows you to control the servo’s position manually. You can adjust the servo’s angle by turning the potentiometer’s knob.
Here’s an updated version of the code with a potentiometer:
int potValue; // Variable to store potentiometer reading
int angle; // Variable to store the calculated servo angle
myServo.attach(9); // Attach servo to pin 9
pinMode(A0, INPUT); // Set potentiometer pin as input
potValue = analogRead(A0); // Read the potentiometer value (0-1023)
angle = map(potValue, 0, 1023, 0, 180); // Map the potentiometer value to an angle (0-180)
myServo.write(angle); // Set the servo to the corresponding angle
delay(15); // Wait for the servo to reach the position
The analogRead(A0) function reads the potentiometer’s value, which ranges from 0 to 1023.
The map() function converts this value into a range suitable for the servo motor, which is 0 to 180 degrees.
The servo motor then moves to the corresponding angle, providing real-time control based on the potentiometer’s position.
Advanced Servo Motor Control and Projects
Using Multiple Servo Motors with Arduino Uno
One of the great features of Arduino is that it can control multiple servo motors simultaneously. If you want to control more than one servo, you simply need to create additional Servo objects and attach them to different pins.
Here’s an example where two servo motors are controlled at the same time:
Servo servo1, servo2; // Create two Servo objects
int angle1 = 0; // Initial angle for servo1
int angle2 = 180; // Initial angle for servo2
servo1.attach(9); // Attach servo1 to pin 9
servo2.attach(10); // Attach servo2 to pin 10
servo1.write(angle1); // Move servo1 to angle1
servo2.write(angle2); // Move servo2 to angle2
delay(1000); // Wait for 1 second
angle1 = (angle1 == 0) ? 180 : 0; // Toggle servo1's position
angle2 = (angle2 == 0) ? 180 : 0; // Toggle servo2's position
In this example, two servo motors are attached to pins 9 and 10. The program alternates the positions of both servos every second.
Controlling Servo Motors Using a Servo Controller Shield
For more advanced projects, you may want to use a servo controller shield. These shields allow you to control many servo motors through an I2C interface, freeing up the Arduino’s pins for other uses. Popular shields, like the Adafruit 16-Channel Servo Driver, can control up to 16 servo motors using just two wires (SCL and SDA for I2C communication).
This method is great for complex robotics projects that require multiple servo motors but only a limited number of pins on the Arduino.
Applications of Servo Motors with Arduino
Robotic Arms: Using multiple servos, you can build a robotic arm capable of performing tasks like picking up objects or drawing pictures.
Camera Gimbals: Servo motors are commonly used to stabilize cameras, adjusting their angles to compensate for movement.
RC Vehicles: Servo motors are used to control steering mechanisms and throttle in remote-controlled vehicles.
Automated Doors or Gates: Servo motors can open or close doors and gates in automation systems.
Whether you're building a simple hobby project or a complex robot, servo motors controlled by an Arduino Uno can provide the precision and flexibility you need. From basic position control to advanced multi-servo systems, this combination is a powerful tool for any electronics enthusiast. Experimenting with different setups, sensors, and input devices like potentiometers opens up countless opportunities for creativity in automation and robotics.
Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.