Note 01 of 02: 笔记 01 / 02:PWM Fan Control and Speed FeedbackPWM 风扇控制与转速反馈
Studying fan power and PWM connections, waveform frequency and duty, and separate tachometer feedback for measuring each fan’s response.从风扇供电与 PWM 接线,研究频率、占空比和独立测速反馈,理解怎样观察每只风扇的实际响应。
The cooler's OLED has a line reading Fan Speed followed by a percentage, which looks a lot like a speed. Following the display function down, what it prints is dutyCycle, the PWM duty sent to the fan. The mismatch between label and content leads straight to a question worth settling: how a duty setting actually acts on a fan, and where a reading of how fast the fan really turns would have to come from.
散热器的 OLED 上有一行 Fan Speed,后面跟着一个百分数,看着挺像转速。顺着显示函数往下找,它印出来的是 dutyCycle,也就是发给风扇的 PWM 占空比。名字和内容对不上,正好引出一个值得弄清楚的问题:占空比设置到底怎样作用到风扇上,风扇实际转多快,又该从哪里读回来。
Fan Interface Wiring风扇接口的连线
On the ESP32 controller schematic, D5 connects to the PWM net, and the fan outputs use three signals, 12V+, 12V− and PWM. The two four-pin connectors carry only those three as well, each leaving one pin unconnected. Following the nets makes it clearer than counting pins which wire carries power and which carries control.
ESP32 控制板原理图上,D5 接到 PWM 网络,风扇输出用到 12V+、12V− 和 PWM 三种信号。两个四针接口也只接了这三种,各空着一针。顺着网络看,比数接口有几只脚直观,电源和控制各走哪根线,一下就能对上。

