樹莓派+溫控+風扇+wata

購進樹莓派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()

上圖:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章