[树莓派]散热风扇转速控制

1264822519 技术分享3379阅读模式

前言

树莓派4代的性能显提升,随之而来的是大功率的发热。如果是做一个NAS服务器或者小型家庭媒体服务器,单纯的靠散热片,是会影响到性能的。
但是如果使用了风扇,可能存在风扇转速过大,噪音大的问题。
这时候后,可以选择模块或者三极管来控制。
【我选择了购买一个T9温控模块】

使用

使用时一件极其简单的事情

[树莓派]散热风扇转速控制
[树莓派]散热风扇转速控制

然后使用PYTHO代码控制

随温度而变化风扇转速[PWM]

#!/usr/bin/env python
# encoding: utf-8
# 随温度变化,自动控制风扇转速代码

import RPi.GPIO
import time
RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(2, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(2,100)
RPi.GPIO.setwarnings(False)


speed = 0
prv_temp = 0

try:


        while True:
        
                tmpFile = open( '/sys/class/thermal/thermal_zone0/temp' )
                cpu_temp = int(tmpFile.read())
                tmpFile.close()
                if cpu_temp>=34500 :
                
                        if prv_temp<34500 :
                        #启动时防止风扇卡死先全功率转0.1秒
                                pwm.start(0)
                                pwm.ChangeDutyCycle(100)
                                time.sleep(.1)
                        speed = min( cpu_temp/125-257 , 100 )
                        pwm.ChangeDutyCycle(speed)
                else :
                        pwm.stop()
                prv_temp = cpu_temp
                
                time.sleep(5)
                
except KeyboardInterrupt:
        pass
pwm.stop()

阈值控制风扇转停

#!/usr/bin/env python
# encoding: utf-8
# From:shumeipai.net
# 设定阀值启动关闭代码
import RPi.GPIO
import time

start = 40
stop = 27

RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(2, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(2,100)
fan = False

try:
    while True:
        with open('/sys/class/thermal/thermal_zone0/temp') as f:
            cur = int(f.read()) / 1000
        now = time.strftime("%H:%M:%S",time.localtime(time.time()))
        if not fan and cur >= start:
            pwm.start(100)
            fan = True
            print("[%s] Fan on @ %s" % (now, cur))
        if fan and cur <= stop:
            pwm.stop()
            fan = False
            print("[%s] Fan off @ %s" % (now, cur))
        time.sleep(1)
except KeyboardInterrupt:
    pwm.stop()

开机运行Python脚本的方法

适用于有图形操作界面的系统

在 /home/pi/.config 下创建一个文件夹,名称为 autostart,并在该文件夹下创建一个xxx.desktop文件(文件名以.desktop结尾,前面可以自定义),文件内容如下:

`[Desktop Entry]`

`Name=example`

`Comment=My Python Program`

`Exec=python /home/pi/example.py`

`Icon=/home/pi/example.png`

`Terminal=false`

`MultipleArgs=false`

`Type=Application`

`Categories=Application;Development;`

`StartupNotify=true`

以上 Name、Comment、Icon 可以自定,分别表示这个启动项目的名称、备注以及显示的图标。Exec 表示调用的指令,和在终端输入运行脚本的指令格式一致。

转载请保留原文链接

weinxin
xzx18691048554
我的微信
微信扫一扫
 
1264822519
  • 本文由 1264822519 发表于 2021年1月23日23:08:51
  • 转载请务必保留本文链接:https://blog.imnifeng.com/archives/344
评论  3  访客  3
    • 龙胤
      龙胤 0

      接了面包板的话怎么接

      • wmxenri
        wmxenri 0

        需要安装其他的库文件?

      匿名

      发表评论

      匿名网友
      :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
      确定

      拖动滑块以完成验证