diff --git a/fan_contorl.py b/fan_contorl.py new file mode 100644 index 0000000..dc0bf4b --- /dev/null +++ b/fan_contorl.py @@ -0,0 +1,67 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +import RPi.GPIO as GPIO +import time +import signal +import sys +import os + +# Configuration +FAN_PIN = 18 # BCM pin used to drive PWM fan +WAIT_TIME = 3 # [s] Time to wait between each refresh +PWM_FREQ = 25000 # [Hz] 25kHz for Noctua PWM control + +# Configurable temperature and fan speed +MIN_TEMP = 40 +MAX_TEMP = 65 +FAN_LOW = 40 +FAN_HIGH = 90 +FAN_OFF = 0 +FAN_MAX = 100 + +# Get CPU's temperature +def getCpuTemperature(): + res = os.popen('vcgencmd measure_temp').readline() + temp =(res.replace("temp=","").replace("'C\n","")) + #print("temp is {0}".format(temp)) # Uncomment for testing + return temp + +# Set fan speed +def setFanSpeed(speed): + fan.start(speed) + return() + +# Handle fan speed +def handleFanSpeed(): + temp = float(getCpuTemperature()) + # Turn off the fan if temperature is below MIN_TEMP + if temp < MIN_TEMP: + setFanSpeed(FAN_OFF) + #print("Fan OFF") # Uncomment for testing + # Set fan speed to MAXIMUM if the temperature is above MAX_TEMP + elif temp > MAX_TEMP: + setFanSpeed(FAN_MAX) + #print("Fan MAX") # Uncomment for testing + # Caculate dynamic fan speed + else: + step = (FAN_HIGH - FAN_LOW)/(MAX_TEMP - MIN_TEMP) + temp -= MIN_TEMP + setFanSpeed(FAN_LOW + ( round(temp) * step )) + #print(FAN_LOW + ( round(temp) * step )) # Uncomment for testing + return () + +try: + # Setup GPIO pin + GPIO.setwarnings(False) + GPIO.setmode(GPIO.BCM) + GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW) + fan = GPIO.PWM(FAN_PIN,PWM_FREQ) + setFanSpeed(FAN_OFF) + # Handle fan speed every WAIT_TIME sec + while True: + handleFanSpeed() + time.sleep(WAIT_TIME) + +except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt + setFanSpeed(FAN_HIGH) + #GPIO.cleanup() # resets all GPIO ports used by this function \ No newline at end of file