2019年8月17日土曜日

Raspberry Pi 3B+にケースfan追加




























           



格安(1コイン以下)のケースとファンのセットを入手したので、試してみました。
とりあえず動きました

[pwm制御]

トランジスタ 2SC1815-GR

配線
E-->Pi GND
C-->fan GND
B-->100Ω抵抗-->Pi GPIO18

fan Vcc --> Pi 5V

sample script

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time

DEBUG=True

GPIO_PWM = 18
Hz = 60
def get_cpu_temp():
    with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
        t=f.read()
    return int(t)/1000

def fanctl_pwm():
    """
    cpu temp,duty
    """
    td=(
        (29,0),
        (30,25),
        (35,50),
        (40,60),
        (41,65),
        (43,70),
        (45,75),
        (46,80),
        (48,90),
        (49,95),
        (50,98),
        (51,100),
       )
    """
    td=(
        (45,0),
        (46,100),
       )
    """
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(GPIO_PWM, GPIO.OUT)
    p = GPIO.PWM(GPIO_PWM, Hz)
    try:
        duty = 100
        p.start(duty)
        time.sleep(1)
        while True:
            cpu_temp = get_cpu_temp()
            duty = 100
            for x in td:
               if DEBUG:
                print "x(0)" + str(x[0]) + " C" ,
                print "x(1)" + str(x[1]) + " %"
               if  x[0]  >= cpu_temp:
                  duty = x[1]
                  break
               elif  x[0]  > cpu_temp:
                  duty = 0
                  break         
            if DEBUG:
             print "cpu temp=" + str(cpu_temp) + " C"
             print "fun duty=" + str(duty) + "%"

            p.ChangeDutyCycle(duty)
            time.sleep(30)
    except Exception as e:
        print "Exception : " + str(e)
    finally:
        p.stop()
        GPIO.cleanup()
if __name__ == "__main__":
    time.sleep(1)
    fanctl_pwm()