树莓派温控风扇脚本(使用三极管 s8050)
290 字
1 分钟
树莓派温控风扇脚本(使用三极管 s8050)

这里均以s8050为开关,s8050为NPN型三极管,以平的那面为参考面,左边阵脚接电源正极,中间针脚为开关,右边阵脚接电源负极
不控速风扇:
#!/usr/bin/python#coding:utf-8import time
import RPi.GPIO as GPIO
#这里选择14(BCM)号针脚作为开关GPIO_OUT = 14
def setup_GPIO(): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(GPIO_OUT, GPIO.OUT)
#开启风扇
def start_fan(): GPIO.output(GPIO_OUT,GPIO.HIGH)
#关闭风扇
def stop_fan(): GPIO.output(GPIO_OUT,GPIO.LOW)
#获取cpu温度
def cpu_temp(): tempFile=open("/sys/class/thermal/thermal_zone0/temp") cpu_temp=tempFile.read() tempFile.close() temp_c=(int(cpu_temp)/1000) return int(temp_c)
#若温度大于等于55度开启风扇,小于等于45度关闭扇。中间相差温度作为缓冲,不然风扇就会一会开一会关。
def fen(): if cpu_temp()>=55: start_fan() fen_state(1)
elif cpu_temp()<=45: stop_fan() fen_state(0)
else: pass
def fen_state(state): f=open("/etc/startup-script/state.txt") file=f.readlines() i=0 for line in file: line = line.strip("\n") v = line.split(":") if v[0] == "fen_state": file[i] = "fen_state:"+str(state) break i=i+1 f=open("/etc/startup-script/state.txt",'w+') f.writelines(file) f.close()
if __name__ == '__main__': setup_GPIO() fen_state(0) while True: fen() time.sleep(5)控速风扇:
#!/usr/bin/env python# encoding: utf-8import RPi.GPIO as GPIOimport time
#以14号(BCM)阵脚为开关GPIO_OUT = 14GPIO.setmode(GPIO.BCM)GPIO.setwarnings(False)GPIO.setup(GPIO_OUT,GPIO.OUT)pwm = GPIO.PWM(GPIO_OUT, 100)
speed=0prv_temp=0try: while True: tmpFile=open('/sys/class/thermal/thermal_zone0/temp') cpu_temp=int(tmpFile.read())/1000 tmpFile.close() if cpu_temp >= 34: if prv_temp < 34: # 启动时防止风扇卡死先全功率转0.1秒 pwm.start(0) pwm.ChangeDutyCycle(100) time.sleep(.1) speed=min(cpu_temp, 100) pwm.ChangeDutyCycle(speed) else: pwm.stop() prv_temp=cpu_temp time.sleep(5)except KeyboardInterrupt: passpwm.stop()支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
相关文章智能推荐
1
【树莓派】温控风扇脚本(shell版)
树莓派脚本2020-09-06
2
【树莓派】延时拍照并上传到服务端
树莓派脚本2019-01-22
3
frp开机启动与自动检查运行脚本(Python)
树莓派脚本2017-12-08
4
【树莓派】温控风扇(golang)
树莓派笔记2019-04-13
5
【树莓派】ZERO W上快速安装Open CV
树莓派脚本2019-01-22
随机文章随机推荐















加载更多…