Published 2026-03-18
Hey, friends who have doneservoprogramming must have encountered this obstacle: you want theservoto turn 45 degrees, but it jumps to 90 degrees instead; you want the mechanical arm to slowly lift up like a human hand, but it "swish" into place, which is scary. To put it bluntly, you just don’t know how to make theservomove smoothly and accurately according to the angle you want. Don’t worry, today we will break it apart and talk about how to program the servo to achieve smooth changes at any angle.
If you want to control the servo as you wish, you must first understand who it listens to. There is a small motor inside the steering gear plus a feedback system consisting of a set of gears and a potentiometer. It only recognizes a signal called PWM (Pulse Width Modulation). You can think of this signal as a "time length" command sent to the servo.
The specific method of this command is to output a high-level pulse to the signal line of the servo in a fixed period (usually 20 milliseconds) through programming. The width of this pulse, that is, the duration of the high level, directly determines where the servo output shaft stops. Changing the width of this pulse in the program can make the servo rotate to any angle you want.
As mentioned earlier, the PWM signal is actually a square wave. You can think of it as a very punctual switch that quickly turns on and off every 20 milliseconds. The key lies in the duration of each turn on, which is the pulse width. For most servos, a pulse of 1 millisecond corresponds to 0 degrees, 1.5 milliseconds corresponds to 90 degrees, and 2 milliseconds corresponds to 180 degrees.
So, if you want the servo to turn to 45 degrees, you need the program to generate a pulse of about 1.25 milliseconds. Whether this signal is generated quickly or accurately directly determines the smoothness and position accuracy of the steering gear. Fortunately, current mainstream microcontrollers, such as STM32, have ready-made library functions to help us accurately generate these signals.
This relationship is actually a one-to-one correspondence between "pulse width" and "rotation angle". To put it simply, the servo will turn to the corresponding angle according to the width of the pulse. There is usually a linear relationship between them. You can think of it like twisting a faucet. The angle of twisting (pulse width) determines the size of the water flow (server angle).
However, please note that for different brands and models of servos, this correspondence may be slightly different. For example, in some servos, a pulse of 0.5 milliseconds corresponds to 0 degrees, and a pulse of 2.5 milliseconds corresponds to 180 degrees. So before you start programming, it is best to look at the technical information of the servo and confirm its pulse width range, so that you can hit where you point it.
It’s not difficult to say that. Today’s programming environment is already very friendly. For example, it can be done with just a few lines of code. You only need to include theServo.hlibrary, define a servo object, then use()to bind the pins, and finally use thewrite(angle)function to fill in the angle you want to turn (such as 117 degrees), and the servo will turn obediently.
If you want to achieve a smooth effect from 30 degrees to 150 degrees slowly, you need to use a loop. Let the angle value start from 30, increase it a little bit each time, such as 1 degree, and then callwrite()function once, adding a small delay in the middle, such as 15 milliseconds. In this way, the servo will move step by step, and it will appear to be a continuous and smooth movement.
️ Simple steps to achieve smooth movement:
1. Use aforloop to increase angleifrom the starting value to the target value.
2. In the loop, use.write(i)to set the current angle.
3. Add a short delay, such asdelay(15), to control the movement speed.
This is of great use! It’s at the core of almost any maker project that wants to make something move. The most typical example is making robots. For example, if you want a six-legged robot to lift one leg gracefully, you need to control the three joint servos on its legs to move in coordination with a specific time and angle.
Another example is making a camera gimbal on a smart car. When the car detects the target, the gimbal needs to rotate the servo smoothly so that the camera is always aimed at the target instead of jumping one by one. There is also a robotic arm to grab items. You need to precisely control the angle of each joint so that the end of the robotic arm draws a beautiful arc to avoid obstacles and accurately grab things.
When you are ready to go big and start purchasing a servo, there are a few parameters that you should pay attention to. The first is torque, which determines how powerful the steering gear is. The unit is usually kg·cm. If the project you want to do is very heavy, such as lifting a robotic arm, then you need to choose one with larger torque.
The second is speed and accuracy. Speed is expressed in "seconds/60 degrees", which means how many seconds it takes to turn 60 degrees. Accuracy is related to whether the servo can stop at the precise position you want. For ordinary entry-level projects, ordinary analog servos are enough; if you have high requirements for accuracy and response speed, you can consider digital servos. It is recommended that you go to the official websites of these professional manufacturers first and look at the servo parameter comparisons and tutorials they provide, which can help you avoid many detours.
Have you ever encountered any particularly strange phenomena when you were controlling the steering gear yourself? For example, the servo shakes, heats up, or doesn't move at all? Come to the comment area to talk about your experiences, and let’s share and avoid pitfalls together! If you find this article useful to you, don’t forget to like it and share it with more friends~
Update Time:2026-03-18
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.