购进树莓派3b+,把玩开始,安装centos版本,速度较慢,卸载之,安装了官方提供的debian arm版,运行效果不错,陆续在某宝上购进J13009三极管(做开关用),管脚说明,面对有文字说明的一面,从左到右:B C E,1k电容(下拉电阻,保护用),杜邦线若干(公对公、母对母、公对母),面包板,扩展board,够用。
接线顺序一定要正确:B(基极)连接下拉电阻、GPIO18(可以任选其他控制口),E(发射极)连接GND(扩展board地线插孔,有几个),C(集电极)连接风扇黑线,风扇红线连接5V(扩展board 5V插孔,有几个)。python控制代码参考前辈codeskyblue神贴(https://testerhome.com/topics/8068),在此表示感谢!!!,代码可能是测试环境不同,稍作修改,加粗标注。
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 = 18
#GPIO.setmode(GPIO.BOARD)#也许使用扩展board导致标注的数字是BCM的,猜测而已。
GPIO.setmode(GPIO.BCM)
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 < 43.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()
上图:
来源:CSDN
作者:brucewata
链接:https://blog.csdn.net/brucewata/article/details/88076312