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.
伪代码里加按键检测:
if(按键1按下)
高电平时间 = 500; // 0.5ms -> 0度
if(按键2按下)
高电平时间 = 1500; // 1.5ms -> 90度
if(按键3按下)
高电平时间 = 2500; // 2.5ms -> 180度

重点:高电平时间单位是微秒。500、1500、2500。
不要写成1、2、3。那是毫秒。差1000倍。
然后呢?然后舵机就会听话地转到对应角度。
不信?你试试。
06 你可能踩过的坑(Q/A)
Q:舵机不转,只是嗡嗡响?
先是A,存在电源电流不足的情况,或者脉宽不准确。需要换5V/2A以上的电源,接着要检查高电平时间是不是在500到2500us之间。
Q:舵机能转但抖得厉害?
将舵机电源两端加上一个大电容(值为 470uF),原因在于 A 所提及的情况,即相邻两次控制周期存在过大差异或者电源带有纹波。
Q:程序烧进去没反应,但拔掉信号线就转到一边?
A:IO 口刚开始时的电平状态是不确定的,或者处于高电平的那段时间超出了规定的范围,在给设备通电的时候,要率先把 IO 口的电平拉低,之后再赋予它初始值。
Q:想控制连续转动(如360度舵机)?
这样不行,得换那种能360度连续旋转的舵机。要是高电平是1.5ms,舵机就会停止转动,要是高于1.5ms舵机就会正向转动,要是低于1.5ms舵机就会反向转动。
Q:手头没有示波器,怎么确认脉冲对了?
将舵机编程为每间隔1秒就交替处于0度与90度状态,通过逻辑分析仪方式或者简易LED的方法,来观察其摆动是不是具有规律。
07 升级:让舵机按你画的曲线运动
写死角度的程序,太初级了。
高级一点的玩法:让舵机慢慢从0度走到180度,再慢慢回来。
怎么做?
利用一个被称作angle的循环变量,使其从0开始进行增加,一直增加到180,并且增加的步进值不要太大,比如说按照每次增加1度这样的方式来进行增加。
每增加1度,重新计算高电平时间:
高电平的时间,等于500加上,角的度数乘以,2000再除以180。
需说明的是,500所对应的是0度,2500所对应的是180度 ,总的范围是2000微秒 ,是将其平均分成180份。
然后延时20ms。再来一次。
效果就是平滑的摆动。像机器人挥手一样。
你能够将这个称作是名叫“舵机运动学”的事物,可是千万别被其名称给吓到,实质上它就是一种线性插值。
08 再进一步:用串口遥控舵机
加入串口通信。电脑发个数字,舵机转到对应角度。
比如发“0”,转0度。发“90”,转90度。
要点:
单片机串口接收中断里,解析字符串。
注意:一次可能收不全。要判断是否收到换行符。
收到完整数字后,限制范围0~180。
转换成高电平时间,更新变量。
这般一来,你的舵机便成为了一个能够进行编程的执行器,连接上蓝牙模块后,就连手机都能够对其实施控制。
09 总结:控制舵机的三要素
电源。给足电流,稳压共地。
时序。周期20ms,高电平500-2500us。
别进行阻塞操作,采用定时器中断方式,而非使用那种会一直等待的delay,不然的话,你就什么事情都做不了啦。
掌握了这三条,市面上99%的舵机你都能控。
铭记:于编写程序之际,思虑多一步“倘若电源忽然产生抖动会呈现何种状况”,而非“程序运行通畅便足矣”。
10 现在,轮到你动手了
不要仅仅将这段文章视作一种收藏,开启你的Keil,把开发板连接妥当,将上面的代码敲击一回。
前面三次大概有很大可能性不会转动。不要着急。拿万用表去测量一下电压,使用逻辑分析仪抓取一下波形。要是实在没有办法,换一个舵机尝试一下。
每解决一个问题,你对嵌入式的理解就深一层。
若干年后再回首,你会发觉,掌握控制舵机的那日,乃是你切实开启去领会“借代码操控物理世界”的起始点。
手里那个嗡嗡响的小舵机,就是通往机器人世界的第一把钥匙。
别等了。现在就通电。
试试看。
Update Time:2026-05-06
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.