Published 2026-05-06
Have you ever encountered this situation?
After the servo is connected to the power supply, there is no response. After writing the program, random jitters occurred. I obviously followed the instructions, but I just couldn't turn it.
Don't panic. You are not alone.
In this article today, with the help of the 51 microcontroller, we will thoroughly explain the control of the servo. There is no complicated mathematical content, no incomprehensible register table, only the most direct ideas and the most practical code framework.
Remember this key point: if the servo remains stationary, it is largely due to a timing problem; if the servo moves randomly, it is probably caused by insufficient power supply.
Are you ready?
01 What exactly is a steering gear?
Many people get a servo for the first time and think it is a motor. It can be turned on when the electricity is turned on.
Wrong.
The steering gear is not an ordinary motor. It's more like a little robot with a "brain".
You give it a command and it turns to the specified angle. Then stop. Stop dead in your tracks.
How is this achieved with the small potentiometer and control board inside? When you send out a pulse, it makes some gestures and locks once it reaches the position.
Therefore, for controlling the steering gear, there is no need to write PID or perform closed-loop calculations. You just need to learn one thing: send a pulse.
02 Core principle: One number can turn the angle
Open your data sheet. Find this sentence: The high-level pulse width corresponds to the angle.
In simple terms: a high level of 0.5 milliseconds will turn to 0 degrees, if it lasts 1.5 milliseconds, it will turn 90 degrees, and if it lasts 2.5 milliseconds, it will turn 180 degrees.
The period is fixed at 20 milliseconds.
Is it clear? Controlling the servo actually means controlling an IO port, pulling it up, waiting for a period of time, then pulling it down, and then waiting for the remaining time.
It's that simple.
For example, the pulse that rotates 90 degrees:
First, perform an upward pull-up operation, then perform a delay lasting 1.5 milliseconds, and then perform a downward pull-down operation, and then perform a delay lasting 18.5 milliseconds, and then repeat the above steps in an infinite loop.
This is a complete control cycle.
03 Before writing a program, set up the environment first
You don’t need fancy equipment.
A 51 microcontroller (STC89C52 is enough)

