这篇文章主要介绍树莓派如何实现CPU温控风扇,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
树莓派温控风扇
三极管方式 J13009三极管(做开关用),管脚说明,面对有文字说明的一面,从左到右:B C E,1k电容(下拉电阻,保护用),杜邦线若干 接线顺序一定要正确:
继电器版(JDQ)方式:
+5V-JDQ输入正极
JDQ输出-风扇红线
GND-JDQ控制线负极-风扇黑线
GPIO40-JDQ控制线圈正极线
代码:
#每2秒读取一次CPU内部的温度传感器温度并显示CPU温度
#超过45°C时打开风扇
#低于38°C时关闭风扇
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 = 40 #最右下脚针脚
GPIO.setmode(GPIO.BOARD)
#GPIO.setmode(GPIO.BCM) #建议以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, 1)
is_close = False
else:
if temp < 38.0:
print(time.ctime(), temp, 'close air fan')
GPIO.output(channel, 0)
is_close = True
time.sleep(2.0)
print(time.ctime(), temp)
if __name__ == '__main__':
main()
树莓派开机运行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 表示调用的指令,和在终端输入运行脚本的指令格式一致,Linux也可参考此代码。
以上是“树莓派如何实现CPU温控风扇”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注天达云行业资讯频道!