简介
树莓派4b的自控风扇
主要信息
系统: Debian-Pi-Aarch64 2020-06-22-v2020-2.0-U4-Release BaseOS Plus++
三极管:S8050 NPN
连接线:杜邦线公对母,母对母
连接方式
风扇正极->5V
风扇负极->S8050 C
S8050-B-> Pi Pin12
S8050-E-> Pi GND
安装相应包
sudo apt-get install python-pip python-dev
pip install RPi.GPIO spidev
控制程序
#!/usr/bin/env python
# encoding: utf-8
import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 100)
GPIO.setwarnings(False)
speed = 0
now_temp = 0
pwm.start(100)
logfile=open('/tmp/auto_temp.log','a')
def get_temp():
tmpFile = open( '/sys/class/thermal/thermal_zone0/temp')
temp=float(tmpFile.read())/1000
tmpFile.close()
return temp
while True:
now_temp=get_temp()
logfile.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" now_temp: "+str(now_temp)+" speed:")
if now_temp>=34:
speed=min(int(now_temp-34)*5,100)
logfile.write(str(speed)+'\n')
logfile.flush()
pwm.ChangeDutyCycle(speed)
time.sleep(5)
开机自启
/etc/systemd/system/auto_temp.service
[Unit]
Description=Auto temp Service
After=network.target
[Service]
Type=simple
User=pi
ExecStart=python /home/pi/auto_temp.py
Restart=on-failure
SuccessExitStatus=3 4
RestartForceExitStatus=3 4
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable auto_temp
systemctl start auto_temp