Looking into how four-wire PWM fans work made me count the drive circuit inside the fan as part of the system. The four connections are power, ground, PWM input and tachometer output. The motor's energy comes through the supply wires, the controller gives its command on the PWM line, and the fan's own electronics drive the motor. Analog Devices' explanation of fan control adds that a dedicated PWM line lets those electronics stay powered, whereas repeatedly switching the whole fan supply can cut the tachometer output along with it.
查四线 PWM 风扇的工作方式,我才把风扇肚子里的驱动电路也算进来。四根线分别是供电、地、PWM 输入和测速输出。电机要的能量从电源线来,控制器从 PWM 线下指令,真正驱动电机的是风扇内部的电路。Analog Devices 的风扇控制说明还提到,PWM 单独走一根控制线,风扇里的电子电路就能一直有电;要是拿开关反复切整只风扇的供电,测速输出也可能跟着断掉。
Power and control therefore need separate thought when wiring. The 12 V supply delivers the motor's running energy, while the GPIO provides a logic signal whose level and ground reference have to suit the fan interface; the motor's supply voltage says nothing about the PWM pin's logic level. The actual pin order still has to be matched wire by wire against the schematic and the fan's definition, because a four-pin housing on its own settles none of it.
这样一来,供电和控制在接线时就得分开想。12 V 送的是电机运转的能量,GPIO 给的是逻辑信号,控制信号的电平和参考地要跟风扇接口对得上,不能拿电机的供电电压去理解 PWM 引脚。具体的脚序还得按图纸和风扇定义一根根对,光看四针的外形,什么也定不下来。
Frequency, Duty and Speed频率、占空比和转速
The program starts PWM at 15 kHz with 8-bit resolution. One cycle lasts about 66.7 μs, so an ideal 50% duty keeps the signal high for roughly 33.3 μs each cycle. The code maps percentages from 0 to 100 onto 0 to 255, and 50 becomes 127, just under half. All of these numbers describe the waveform on the pin; none of them yet says how many times the blades turn in a minute.
程序一开始把 PWM 设在 15 kHz、8 位分辨率。一个周期约 66.7 μs,理想的 50% 占空比,就是每个周期里大约 33.3 μs 是高电平。代码把 0 到 100 的百分数映射到 0 到 255,50 得到 127,略低于一半。这些数说的都是引脚上的波形,跟叶片一分钟转几圈还没扯上关系。
The main loop does not have to toggle the pin for every 15 kHz pulse either. According to the ESP32 LEDC documentation, once a channel is configured the peripheral generates PWM at the set frequency and duty by itself. The program writes a new duty only now and then, and between writes the peripheral keeps producing the old waveform. How often temperature processing changes the setting and how long one PWM cycle lasts sit on entirely different time scales.
15 kHz 的脉冲也用不着主循环一个个去翻。ESP32 的 LEDC 文档说明,通道配好以后,外设会按设定的频率和占空比自己产生 PWM。程序隔一阵才写一次新的占空比,中间外设照旧输出原来的波形。温度处理多久改一次设置,和 PWM 一个周期多长,完全是两个时间尺度上的事。
Choosing the frequency starts with the particular fan's interface requirements. Noctua's microcontroller guide, for example, targets 25 kHz for its fans and accepts 21 to 28 kHz. That is one manufacturer's requirement for its own fans, and a handy example of what to check. In the original program each button press moves the frequency up or down by 200 Hz, which only changes how fast the waveform repeats; raising that number does not make the fan spin proportionally faster.
频率选多少,先得查具体风扇的接口要求。以 Noctua 的微控制器接线说明为例,他们家风扇的目标值是 25 kHz,可接受范围 21 到 28 kHz。这是厂商对自家风扇的要求,正好拿来练习怎么核对接口。原程序里按一次键,频率加减 200 Hz,改的只是波形重复得多快;频率数字调高了,风扇并不会按同样的比例转得更快。
Tachometer Interface and RPM Calculation测速接口与转速换算
A real speed reading has to travel back from the fan. Noctua's PWM white paper describes an open-collector tachometer output on its fans that produces two pulses per revolution. When the fan's internal output transistor conducts it pulls the line low; when it switches off, an external pull-up resistor brings the line back high. A controller attached to that node can count the pulses as the fan turns.
真正的转速得从风扇那头传回来。Noctua 的 PWM 技术白皮书说明,他们的风扇用开集电极测速输出,每转一圈出两个脉冲。风扇里的输出晶体管导通时把信号线拉低,关断时靠外部上拉电阻把线拉回高电平。控制器接在这个节点上,就能数到随转动产生的脉冲。
The pull-up should go to the controller's logic voltage, and the tachometer output and controller need a common ground. With a 10 kΩ pull-up to 3.3 V, about 0.33 mA flows while the output is low. That figure checks the current, but the actual value also has to respect the output's allowed sink current and the signal's rise time, since a larger resistor charges the line capacitance more slowly. Whether the controller pin sees clean enough levels and edges to count reliably is something to confirm on an oscilloscope.
上拉电源要按主控的逻辑电压来选,测速输出和主控还得共地。假设上拉到 3.3 V、电阻取 10 kΩ,输出拉低时电流约 0.33 mA。这个算式能核对电流大小,实际取值还要顾及输出允许的灌电流和信号上升速度,电阻越大,线上的电容充得越慢。主控引脚上的高低电平和边沿能不能被可靠识别,得拿示波器看过才算数。
Once pulses per revolution are known, the conversion is short. At two pulses per revolution, a 100 Hz tachometer signal means 50 revolutions per second, or 3000 rpm. The car's power and wheel-speed note worked through pulse timing, and the same counting idea carries over here, as long as the fan's own pulses-per-revolution value is used and the tachometer frequency is kept apart from the 15 kHz control waveform.
每圈脉冲数确定以后,换算就很短了。按两脉冲一圈算,100 Hz 的测速信号对应每秒 50 圈,也就是 3000 rpm。小车那篇供电与轮速测量研究过脉冲计时,这里沿用同一个计数思路,只是要换成风扇自己的每圈脉冲数,也别把测速频率和控制端的 15 kHz 搅在一起。
Two fans bring one more wiring question. Both fan outputs on the schematic share PWM, so they receive the same control waveform. Knowing each fan's speed separately would need separate tachometer inputs. Following the open-collector behaviour through, tying both outputs together would let either fan pull the shared node low, and with the pulses overlapping there would be no telling which fan produced them. A shared command is convenient; watching each fan on its own needs a feedback line for each.
两只风扇又带出一个接线问题。原理图上两路风扇输出共用 PWM,收到的是同一个控制波形。想分别知道两只风扇的转速,测速信号就得分开接。按开集电极的工作方式推下去,两个测速输出要是并在一起,任何一边拉低都会把公共节点拉低,脉冲叠在一起,就分不清是哪一只的了。控制指令共用很省事,想单独看清每只风扇,还得各给一路反馈。
Duty Cycle and Fan Speed占空比与转速的对应关系
With a speed reading available, duty and rotation can finally be compared side by side. I would hold the supply voltage and PWM frequency fixed, step the duty one notch at a time, record the speed once it settles, and plot this fan's response curve. The low-duty end deserves its own test: whether the fan can start from rest, and how low the duty can go once it is already spinning before it stops, recorded separately. A curve built that way includes the starting and low-speed behaviour that matters most in actual use.
有了测速读数,占空比和转速才能放在一起比。我想固定供电电压和 PWM 频率,一档一档改占空比,等转速稳定后记下来,画出这只风扇的响应曲线。低占空比那一段尤其值得单独试,从静止能不能起转,已经转起来以后能降到多低还不停,分开记。这样画出来的曲线,才包含实际使用时最在意的启动和低速表现。
This version of the program hangs duty cycle after Fan Speed and never reads a tachometer input. To change that, the screen line would simply say PWM, with a separate RPM line added once speed sensing is wired in. One line shows the setting I sent, the other shows how the fan answered, and with the two side by side it would be obvious at a glance which fan was slacking off.
这版程序把占空比挂在 Fan Speed 后面,又没读测速输入。要改的话,屏幕上那行干脆写成 PWM,接上测速以后再单独加一行 RPM。一行是我发出去的设置,一行是风扇给出的回应,两行并排摆着,调风扇的时候哪只在偷懒,一眼就看得出来。