-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan_control.py
52 lines (40 loc) · 1.4 KB
/
fan_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python3
import RPi.GPIO as GPIO
import time
import signal
import sys
FAN_PIN = 18 # BCM pin used to drive PWM fan
WAIT_TIME = 1 # [s] Time to wait between each refresh
PWM_FREQ = 25 # [Hz] PWM frequency
OFF_TEMP = 25 # [°C] temperature below which to stop the fan
MIN_TEMP = 35 # [°C] temperature above which to start the fan
MAX_TEMP = 75 # [°C] temperature at which to operate at max fan speed
FAN_LOW = 20
FAN_HIGH = 100
FAN_OFF = 0
FAN_MAX = 100
FAN_GAIN = float(FAN_HIGH - FAN_LOW) / float(MAX_TEMP - MIN_TEMP)
def getCpuTemperature():
with open('/sys/class/thermal/thermal_zone0/temp') as f:
return float(f.read()) / 1000
def handleFanSpeed(fan, temperature):
if temperature > MIN_TEMP:
delta = min(temperature, MAX_TEMP) - MIN_TEMP
fan.start(FAN_LOW + delta * FAN_GAIN)
# print("Temperature: {:05.02f}, Speed: {:05.02f}".format(temperature, FAN_LOW + delta * FAN_GAIN))
elif temperature < OFF_TEMP:
fan.start(FAN_OFF)
try:
signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(FAN_PIN, GPIO.OUT, initial=GPIO.LOW)
fan = GPIO.PWM(FAN_PIN, PWM_FREQ)
while True:
handleFanSpeed(fan, getCpuTemperature())
time.sleep(WAIT_TIME)
except KeyboardInterrupt:
print("Fan ctrl interrupted by keyboard")
pass
finally:
GPIO.cleanup()