A servo (common SG90 or MG995 will do)
A few Dupont wires
There is a 5 V power supply, so don't expect it to be powered by a microcontroller. Let me say important things three times, don't expect it to be powered by a microcontroller.
Many people's servos shake and don't turn properly. It's not a program error, but insufficient power.
When the servo is at the moment of startup, the current can reach more than 1A, and the small amount of power drawn by the USB cannot fully feed it.
what to do? External power supply. Common ground. Common ground. Common ground.
The positive terminal of the power supply should be VCC, and the negative terminal of the power supply should be GND. The GND of the microcontroller must also be connected to the power supply GND, and then the signal line needs to be connected to the IO port of the microcontroller.
Is the circuit connected? Congratulations. The hardest half is completed.
04 Hands-on coding: start from the simplest
We don't play fake. Directly give the code that can be run.
Key points: Use a timer to generate an interrupt with a period of 20ms, and flexibly control the duration of the high level during the interrupt.。
Idea:
Define a variable to store the currently required high-level time, just like that 15, and this 15 represents 1500 microseconds, which is 1.5ms。
At the beginning of each 20ms period, first pull the IO port high.
Delay high level time.
Pull down the IO port.
Delay the remaining time.
Written as pseudo code:
void main() { Initialize timer (set 20ms interrupt); while(1) { // Do nothing, rely on interrupts } } void timer interrupt () interrupt 1 { Pull high; delay (high level time); pull low; delay (20ms - high level time); }
Important: Precise microsecond-level delays require the use of delay functions. When the main frequency of 51 microcontroller is 11.0592MHz, one nop is approximately equivalent to 1.085 microseconds. To write a delay_us function, the function needs to pass in the number of microseconds and implement it by subtracting it through a while loop.
Don't be afraid of trouble. This kind of successive approximation debugging is the norm in embedded systems.
05 Let the rudder move: forward, reverse, any angle
Now you need to achieve: press button 1, turn to 0 degrees; press button 2, turn to 90 degrees; press button 3, turn to 180 degrees.
Add key detection in pseudo code:
if (button 1 is pressed) high level time = 500; // 0.5ms -> 0 degrees if (button 2 is pressed) high level time = 1500; // 1.5ms -> 90 degrees if (button 3 is pressed) high level time = 2500; // 2.5ms -> 180 degrees

Important: The high level time unit is microseconds. 500, 1500, 2500.
Don't write 1, 2, 3. That's milliseconds. 1000 times worse.
Then what? Then the servo will obediently turn to the corresponding angle.
Don’t believe it? You try.
06 Pitfalls you may have stepped on (Q/A)
Q: The servo doesn't turn, but just buzzes?
First, A, there is insufficient power supply current, or the pulse width is inaccurate. It is necessary to change the power supply to a power supply above 5V/2A, and then check whether the high level time is between 500 and 2500us.
Q: The servo can turn but it shakes badly?
Add a large capacitor (value 470uF) to both ends of the servo power supply because of the situation mentioned in A, that is, there is an excessive difference between two adjacent control cycles or the power supply has ripples.
Q: There is no response when the program is burned in, but it turns aside when the signal cable is unplugged?
A: The level state of the IO port at the beginning is uncertain, or the period of time when it is at high level exceeds the specified range. When powering on the device, you must first pull the level of the IO port low, and then give it the initial value.。
Q: Want to control continuous rotation (such as 360-degree servo)?
If this doesn't work, you need to replace it with a servo that can rotate continuously 360 degrees. If the high level is 1.5ms, the servo will stop rotating. If it is higher than 1.5ms, the servo will rotate forward. If it is lower than 1.5ms, the servo will rotate reversely.
Q: If I don’t have an oscilloscope at hand, how can I confirm that the pulses are correct?
Program the servo to alternate between 0 degrees and 90 degrees every 1 second. Use a logic analyzer or a simple LED method to observe whether its swing is regular.
07 Upgrade: Let the servo move according to the curve you draw
A program with hard-coded angles is too elementary.
A more advanced way to play: let the servo slowly go from 0 degrees to 180 degrees, and then slowly come back.
How to do it?
Use a loop variable called angle to increase from 0 to 180, and the step value should not be too large. For example, increase by 1 degree each time.。
For each increment of 1 degree, recalculate the high level time:
The high level time is equal to 500 plus the angle multiplied by 2000 divided by 180.
It should be noted that 500 corresponds to 0 degrees, 2500 corresponds to 180 degrees, and the total range is 2000 microseconds, which is divided into 180 equal parts.
Then delay 20ms. Do it again.
The effect is a smooth swing. Like a robot waving.
You can call this something called "servo kinematics", but don't be scared by its name. In essence, it is a kind of linear interpolation.
08 Go one step further: Use the serial port to remotely control the servo
Add serial communication. The computer sends a number and the servo turns to the corresponding angle.
For example, pronounce "0" and turn 0 degrees. Say "90" and turn 90 degrees.
Key points:
In the microcontroller serial port receiving interrupt, the string is parsed.
Note: You may not be able to collect everything at one time. To determine whether a newline character is received.
After receiving the complete number, the range is limited to 0~180.
Convert to high level time and update the variable.
In this way, your servo becomes a programmable actuator. After connecting to the Bluetooth module, even your mobile phone can control it.
09 Summary: Three elements for controlling the steering gear
power supply. Provide sufficient current and stabilize the voltage to the common ground.
Timing. Period 20ms, high level 500-2500us.
Don't perform blocking operations, use timer interrupts instead of using delays that keep waiting, otherwise you won't be able to do anything.
Once you master these three things, you can control 99% of the servos on the market.
Remember: when writing a program, think one more step about "what will happen if the power supply suddenly jitters" instead of "it is enough for the program to run smoothly".
10 Now, it’s your turn to take action
Don't just regard this article as a collection. Turn on your Keil, connect the development board properly, and type the above code once.
There is a high probability that the first three times will not turn. Don't be in a hurry. Take a multimeter to measure the voltage, and use a logic analyzer to capture the waveform. If there is really no other way, try changing the servo.
Every time you solve a problem, your understanding of embeddedness deepens.
Looking back a few years later, you will find that the day you mastered the steering gear was the starting point for you to truly understand "controlling the physical world through code"。
The buzzing little servo in your hand is the first key to the world of robots.
Don't wait any longer. Power up now.
Give it a try.
Update Time:2026-05-06
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.