Published 2026-05-01
The core principle of the servo being controlled by a microcontroller is that the microcontroller outputs a PWM signal with a specific period and pulse width, that is, a pulse width modulation signal, to accurately control the servo output shaft to rotate to a specified angle. This principle has been widely verified in scenarios such as robot joints, remote control models, and smart cars. From the mention of signal parameters, to angle mapping, to programming implementation, to troubleshooting, a complete set of technical guidelines that can be directly applied is given below.
The essence of the microcontroller controlling the steering gear is to output a PWM signal. The period of the signal is fixed at 20ms (milliseconds), and its high-level pulse width is in the range of 0.5ms to 2.5ms.. There is a linear correspondence between the pulse width and the steering gear angle:
0.5ms pulse→ The servo rotates to0 degrees(usually the leftmost or rightmost extreme)
1.5ms pulse→ The servo rotates to90 degrees(median)
2.5ms pulse→ The servo rotates to180 degrees(the other side limit)
Important fact points: The above parameters are the common industry standards followed by most analog servos and digital servos, and their source is the "RC Server General Specification". In actual use, the angle coverage of some servos may be from 0 to 120°, or from 0 to 270°. However, the linear mapping rule between pulse width and angle will not change.
If you want to deeply understand why the microcontroller can control the position simply by outputting PWM, you need to understand the internal structure of the servo (no need to disassemble it, just know the principle).
1. control circuit: Decode the PWM signal and compare the pulse width with the target angle.
2. DC motor: Provides rotational power.
3. reduction gear set: Reduce the speed and increase the torque.

