Home > Industry Insights >Gear Motor
TECHNICAL SUPPORT

Product Support

How to Control a Servo Motor: A Complete Guide to Principles and Programming

Published 2026-04-01

Controlling aservomotor precisely is a fundamental skill in robotics and electronics. This guide explains the core operating principle ofservomotors and provides clear, step-by-step programming instructions. You will learn how to generate the necessary control signal and write code to command aservoto any angle, ensuring your project moves exactly as intended.

1. Core Principle: The Control Signal

Every standard servo motor is controlled by a single signal wire usingPulse Width Modulation (PWM). The servo’s position is determined not by the voltage level, but by the width of a pulse sent every 20 milliseconds (50 Hz).

Pulse Width Range:The control pulse typically ranges from1.0 ms to 2.0 ms.

Angle Mapping:This pulse width maps directly to the servo’s angular position.

A1.0 ms pulsegenerally commands the servo to0 degrees.

A1.5 ms pulsecommands thecenter (90 degrees)position.

A2.0 ms pulsecommands thefull sweep (180 degrees) .

Common Scenario:In a robotic arm, a servo controlling the gripper needs to close tightly. By sending a consistent 1.0 ms pulse, the arm reliably closes to the same position every cycle. If the pulse drifts even slightly, the gripper may fail to hold the object.

2. Hardware Setup: Simple Connections

Before writing code, connect your servo to a microcontroller (like an Arduino or similar). All standard servos use a 3-wire interface. Confirm the wiring against your servo’s datasheet; errors here are the most common cause of failure.

Wire Color (Typical) Function Connection
Brown or Black Ground (GND) Connect to the system’s common ground.
Red Power (Vcc, 4.8V–6V) Connect to an external 5V power supply.Do not power a servo directly from a microcontroller’s 5V pin unless it is a very small micro-servo.
Orange or Yellow Signal (PWM) Connect to a PWM-capable digital pin (e.g., pin 9).

Common Scenario:A beginner building a remote-controlled car often connects the servo directly to the microcontroller’s 5V pin. During testing, this works, but under load, the microcontroller resets repeatedly. The solution is always to use a separate, adequate power supply for the servo, ensuring the microcontroller and servo share a common ground.

3. Programming the Control Signal

The core of the program is generating the precise 1.0 ms to 2.0 ms pulse every 20 ms. While you can write low-level code to toggle a pin, using a library is the most reliable and efficient method for most projects.

Method 1: Using a Standard Servo Library

This is the recommended method for beginners and most applications. The library handles all the complex timing in the background.

#includeServo myServo; // Create a servo object void setup() { myServo.attach(9); // Attaches the servo on pin 9 } void loop() { myServo.write(0); // Command to 0 degrees delay(1000); // Wait 1 second myServo.write(90); // Command to 90 degrees delay(1000); // Wait 1 second myServo.write(180); // Command to 180 degrees delay(1000); // Wait 1 second }

Explanation:

#include: Imports the library.

myServo.attach(pin): Tells the microcontroller which pin to use for the signal.

myServo.write(angle): The simplest way to command a position. The library automatically converts the angle (0-180) to the correct pulse width (1.0-2.0 ms).

Method 2: Direct Pulse Width Control (For High Precision)

UsingmyServo.write()is convenient, but the relationship between the angle and the pulse width is not always perfectly linear. For applications requiring exact positioning, such as a camera stabilization gimbal, you can directly set the pulse width in microseconds.

Common Scenario:A gimbal for an action camera must be perfectly level. If the servo library’s mapping is slightly off, the camera will have a constant tilt. By using thewriteMicroseconds()function, you can manually calibrate the exact pulse widths needed for a perfect 0-degree and 180-degree position.

#includeServo myServo; void setup() { myServo.attach(9); } void loop() { // Directly command the pulse width in microseconds myServo.writeMicroseconds(1000); // 1.0 ms -> 0 degrees delay(1000); myServo.writeMicroseconds(1500); // 1.5 ms -> 90 degrees delay(1000); myServo.writeMicroseconds(2000); // 2.0 ms -> 180 degrees delay(1000); }

4. Advanced: Smooth Motion

For natural movement, a servo should not snap instantly to a new position. Instead, it should move smoothly. This is achieved by incrementally changing the commanded angle with short delays.

Common Scenario:A robot head that turns to look at a person. If the servo instantly snaps to the new position, the movement looks unnatural and mechanical. Using a smooth motion algorithm makes the robot appear more intelligent and lifelike.

#includeServo myServo; int pos = 0; // Variable to store the current position void setup() { myServo.attach(9); } void loop() { // Sweep from 0 to 180 degrees, 1 degree at a time for (pos = 0; pos = 0; pos -= 1) { myServo.write(pos); delay(15); } }

5. Troubleshooting Common Issues

Even with correct code, issues can arise. Here are the most frequent problems and their solutions based on real-world experience.

Problem Most Likely Cause Solution
Servo does not move or jitters. Insufficient power supply. Use a dedicated external power supply. Ensure the ground of the power supply is connected to the ground of the microcontroller.
Servo moves to the wrong angles. Incorrect pulse width calibration or library assumptions. UsewriteMicroseconds()and manually calibrate the minimum and maximum pulse values for your specific servo.
Servo is unresponsive but was working. Damaged signal pin or wiring. Test the servo with a simple sweep sketch. If it works, the issue is in your new code. If it doesn't, inspect the wiring and try a different signal pin.
Continuous rotation servo moves, butwrite(90)does not stop it. This is a continuous rotation servo, not a standard position servo. For continuous rotation servos, the pulse width controls speed and direction. 1.5 ms is stop, 1.0 ms is full speed one direction,2.0 ms is full speed the opposite direction.

Conclusion: Key Principles for Success

To reliably control any servo motor, always remember the three core principles:

1. Signal is key:The servo’s position is determined solely by the 1.0 ms to 2.0 ms pulse width within a 20 ms frame.

2. Power is separate:A microcontroller is a logic device; a servo is a power device. Always use a dedicated power supply for the servo.

3. Libraries simplify:Use a well-established servo library for quick setup, and switch to direct microsecond control when you need high precision.

Actionable Next Steps:

1. Start with a sweep:Wire a single servo to your microcontroller using an external power supply and run the sweep code. This verifies your hardware setup and fundamental control.

2. Calibrate your servo:Use thewriteMicroseconds()function to find the exact pulse width for your servo’s 0° and 180° positions and note these values.

3. Incrementally build:Add one servo at a time to your project. Master control of a single actuator before moving to multi-servo coordination to simplify troubleshooting.

Update Time:2026-04-01

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.

Mail to Kpower
Submit Inquiry
+86 0769 8399 3238
 
kpowerMap