From 502e53b8b8bccde78fa2689347ea8673d614cd5b Mon Sep 17 00:00:00 2001 From: Rafael Zasas Date: Mon, 4 May 2020 19:49:18 -0700 Subject: [PATCH 1/3] PWM Control! Changed the controller to dynamically update the speeds based the temperature --- fancontrol.py | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 0bb0307..ca8e9dc 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -3,23 +3,19 @@ import subprocess import time -from gpiozero import OutputDevice +from gpiozero import PWMOutputDevice - -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. +SLEEP_INTERVAL = 3 # (seconds) How often we check the core temperature. +GPIO_PIN = 18 # 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] def get_temp(): """Get the core temperature. - Run a shell script to get the core temp and parse the output. - Raises: RuntimeError: if response cannot be parsed. - Returns: float: The core temperature in degrees Celsius. """ @@ -31,25 +27,24 @@ def get_temp(): raise RuntimeError('Could not parse temperature output.') -if __name__ == '__main__': - # Validate the on and off thresholds - if OFF_THRESHOLD >= ON_THRESHOLD: - raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') - fan = OutputDevice(GPIO_PIN) +def get_speed(): + """ + Dynamically change the output of the motor + Returns: A percentage of max max speed corresponding to temperature + """ + 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 - while True: - temp = get_temp() - # Start the fan if the temperature has reached the limit and the fan - # isn't already running. - # NOTE: `fan.value` returns 1 for "on" and 0 for "off" - if temp > ON_THRESHOLD and not fan.value: - fan.on() +if __name__ == '__main__': - # 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() + fan = PWMOutputDevice(GPIO_PIN) + + while True: + fan.value = get_speed() - time.sleep(SLEEP_INTERVAL) + time.sleep(SLEEP_INTERVAL) \ No newline at end of file From b780e1c24383eed4f0d53ec2318e5b3bebf99ad5 Mon Sep 17 00:00:00 2001 From: Rafael Zasas Date: Tue, 2 Jun 2020 15:27:58 -0700 Subject: [PATCH 2/3] Fixes Added new line at end of file Added Better Documentation --- fancontrol.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index ca8e9dc..3395b39 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -30,8 +30,8 @@ def get_temp(): def get_speed(): """ - Dynamically change the output of the motor - Returns: A percentage of max max speed corresponding to temperature + 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 @@ -47,4 +47,4 @@ def get_speed(): while True: fan.value = get_speed() - time.sleep(SLEEP_INTERVAL) \ No newline at end of file + time.sleep(SLEEP_INTERVAL) From 4727e1f6e4a8ee60cfd09bc77af2b5eebbd091c1 Mon Sep 17 00:00:00 2001 From: Rafael Zasas Date: Sat, 8 Oct 2022 08:45:44 -0700 Subject: [PATCH 3/3] Add option to use variable speed or threshold --- fancontrol.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/fancontrol.py b/fancontrol.py index 18badf3..d23bf35 100755 --- a/fancontrol.py +++ b/fancontrol.py @@ -5,9 +5,13 @@ from gpiozero import PWMOutputDevice SLEEP_INTERVAL = 3 # (seconds) How often we check the core temperature. -GPIO_PIN = 18 # Which GPIO pin you're using to control the fan. +ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature. +OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature. 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(): @@ -26,11 +30,6 @@ def get_temp(): except (IndexError, ValueError,) as e: raise RuntimeError('Could not parse temperature output.') from e -if __name__ == '__main__': - # Validate the on and off thresholds - if OFF_THRESHOLD >= ON_THRESHOLD: - raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') - def get_speed(): """ Gets the temp of the board and assigns percentage of max max speed correspondingly @@ -45,9 +44,27 @@ def get_speed(): if __name__ == '__main__': + # Validate the on and off thresholds + if not USE_PWM and OFF_THRESHOLD >= ON_THRESHOLD: + raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') + fan = PWMOutputDevice(GPIO_PIN) - while True: - fan.value = get_speed() + 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 + # isn't already running. + # NOTE: `fan.value` returns 1 for "on" and 0 for "off" + if temp > ON_THRESHOLD and not fan.value: + fan.on() - time.sleep(SLEEP_INTERVAL) + # 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() \ No newline at end of file