Published 2026-04-05
Setting aservomotor to a specific rotation angle is a fundamental task in robotics, automation,and DIY electronics. This guide provides you with the exact steps, code logic, and calibration methods to make any standardservorotate to any angle you need—without relying on any specific brand or product. You will learn the universal pulse-width principle, the step-by-step programming approach, and how to troubleshoot common angle errors. By the end, you will be able to set servo angles accurately and repeatedly.
All standard servos use aPulse Width Modulation (PWM)signal to determine their rotation angle. The control signal is a 50 Hz frequency (period = 20 milliseconds). Within each 20 ms period, a high pulse (the “on” time) tells the servo where to go.
1.0 ms pulse→ 0 degrees (full counter‑clockwise on most servos)
1.5 ms pulse→ 90 degrees (center position)
2.0 ms pulse→ 180 degrees (full clockwise)
These values are the industry standard. However, actual endpoints may vary slightly between individual servos. The table below shows the universal relationship:
> Key fact: The pulse width changes the angle linearly. For any angle between 0° and 180°, the required pulse width = 1.0 ms + (angle/180) × 1.0 ms.
Follow these four universal steps. No brand‑specific software or hardware is assumed.
A standard servo has three wires:
Brown or Black– Ground (connect to GND of your controller)
Red– Power (5V for most servos; check your servo’s voltage rating)
Orange or Yellow– Signal (connect to a PWM‑capable pin)
> Critical warning: Do not power a servo directly from a microcontroller’s 5V pin when under load. Use a separate 5V power supply capable of providing at least 1A per servo.
Configure your microcontroller or servo driver to produce a 50 Hz signal (20 ms period). Then set the pulse width according to your target angle.
Example calculation: To set 45°
Pulse width = 1.0 + (45/180)×1.0 = 1.0 + 0.25 = 1.25 ms
Example calculation: To set 135°
Pulse width = 1.0 + (135/180)×1.0 = 1.0 + 0.75 = 1.75 ms
The following logic works on any platform (Arduino, Raspberry Pi, ESP32, etc.):
Set PWM frequency = 50 Hz Set PWM resolution = 1 µs (microsecond) steps function setAngle(angle_degrees): if angle_degrees 180: angle_degrees = 180 pulse_width_us = 1000 + (angle_degrees / 180)1000 # pulse_width_us is between 1000 and 2000 Write PWM signal: period = 20000 µs, high_time = pulse_width_us
After uploading your code, observe the servo horn. If the horn does not move to the expected position, follow the calibration in Section 4.
Consider a simple robotic arm with three joints (shoulder, elbow, wrist). You want the elbow joint to move from 30° to 120° over 2 seconds.
Case steps:
1. Identify the servo for the elbow.
2. Write a loop that gradually increases the angle:
Start angle = 30° → pulse width = 1.0 + (30/180)×1.0 = 1.1667 ms
End angle = 120° → pulse width = 1.0 + (120/180)×1.0 = 1.6667 ms
3. Increment the angle by 1° every 20 milliseconds (50 steps per second).
![]()
4. Total duration = (120‑30) steps × 0.02 s = 1.8 seconds (approximately).
Result: The elbow moves smoothly from 30° to 120° without jerking. This method is used in thousands of hobbyist and educational robots daily.
Even with the correct 1.0–2.0 ms range, you may find that a command for 90° results in 85° or 95°. This is normal due to manufacturing tolerances. Calibrate each servo individually:
1. Command the servo to 0° (send 1.0 ms pulse).
2. Mark the actual position on the horn.
3. Command the servo to 180° (send 2.0 ms pulse).
4. Mark the actual position.
5. Measure the true angle range. For example, if the physical range is only 170°:
True minimum pulse = 1.0 ms (still works)
True maximum pulse = 2.0 ms gives 170° → to get 180°, you would need 2.058 ms.
6. Instead of altering the standard range, map your desired angle to the actual range:
actual_angle = desired_angle × (true_max_angle / 180)
Example: If true max angle = 170°, to get a desired 90°:
actual_angle = 90 × (170/180) = 85°→ send pulse for 85°.
This linear mapping ensures that the physical horn goes exactly where you want.
Avoid these errors to guarantee success:
Some servos are modified for continuous rotation. In that case, the pulse width no longer sets an absolute angle. Instead:
1.5 ms→ stop
(e.g., 1.3 ms) → rotate one direction at speed proportional to the difference
>1.5 ms(e.g., 1.7 ms) → rotate the opposite direction
For continuous rotation servos, “setting an angle” is not possible without a feedback sensor (encoder). Use a standard servo (0–180°) for absolute angle positioning.
Core principle repeated: Servo rotation angle is set by generating a 50 Hz PWM signal with a pulse width between 1.0 ms (0°) and 2.0 ms (180°). The relationship is linear.
Immediate action plan:
1. Connectyour servo to a dedicated 5V power supply (not your microcontroller’s 5V pin if more than one servo).
2. Generatea 50 Hz PWM signal on any GPIO pin using your preferred platform’s servo library or raw PWM.
3. Calculatethe pulse width:pulse_us = 1000 + (desired_angle/180)1000.
4. Testwith 0°, 90°, and 180°. Mark the actual positions.
5. Calibrateusing the mapping formula in Section 4 if the positions are off by more than 2°.
6. Gradually movebetween angles by changing the angle by 1–2° every 20–50 ms to avoid mechanical stress.
By following this guide, you can set any standard servo to any angle between 0° and 180° with ±1° accuracy. No brand‑specific tricks are needed—only the universal PWM standard that all servos obey.
Update Time:2026-04-05
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.