树莓派4 PWM控制风扇转速
生活随笔
收集整理的這篇文章主要介紹了
树莓派4 PWM控制风扇转速
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
5V風扇是筆記本拆下來的,接了個ss8550 NPNPNP三極管。
接線:
pi 5V --- 風扇5V
風扇GND --- 三接管C極
pi BCM 18 --- 三極管B極
pi GND --- 三極管E極
效果圖:
代碼:
#!/usr/bin/python3 # encoding: utf-8import RPi.GPIO import time RPi.GPIO.setwarnings(False) RPi.GPIO.setmode(RPi.GPIO.BCM) RPi.GPIO.setup(18, RPi.GPIO.OUT) pwm = RPi.GPIO.PWM(18, 75)duty = 0 lastDuty = 0try:while True:tmpFile = open('/sys/class/thermal/thermal_zone0/temp')temp = int(tmpFile.read())tmpFile.close()duty = 100;if temp >= 34500 :duty = 60if lastDuty >= 36500 :duty = 50if temp >= 38500:duty = 40if temp >= 40500:duty = 30if temp >= 42500:duty = 20if temp >= 44500:duty = 10if temp >= 46500:duty = 0if duty == 100:pwm.stop()time.sleep(0.2)RPi.GPIO.output(18, RPi.GPIO.HIGH)if duty == 0:pwm.stop()time.sleep(0.2)RPi.GPIO.output(18, RPi.GPIO.LOW)if duty > 0 and duty < 100:if lastDuty == 0 or lastDuty == 100:pwm.start(0)time.sleep(1)pwm.ChangeDutyCycle(duty)lastDuty = dutytime.sleep(5) except KeyboardInterrupt:pass pwm.stop() time.sleep(1) RPi.GPIO.output(18, RPi.GPIO.LOW)總結
以上是生活随笔為你收集整理的树莓派4 PWM控制风扇转速的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用云安监控和管理云
- 下一篇: Wi-Fi Expert专业无线网测试软