Published 2026-05-08
late at night. The lab lights are still on.
I stared intently at the motionless servo, holding the STM32 development board tightly in my hand. The code compilation had passed and the download was successful, but why was it motionless?
The debugger flickered on the screen, as if laughing at me.
Have you ever been like this, looking through the datasheet repeatedly, but you can't find that line of configuration that plays a key role?
That night four years ago, I used STM32 to control the servo for the first time. I think PWM is just as easy as changing the duty cycle. As a result, the servo trembled, and then became completely silent.
Later I realized that I forgot the most critical step: enabling the clock.
Writing Tips: Writing Drivers from Scratch
The servo is an obedient child. It only requires one language: PWM.
The period is 20 milliseconds, and the high-level time is between 0.5 milliseconds and 2.5 milliseconds, which is related to the standard servo.
There are a large number of STM32 timer modes that can be selected. What is the difference between PWM1 and PWM2? Counting upwards and counting downwards, does the servo really care about these situations?
There are three servos at different price points that I tested on a project. One of the entry-level servos has a good response performance of 0.5ms. But another industrial-grade servo has a requirement to make the dead time accurate to 1us.
There is a case like this: In the past year, I helped a friend debug a five-axis robotic arm. What is used is the common medium torque servo.The code seems to be in perfect condition. When the duty cycle is adjusted from 5% to 10%, the servo just jumps at two extreme positions.。
Calculation of prescaler and auto-reload values.
For STM32 in the 72MHz state, if you want to get a PWM frequency of 50Hz, the frequency division coefficient is not determined arbitrarily. Each servo has different sensitivity, different dead zones, and even different power supply voltages, which will cause its "hearing" to change.
I've seen too many people connect the servo directly to the 3.3V PWM pin.
Then complained: "STM32 driver capability is too weak."
No, you forgot to quarantine.
Writing prompts: Understand the deeper meaning of the datasheet
The debugging lasted for as long as three hours. I used an oscilloscope to investigate wire by wire. During the investigation, I found that when the servo rotated, the current surged to 1.2A. Then I realized that the 3.3V voltage regulator of the development board could not withstand such a current.
The solution is simple: external power supply, common ground.
![]()
Since then, every steering gear project of mine has followed three iron rules:
Separate power supply for servo (4.8V-6V)
Control signal string 1kΩ resistor
The power ground wire is reliably connected to the STM32 ground wire.
These three items are worth all the debugging time.
But power supply alone is not enough. Precision is the soul of servo control.
What about regular delay-like functions? Forget it, give it up decisively. What about interrupts caused by timers? It's okay and usable.The operation method that can truly be called a professional is to use the advanced timer of STM32 to output complementary PWM and turn on the braking function.。
Why? Because when the servo gets stuck, you don't want it to burn out.
Open the STM32 reference manual. Chapter 14 belongs to timers, Chapter 15 belongs to timers, Chapter 16 belongs to timers, and Chapter 17 belongs to timers.
Beginners get dizzy looking at register lists. Me too.
But there is a page, which has the most important property: the configuration table of the output comparison mode. It can tell you which register controls the duty cycle, which register controls the polarity, and which register controls the dead zone.
Writing Prompt: The Importance of Dead Time
My next project requires a servo angle accuracy of 0.5 degrees. Most servos on the market generally only support a resolution of 1us.; In a 20ms period, 1us corresponds to exactly 0.18 degrees. From a theoretical analysis, this accuracy is sufficient.
However, actual testing found that the return difference of cheap servos is only 2us.
what to do?
Software Compensation.
In the code, I built a mapping table to associate the theoretical duty cycle with the actual angle according to the corresponding relationship. I used linear interpolation to correct it, and the final accuracy reached 0.3 degrees.
This case tells us: Hardware has limitations, but algorithms can remedy it.
Q/A
Q: The servo doesn’t move at all, what should I do?
A: First check the power supply voltage and common ground. Use an oscilloscope to see if the PWM signal is present.
Q: The servo vibrates seriously, what’s the reason?

The period is 20ms and there is no glitch in the signal, but the PWM frequency does not match or the duty cycle is not stable and needs to be confirmed.
Q: What should I do if the PWM resolution of STM32 is not enough?
A: Reduce the timer clock, or use 16-bit mode, and you can also connect an external PCA9685 module.
Q: How to control multiple servos to move at the same time?
A: Use multiple channels of the same timer to output PWM to avoid interrupt conflicts.
Many people finish writing the code and call it a day.
But a steering gear is a mechanical device. It has its own temper.
Writing prompts: Physical limits and safety protection
In the past, I designed an automatic door lock in which a servo rotated ninety degrees to push the deadbolt. At that time, the program was tested as many as a hundred times, and the results of each test showed normal. However, on the third day after installation, the lock became stuck.
When the servo continues to be blocked, the temperature of the internal driver chip rises to 120 degrees.
What's the lesson?
For every steering gear project, timeout protection and current detection should be added. The STM32 ADC can monitor the steering gear current in real time. If the current exceeds the threshold and continues for 0.5 seconds, the PWM output will be cut off immediately.
This isn't over-engineering. This is the watershed between professionals and amateurs.
There is also a blind spot: the initial position of the servo.
If your robotic arm is in a dangerous position when the power is off, the STM32 will output the default level the moment it is powered on. If this level happens to be the maximum duty cycle, the servo will slam into the limit.
First, the solution is to configure the timer output pin to a high-impedance state before the GPIO is initialized.Secondly, the solution is to add a hardware enable signal and wait until the system is stable before powering on the servo.。
The datasheet will not tell you these details.
The STM32 development board in your hand can do more than just light up LEDs.
It allows the servo to draw circles, write, grab objects, and adjust valves.
But the premise is that you are willing to take the time to understand the origin of each parameter.
Writing Prompt: Closed-loop feedback is the ultimate solution
If you really pursue high accuracy, give up on open-loop control.
I devoted myself to a bionic robot project and added potentiometer feedback to each joint. STM32 was responsible for reading the ADC value, comparing it with the target angle, and then using the PID algorithm to adjust the PWM. In this way, no matter how the load changes, the position is always accurate.
Open-loop control is like walking blindfolded. Closed-loop control is like walking with your eyes open.
Which one do you choose?
At this moment, look back at the laboratory in the middle of the night. The steering gear did not rotate, not because there was an error in the code, but because there was a blind spot in my knowledge that I had not yet completely mastered.
Every failure is a learning experience.
If you want to use STM32 to really control the servo, remember these steps:
Read the servo data sheet carefully to confirm the pulse width modulation period and pulse width range.
Use an oscilloscope to verify the waveform output by the STM32, do not trust the calculation.
Independent power supply, common ground, plus protective resistor.
Angle mapping and timeout protection are added to the software.
Test extreme conditions: power outage and restart, blocked rotor, signal interference.
Writing prompts: Repeat key ideas and act on them
There is only one core: accuracy is hidden in documents, and stability comes from testing.
Check your code today to see if the prescaler value is calculated correctly, see if it is grounded, and see if the pin state is safe at the moment of power-on.
Re-program.
Listen to the sound of the steering gear turning.
It no longer trembles.
It's working for you.
In the laboratory late at night, the lights are still on.
But this time, you're smiling.
Update Time:2026-05-08
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.