Published 2026-03-03
Don’t understand the relationship between PWM andservoangle? Seeing theservonot turning or turning randomly, I felt anxious. Don't worry, we'll break it down and explain it clearly today, so that you not only know how to adjust it, but also understand the principles behind it.
Many friends have encountered this situation when they played with servos for the first time: after the wires are connected and the program is burned in, the servos just won’t move. This is usually because you don't understand what the PWM signal is. To put it simply, there is a small motor in the steering gear and a set of control circuits. It does not look at the voltage or current, but only recognizes a special pulse signal, which is PWM.
This signal is like a strict commander, sending a command to the steering gear every 20 milliseconds (that is, a frequency of 50Hz). The width of this command, that is, the duration of the high level, directly determines how much the servo rotates. Your signal is either of wrong frequency or wrong pulse width. The servo cannot understand it and will naturally ignore you.
We need to clarify this core relationship clearly. The pulse width of a standard servo control signal is generally between 0.5 milliseconds and 2.5 milliseconds. You can think of it like a time scale. When the high level lasts for 1.5 milliseconds, the output shaft of the servo will stop at the middle position, which is 90 degrees.
If the high level time is shortened to 0.5 milliseconds, the servo will turn to the far left, usually 0 degrees. On the contrary, if it is extended to 2.5 milliseconds, it will turn to the far right, which is 180 degrees. Therefore, controlling the steering gear angle is essentially to accurately control the duration of this high level, which is what we often call the "duty cycle".
After understanding the relationship between pulse width and angle, we have to figure out how to get this precise time. We use a 50Hz signal with a period of 20 milliseconds. To get a high level of 1.5 milliseconds, the duty cycle is 1.5 divided by 20, which equals 7.5%. Similarly, 0.5 milliseconds corresponds to a 2.5% duty cycle, and 2.5 milliseconds corresponds to a 12.5% duty cycle.
This calculation is crucial when programming. For example, if you use it, its function outputs a value from 0 to 255, corresponding to a duty cycle of 0% to 100%. You need to convert 7.5% into the corresponding value, which is about 19. If the calculation is not accurate, the servo will not be able to turn to the precise position you want.
Now that the theory is clear, let’s start writing it down. Taking the most common one as an example, it is not possible to use it alone because its default frequency is not 50Hz. We need to use the Servo.h library, which can help us handle all complex PWM calculations. You only need to write .(9), connect the signal line to pin 9, and then write .write(90), and the servo will turn to 90 degrees.
If you are using other development boards, such as STM32, the principle is the same. The key is to find the function that controls the PWM frequency and pulse width. The core idea is: configure the timer, set the PWM frequency to 50Hz, and then change the value of the comparison register to adjust the pulse width to control the angle.
Servo vibration is a troublesome problem. There are two most common reasons. First, the power supply is insufficient. The servo requires a relatively large current to rotate. If the USB port of your development board has insufficient power supply, it will cause voltage instability, signal disorder, and the servo will naturally shake. The solution is to connect a separate external power supply to the servo, and connect the ground wires of the development board and the servo to the same ground.
Another reason is signal interference. If your control lines and motor drive lines are tangled together, or the PWM signal itself is not stable enough, it can also cause jitter. Check the wiring, try to keep the signal lines away from high-current lines, and ensure in the code that the timer interrupt generated by the PWM signal is not frequently interrupted by other programs.
If you feel that the standard 180-degree servo is not enough and want to play something cooler, then you have to look at the 360-degree continuous rotation servo or use a brushless motor with an ESC. Although their control principle is still PWM, the logic is different. For a 360-degree servo, a 1.5ms pulse width makes it stop. If it is less than 1.5ms, it will turn in one direction. If it is greater than 1.5ms, it will turn in the opposite direction. The more the pulse width deviates, the faster it will turn.
This gives you more creative space, and you can easily make a car or robot wheel. However, it should be noted that this type of steering gear cannot accurately control the angle, but can only control the speed and direction. If your project requires precise positioning, such as a robotic arm, you still have to use standard servos.
I hope what I talked about today can help you thoroughly understand PWM and servo control. What other weird problems have you encountered when adjusting the steering gear? For example, have you ever encountered a situation where the servo reverses or becomes extremely hot? Welcome to share it in the comment area, let’s discuss and solve it together! If you find the article useful, don’t forget to like it and share it with your friends who are also playing.
Update Time:2026-03-03
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.