Sipeed NanoCluster风扇降噪:PWM调速教程

AI摘要

Sipeed NanoCluster风扇默认满速噪音大,可通过CM4主板修改配置启用PWM,使用Python脚本实现温度自动调速,并设置开机自启服务。

Sipeed NanoCluster迷你集群板 风扇通电后是满速运行,非常的吵,但是可以通过Slot 1设备来控制风扇转速;

我的是CM4主板,下面是具体的操作;

1、修改/boot/firmware/config.txt文件添加 下面的内容

[all]
dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4

然后重启slot 1的设备

reboot

2、测试控制风扇

# 重启后检查 PWM 是否可用
ls /sys/class/pwm/

# 应该能看到 pwmchip0 和 pwmchip2

# 导出 GPIO13(对应 pwmchip0 的通道 1)
echo 1 | sudo tee /sys/class/pwm/pwmchip0/export 2>/dev/null

# 设置周期和占空比
echo 20000000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/period      # 20ms = 50Hz
echo 8000000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/duty_cycle   # 40% 转速
echo 1 | sudo tee /sys/class/pwm/pwmchip0/pwm1/enable             # 启动

# 关风扇
echo 0 | sudo tee /sys/class/pwm/pwmchip0/pwm1/duty_cycle

# 满速
echo 20000000 | sudo tee /sys/class/pwm/pwmchip0/pwm1/duty_cycle

3、自动温控脚本(Python)

#!/usr/bin/env python3
import time

PWM_PATH = "/sys/class/pwm/pwmchip0/pwm1"
PERIOD = 20000000  # 50Hz

TEMP_CURVE = [
    (40, 0),
    (45, 20),
    (50, 40),
    (55, 60),
    (60, 80),
    (65, 100),
]

def get_temp():
    try:
        with open("/sys/class/thermal/thermal_zone0/temp") as f:
            return float(f.read().strip()) / 1000.0
    except:
        return 50

def set_duty(duty_pct):
    duty_ns = int(PERIOD * duty_pct / 100)
    try:
        with open(f"{PWM_PATH}/duty_cycle", "w") as f:
            f.write(str(duty_ns))
    except:
        pass

# Init PWM
try:
    with open(f"{PWM_PATH}/period", "w") as f:
        f.write(str(PERIOD))
    with open(f"{PWM_PATH}/enable", "w") as f:
        f.write("1")
except:
    pass

current_pct = -1

while True:
    temp = get_temp()
    duty = 0
    for t, d in TEMP_CURVE:
        if temp >= t:
            duty = d
    
    if duty != current_pct:
        set_duty(duty)
        current_pct = duty
        print(f"Temp: {temp:.1f}°C → Fan: {duty}%")
    
    time.sleep(5)

4、设置开机自启动

sudo nano /etc/systemd/system/fan-control.service

写入

[Unit]
Description=NanoCluster Fan Control
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/local/bin/fan_control.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

然后启用:

sudo systemctl daemon-reload
sudo systemctl enable --now fan-control.service
sudo systemctl status fan-control.service

点赞 0