温控散热

记录树莓派配置温控散热风扇

接线

风扇红线 接 树莓派5V,比如4号引脚
风扇黑线 接 三极管C
三极管E极 接 树莓派0V,比如6号引脚
三极管B极 接 GPIO, 我用的8号引脚GPIO14

Raspberry Pi3 Model B GPIO针脚定义

三极管顾名思义,它有3个极。分别是E发射机、B基极、C集极。
可以理解为E极是正极、C极是负极、B极是信号极控制是否通电。
和二极管一样,正极接电源正极,负极接电源的负极。S8050的引
脚示意图如下:

网上的温控代码

地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# encoding: utf-8
import RPi.GPIO
import time
import sys
import math

RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)
gpio=14
RPi.GPIO.setup(gpio, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(gpio, 440)
RPi.GPIO.setwarnings(False)

#风扇能保持旋转的最低速度,需要自己尝试
min_speed=20.0
#希望cpu温度稳定在多少
temperature=39400
prv_speed = 0.0
speed_fin=50.0
max_step=3.0

def sigmoid(x):
x=x/4000.0
result= 2.0*max_step/(1+math.exp(-x))-max_step
return result

try:
pwm.start(0)
while True:
tmpFile = open( '/sys/devices/platform/sunxi-i2c.0/i2c-0/0-0034/temp1_input' )
cpu_temp = int(tmpFile.read())
tmpFile.close()
speed_fin=speed_fin+sigmoid(cpu_temp-temperature)
speed_fin = min( speed_fin, 100.0 )
speed_fin = max( speed_fin, min_speed-0.5 )
if speed_fin < min_speed:
speed=0
else:
speed=speed_fin
if prv_speed == 0.0 :
if speed!=0.0 :
pwm.ChangeDutyCycle(100)
time.sleep(0.3)
print "start"
prv_speed = speed
pwm.ChangeDutyCycle(speed)
time.sleep(2)
except KeyboardInterrupt:
pass
pwm.stop()

我自己修改后的温控代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# encoding: utf-8

import RPi.GPIO
import time
import sys
import math

RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)
#这里根据自己接法修改
gpio=14
RPi.GPIO.setup(gpio, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(gpio, 100)

speed = 100

#风扇最小能保持旋转的速度,低于这个速度可能停转,需要拿自己的风扇测试然后修改
speed_min=20.0

#启动风扇的温度35000~60000
temperature_start=35000
max_speed_temperature = 46000

temperature_cpu_last = 0

def getSpeed(temperature):
speed = min((temperature/max_speed_temperature) * (100 - speed_min) + speed_min,100)
return speed

try:
pwm.start(0)
while True:
temperatureFile = open('/sys/class/thermal/thermal_zone0/temp')
temperature_cpu = int(temperatureFile.read())
temperatureFile.close()
print('now cpu temperature is: {}'.format(temperature_cpu))

if temperature_cpu >= temperature_start:

if temperature_cpu_last < temperature_start:
# print('---------------first start fan--------------')
pwm.ChangeDutyCycle(100)
time.sleep(.1)

speed = getSpeed(temperature_cpu)
print('fan speed is: {}'.format(speed))
pwm.ChangeDutyCycle(speed)
else:
pwm.stop()

temperature_cpu_last = temperature_cpu
time.sleep(3)

except KeyboardInterrupt:
# print('interrupt')
pass
pwm.stop()

设置开机自启动

  • 给Python脚本设置权限
    chmod +x + python脚本位置 + 名称,example:chmod +x /home/Python/Fan.py
  • 新建开机启动脚本
    /etc/init.d目录下新建一个文件,如sudo nano /etc/init.d/fan,然后粘贴下列内容,并将对应的自己的路径替换:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    #!/bin/sh
    #/etc/init.d/fan
    ### BEGIN INIT INFO
    # Provides: fan
    # Required-Start: $remote_fs $syslog
    # Required-Stop: $remote_fs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: fan
    # Description: This service is used to start fan control
    ### END INIT INFO

    case "$1" in
    start)
    echo "Starting app"
    python /home/pi/Python/fan.py
    ;;
    stop)
    echo "Stop"
    #kill $( ps aux | grep -m 1 'python /home/pi/Python/fan.py' | awk '{ print $2 }')
    ;;

    *)
    echo "Usage: service fan start|stop"
    exit 1
    ;;
    esac
    exit 0
  • 设置启动脚本权限
    sudo chmod +777 /etc/init.d/fan
  • 设置开机自启
    sudo update-rc.d fan defaults
  • 启动脚本以后可以使用service命令
    sudo service fan start
  • 关闭脚本service命令
    sudo service fan stop

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×