Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions fancontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import time

from gpiozero import OutputDevice

from gpiozero import PWMOutputDevice

SLEEP_INTERVAL = 3 # (seconds) How often we check the core temperature.
ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature.
OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature.
SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature.
GPIO_PIN = 17 # Which GPIO pin you're using to control the fan.
OUTPUTS = [0.5, 0.6, 0.7, 0.8, 0.9, 1]
TEMPS = [40.0, 45.0, 50.0, 55.0, 60.0, 65.0]
USE_PWM = True
GPIO_PIN = 18 if USE_PWM else 17 # Which GPIO pin you're using to control the fan.



def get_temp():
Expand All @@ -27,14 +30,32 @@ def get_temp():
except (IndexError, ValueError,) as e:
raise RuntimeError('Could not parse temperature output.') from e

def get_speed():
"""
Gets the temp of the board and assigns percentage of max max speed correspondingly
Returns: A value between 0-1. Can be configured by adjusting OUTPUTS and TEMPS.
"""
speed = 0.0
for i in range(len(TEMPS)): # loop through list of temps
if get_temp() > TEMPS[i]:
speed = OUTPUTS[i] # set speed to the corresponding temp
return speed


if __name__ == '__main__':

# Validate the on and off thresholds
if OFF_THRESHOLD >= ON_THRESHOLD:
if not USE_PWM and OFF_THRESHOLD >= ON_THRESHOLD:
raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD')

fan = OutputDevice(GPIO_PIN)
fan = PWMOutputDevice(GPIO_PIN)

while True:
if USE_PWM:
while True:
fan.value = get_speed()

time.sleep(SLEEP_INTERVAL)
else:
temp = get_temp()

# Start the fan if the temperature has reached the limit and the fan
Expand All @@ -46,6 +67,4 @@ def get_temp():
# Stop the fan if the fan is running and the temperature has dropped
# to 10 degrees below the limit.
elif fan.value and temp < OFF_THRESHOLD:
fan.off()

time.sleep(SLEEP_INTERVAL)
fan.off()