Published 2026-03-10
I see that during theservodebugging process, do you often get headaches about the PWM value corresponding to that angle? The angle has been calculated accurately, but the motor cannot rotate to the specified position, or it keeps shaking. In fact, there is a simple mathematical formula at work behind this. Once you understand it, you can accurately control every degree of theservo.
To put it bluntly, the function of theservoPWM calculation formula is to help you establish a one-to-one correspondence between "angle" and "pulse width".
The commonly used servo control signal is 50Hz PWM with a period of 20ms. Within this period range, the width of the high level is generally between 0.5ms and 2.5ms, and the angle range corresponding to this width interval is 0° to 180°.
The calculation formula can be written as:target pulse width = minimum pulse width + (angle/180°) × (maximum pulse width - minimum pulse width). This formula clearly shows how to calculate the target pulse width based on specific parameters. For example, in the context of servo control, if your servo 0° corresponds to 0.5ms and 180° corresponds to 2.5ms, then calculate the 90° position through this formula and the result will be 1.5ms. This calculation process is of great significance in the correspondence between the steering gear angle and the pulse width, and can accurately determine the pulse width value required by the steering gear at different angles.
Knowing the pulse width is not enough, because the duty cycle register usually needs to be configured in the microcontroller. Duty cycle = target pulse width/20ms. For example, if you need a pulse width of 1.5ms, the duty cycle is 1.5 / 20 = 0.075, which is 7.5%.
Specifically implemented in the code, for example, if you choose STM32 and want to set the auto-reload value to 2000 (this value represents 20ms), then the comparison value needs to be set to 150 (this value represents 1.5ms). This process actually converts the time ratio into a register value to ensure that the hardware can output an accurate waveform.
In actual code operation, taking STM32 as an example, when you set the auto-reload value to 2000 (which represents 20ms), the comparison value must be set to 150 (which represents 1.5ms). This process of converting time proportions into register values plays a key role in ensuring that the hardware outputs accurate waveforms.
This is most likely the easiest trap to fall into. Different brands and models of servos, or even servos of the same model but different batches, have different PWM ranges. The corresponding PWM value of some servos when in the 0° state is 0.5ms, while others are 0.6ms; when in the 180° state, the corresponding PWM value of some servos may be 2.5ms, while others may be 2.4ms.
Therefore, when using the servo, be sure to understand its specific PWM range in advance, otherwise it is easy to cause various problems due to parameter mismatch, affecting the normal use of the servo and the operation of related equipment.
The safest way is to check the official data sheet of the servo. If you cannot find the manual, it is recommended that you measure it yourself: first slowly increase the pulse width to find the minimum value at which the servo starts to rotate, and then find the maximum value at which the servo stops rotating. Substitute these two values obtained through actual measurements into the formula, so that the servo can accurately point to the target position and prevent the motor from being damaged due to excessive stress.
The most direct consequence of inaccurate calculations is "nonlinearity". For example, when you expect the servo to rotate to 45°, the actual situation is that it only rotates 40°. What's more serious is that once the pulse width exceeds the allowable range of the servo, for example, if a pulse width of 2.6ms is given, the limiter inside the servo will be stuck hard, causing the servo to overload, generate heat, and even burn the driver chip.
On the other hand, if the pulse width range is too narrow, for example, only 1ms to 2ms is used, then the actual rotation range of your servo may only be 120°, which wastes its mechanical performance. Therefore, the two endpoint values in the formula must accurately correspond to the physical limits of the steering gear.
When writing code, we usually encapsulate the formula into a function. The input is the angle you want, and the output is the comparison value of the timer. The steps are simple:
1. First define two constants:and, corresponding to the measured 0° and 180° pulse widths.
2. Then use the formula:= + (angle / 180.0) * ( - ) .
3. Don’t forget to convert the floating point result into an integer and assign it directly to the capture compare register of the timer.
In this way, every time the function is called, the servo can smoothly turn to any angle you want.
If you are working on a robotic arm or visual tracking project that requires high accuracy, you may consider introducing closed-loop control. The above open-loop formula can only guarantee "how much pulse width is given, how much angle to turn", but if the servo is blocked due to force, the position will be lost.
At this time, you can add a potentiometer or encoder to the servo output shaft to read the actual angle in real time, and then use the PID algorithm to dynamically adjust the PWM value. In this way, even if there is external interference, the system can automatically pull the steering gear back to the target position, truly achieving precise control.
I wonder if you have ever encountered a situation where the swing range of both sides of the entire mechanism is asymmetrical due to the inaccurate servo median value (90°) in actual projects? Welcome to chat about your debugging experience in the comment area. If you find it useful, don’t forget to give it a like and share it with more friends.
Update Time:2026-03-10
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.