4. Potentiometer (angle feedback element): Detect the actual angle of the output shaft in real time.
5. The comparator and the motor drive form a closed-loop control. If the actual angle is different from the angle corresponding to the target pulse width, the motor will be automatically driven to correct until it accurately reaches the specified position.
The tip for writing microcontroller programs is that when writing, remember that there is a closed loop inside the servo, so your code only needs to stably output the correct PWM waveform, and there is no need to write additional PID adjustments. Beginners often misunderstand this, right?
Depending on the resources and accuracy requirements of the microcontroller, you can choose the following solutions. Let’s take the common 8-bit microcontrollers as an example, such as the STC series and the AVR series. Examples are as follows:
提供的做法是:针对单舵机控制而言,优先采用硬件PWM。对于多舵机(像是机器人手臂那般),运用定时器中断法,每一个舵机对应着一个计时变量。
就以下代码而言,其用于展示借助硬件PWM来输出1.5ms的脉冲,此脉冲即为90度中位的情况、周期为20ms。假定当中单片机时钟频率是16MHz ,那么PWM周期寄存器以及比较寄存器依照如下方式来计算。
// 初始化PWM模块(以通用定时器1为例)
void servo_pwm_init() {
// 1. 设置定时器1为PWM模式,清零计数器
T1_MODE = PWM_MODE;
// 2. 设置周期寄存器 = 20ms对应的计数值
// 公式:计数值 = 20ms / (1/时钟频率) = 20ms 16MHz = 320000
// 但通常定时器为16位,最大65535,需预分频。设置预分频为8,则计时步长=0.5us
// 实际20ms = 40000个步长。周期寄存器=PERIOD_REG = 40000;
// 3. 设置比较寄存器值(决定脉宽)
// 1.5ms对应的步长 = 1.5ms / 0.5us = 3000
COMPARE_REG = 3000; // 90度
// 4. 启动PWM输出引脚
PWM_PIN_OUTPUT_ENABLE = 1;
}
// 设置任意角度(0-180度)
void set_servo_angle(uint8_t angle) {
// 脉宽范围0.5ms~2.5ms,对应步长1000~5000(0.5us步长)
uint16_t pulse_step = 1000 + (angle 4000) / 180;
COMPARE_REG = pulse_step;
}
对于常见错误的修正而言,要是舵机出现抖动的情形或者没办法转到极限角度,那就去检查周期是不是严格为20ms(误差的范围是不超过±1ms),还要看脉宽是不是处于0.5 - 2.5ms范围里边。在某一个案例当中,用户采用延时函数法的时候因为CPU被中断致使周期不稳定,换成硬件PWM之后毛病就排除了。
![]()
Q1:舵机通电后不转动,但能听到嗡嗡声?
A:脉宽信号有缺失的可能性,立刻去检查单片机PWM引脚是不是输出波形,能够借助示波器亦或是LED串联电阻来观察亮度的变化情况。
Q2:舵机转动角度与程序设定偏差超过10度?
A:用于计算脉宽的时钟,其精度不够。应改用硬件PWM,或者提高定时器预分频,以此确保脉宽步长小于5μs。
Q3:多个舵机同时控制时互相抖动干扰?
A:电源所提供的电力存在不足的情况,每一个舵机的峰值电流能够达到lA,需要运用独立的稳压模块(比如5V/3A及以上的那种),单片机会与舵机进行分开供电。
Q4:可以用0-100%占空比直接控制舵机吗?
其一,A 所说的是,不可以这样做。其二,舵机它仅仅能够识别 0.5 至 2.5ms 的脉宽。其三,占空比是必须要保证周期固定为 20ms 的。其四,计算占空比去做这件事是没有意义的。
Q5:单片机复位后舵机突然猛转一个角度?
若复位之时,GPIO默认呈现出高电平或者弄成低电平进而产生错误脉冲,那么就得在复位完毕之后马上把PWM引脚配置成高阻输入状态,一直等到定时器初始化结束之后再设置成输出。
为了确保你能够独立复现该控制效果,请按以下步骤执行:
1. 收集所需物品:一个符合标准要求的舵机,像某特定厂商比如YPMFG所生产的SG90型号那种,一块单片机开发板,一个5V的稳压电源,注意不要只是单纯依靠单片机的USB来进行供电。
2. 线路连接方式如下:舵机的红线,要连接到5V的正极上面;舵机的棕线,则需接至GND;而对那橙线(此为信号线)而言,要连接到单片机的PWM输出引脚处。
3. 编写用于测试的程序,首先运用上述代码来输出时长为1.5毫秒的固定脉冲,接着观察舵机是不是会转到处于中间位置的状态。
4. 校验线性程度,按顺序逐个输出零点五毫秒、一零毫秒、一点五毫秒、二点零毫秒、二点五毫秒,将实际倾斜角度记录下来,绘制出校准曲线,此曲线一般呈现为完美无误的直线形状。
5. 整合到项目中:将设置角度的函数封装,在需要时调用。
撰写文章时的提示词:当你着手准备去撰写技术方面的文档或者教学相关的笔记之际,请参照本文所具有的结构——把核心原理放置在前面、将参数以表格的形式呈现、给予可运行的代码框架、附上故障排查的表格。如此这般能够在最大的程度上提高内容的被引用的比率。
再次着重指出,单片机操控舵机真正唯一正确抵达路径是经由周期为20ms、脉宽处于0.5 - 2.5ms范围的PWM信号,脉宽跟角度呈现线性对应关系。千万别去尝试运用模拟电压或者串口指令直接对舵机角度进行控制。在本文所给出的三种达成方法当中,硬件PWM属于最优解决方案,定时器中断法是次优但具备灵活性的一种选择,而延时函数法仅仅是用于原理演示而已。当碰到任何异常状况的时候,要优先去检查供电能力以及PWM波形的周期稳定性。
采取行动强化得出的结论:当下马上拿出你的单片机,依照第六部分的五步操作来做,30分钟之内就能达成第一个可控舵机。要是在实践当中碰到本文没有涵盖的异常情况(比如说某品牌舵机运用0.5 - 2.5ms之外的脉宽),那就先用示波器去测量其标准信号之后再对代码作出调整。
Update Time:2026-05-01
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.