树莓派+温控+风扇+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()

上图:
在这里插入图片描述

已标记关键词 清除标记
相关推荐
第35讲:智能温度测量系统(综合实验)c python import RPi.GPIO as GPIO import importlib import time import sys # BOARD pin numbering LedR = 11 LedG = 12 LedB = 13 Buzz = 15 #ds18b20 = '28-031467805fff' #location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave' joystick = importlib.import_module('15_joystick_PS2') ds18b20 = importlib.import_module('26_ds18b20') beep = importlib.import_module('10_active_buzzer') rgb = importlib.import_module('02_rgb_led') joystick.setup() ds18b20.setup() beep.setup(Buzz) rgb.setup(LedR, LedG, LedB) color = {'Red':0xFF0000, 'Green':0x00FF00, 'Blue':0x0000FF} def setup(): global lowl, highl lowl = 29 highl = 31 def edge(): global lowl, highl temp = joystick.direction() if temp == 'Pressed': destroy() quit() if temp == 'up' and lowl < highl-1: highl += 1 if temp == 'down' and lowl >= -5: highl -= 1 if temp == 'right' and highl <= 125: lowl += 1 if temp == 'left' and lowl < highl-1: lowl -= 1 def loop(): while True: edge() temp = ds18b20.read() print 'The lower limit of temperature : ', lowl print 'The upper limit of temperature : ', highl print 'Current temperature : ', temp if float(temp) < float(lowl): rgb.setColor(color['Blue']) for i in range(0, 3): beep.beep(0.5) if temp >= float(lowl) and temp < float(highl): rgb.setColor(color['Green']) if temp >= float(highl): rgb.setColor(color['Red']) for i in range(0, 3): beep.beep(0.1) def destroy(): beep.destroy() joystick.destroy() ds18b20.destroy() rgb.destroy() GPIO.cleanup() if __name__ == "__main__": try: setup() loop() except KeyboardInterrupt: destroy()
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页