#!/usr/bin/python3 import RPi.GPIO as IO import time import subprocess servo = 18 IO.setwarnings(False) IO.setmode (IO.BCM) IO.setup(servo,IO.OUT) fan = IO.PWM(servo,2000) fan.start(0) def get_temp(): output = subprocess.run(['/opt/vc/bin/vcgencmd', 'measure_temp'], capture_output=True) temp_str = output.stdout.decode() try: return float(temp_str.split('=')[1].split('\'')[0]) except (IndexError, ValueError): raise RuntimeError('Could not get temperature') def set_speed(fan, speed): fan.ChangeDutyCycle(speed) while 1: temp = get_temp() # Get the current CPU temperature if temp > 70: # Check temperature threshhold, in degrees celcius set_speed(fan, 100) # Set fan duty based on temperature, 100 is max speed and 0 is min speed or off. elif temp > 60: set_speed(fan, 85) elif temp > 50: set_speed(fan, 60) elif temp > 40: set_speed(fan, 50) elif temp > 32: set_speed(fan, 45) elif temp > 25: set_speed(fan, 40) else: set_speed(fan, 0) time.sleep(5) # Sleep for 5 seconds