树莓派 根据 CPU 温度控制风扇起停

来自linux中国网wiki
Evan讨论 | 贡献2019年10月14日 (一) 13:55的版本 (导入1个版本)
跳到导航 跳到搜索

Q

风扇声音非常大 晚上睡觉时就烦死了

R

树莓派使用三极管实现温度控制风扇


https://testerhome.com/topics/8068

https://blog.csdn.net/qq_15947947/article/details/79637718

http://bbs.elecfans.com/forum.php?mod=viewthread&tid=1117101&extra=page=2



我用了,连续开机半年,没问题。一天散热片掉了(我把派竖着放了),几天后,派挂了。


1.拆开风扇中间的胶,加机油

2.垫块海棉 在壳最下面

散热片

import sys
import time
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("Error importing RPi.GPIO!  This is probably because you need superuser privileges.  You can achieve this by using 'sudo' to run your script")


def cpu_temp():
    with open("/sys/class/thermal/thermal_zone0/temp", 'r') as f:
        return float(f.read())/1000


def main():
    channel = 18
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)

    # close air fan first
    GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH)
    is_close = True
    while True:
        temp = cpu_temp()
        if is_close:
            if temp > 45.0:
                print time.ctime(), temp, 'open air fan'
                GPIO.output(channel, GPIO.HIGH)
                is_close = False
        else:
            if temp < 38.0:
                print time.ctime(), temp, 'close air fan'
                GPIO.output(channel, GPIO.LOW)
                is_close = True

        time.sleep(2.0)
        print time.ctime(), temp


if __name__ == '__main__':
    main()

https://testerhome.com/topics/8068

鄙人最终就使用了三极管方案,使用最短,最简单的接线方法,完全藏匿起来。
代码:
代码是最简单的代码,仅根据温度控制转与不转,可以直接复制使用,

import RPi.GPIO as GPIO
import time
import commands

T_HIGH = 45
T_LOW = 35
fan_pin = 12
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fan_pin, GPIO.OUT)

def get_gpu_temp():
    gpu_temp = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )
    return  float(gpu_temp)

while 1:
    gpu_temp_loop = get_gpu_temp()
    print 'GPU temp:', gpu_temp_loop,'C'
    if gpu_temp_loop > T_HIGH:
        GPIO.output(fan_pin, 1)
    elif gpu_temp_loop < T_LOW:
        GPIO.output(fan_pin, 0)
    time.sleep(5)

复制时注意分割线,另存为python文件即可(如pi2_fan.py)
运行就是直接python pi2_fan.py
如果想随机启动就加到rc.local里,这个可以搜一下就知道。

see also

三种树莓派温度控制风扇方案整理

为什么买回来的风扇都他不是静音

树莓派3散热片的安装

厨房直饮水净水器 DIY 安装记录