├── requirements.txt ├── script └── install ├── README.md ├── fancontrol.sh └── fancontrol.py /requirements.txt: -------------------------------------------------------------------------------- 1 | gpiozero==1.5.1 2 | RPi.GPIO==0.7.0 3 | -------------------------------------------------------------------------------- /script/install: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | set -e 4 | 5 | cd "$(dirname "$0")/.." 6 | 7 | echo "=> Installing fan controller...\n" 8 | sudo cp fancontrol.py /usr/local/bin/ 9 | sudo chmod +x /usr/local/bin/fancontrol.py 10 | 11 | echo "=> Starting fan controller...\n" 12 | sudo cp fancontrol.sh /etc/init.d/ 13 | sudo chmod +x /etc/init.d/fancontrol.sh 14 | 15 | sudo update-rc.d fancontrol.sh defaults 16 | sudo /etc/init.d/fancontrol.sh start 17 | 18 | echo "Fan controller installed." 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pi Fan Controller 2 | 3 | Raspberry Pi fan controller. 4 | 5 | ## Description 6 | 7 | This repository provides scripts that can be run on the Raspberry Pi that will 8 | monitor the core temperature and start the fan when the temperature reaches 9 | a certain threshold. 10 | 11 | To use this code, you'll have to install a fan. The full instructions can be 12 | found on our guide: [Control Your Raspberry Pi Fan (and Temperature) with Python](https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python). 13 | -------------------------------------------------------------------------------- /fancontrol.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | ### BEGIN INIT INFO 4 | # Provides: fancontrol.py 5 | # Required-Start: $remote_fs $syslog 6 | # Required-Stop: $remote_fs $syslog 7 | # Default-Start: 2 3 4 5 8 | # Default-Stop: 0 1 6 9 | ### END INIT INFO 10 | 11 | # Carry out specific functions when asked to by the system 12 | case "$1" in 13 | start) 14 | echo "Starting fancontrol.py" 15 | /usr/local/bin/fancontrol.py & 16 | ;; 17 | stop) 18 | echo "Stopping fancontrol.py" 19 | pkill -f /usr/local/bin/fancontrol.py 20 | ;; 21 | *) 22 | echo "Usage: /etc/init.d/fancontrol.sh {start|stop}" 23 | exit 1 24 | ;; 25 | esac 26 | 27 | exit 0 28 | -------------------------------------------------------------------------------- /fancontrol.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import time 4 | 5 | from gpiozero import OutputDevice 6 | 7 | 8 | ON_THRESHOLD = 65 # (degrees Celsius) Fan kicks on at this temperature. 9 | OFF_THRESHOLD = 55 # (degress Celsius) Fan shuts off at this temperature. 10 | SLEEP_INTERVAL = 5 # (seconds) How often we check the core temperature. 11 | GPIO_PIN = 17 # Which GPIO pin you're using to control the fan. 12 | 13 | 14 | def get_temp(): 15 | """Get the core temperature. 16 | 17 | Read file from /sys to get CPU temp in temp in C *1000 18 | 19 | Returns: 20 | int: The core temperature in thousanths of degrees Celsius. 21 | """ 22 | with open('/sys/class/thermal/thermal_zone0/temp') as f: 23 | temp_str = f.read() 24 | 25 | try: 26 | return int(temp_str) / 1000 27 | except (IndexError, ValueError,) as e: 28 | raise RuntimeError('Could not parse temperature output.') from e 29 | 30 | if __name__ == '__main__': 31 | # Validate the on and off thresholds 32 | if OFF_THRESHOLD >= ON_THRESHOLD: 33 | raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD') 34 | 35 | fan = OutputDevice(GPIO_PIN) 36 | 37 | while True: 38 | temp = get_temp() 39 | 40 | # Start the fan if the temperature has reached the limit and the fan 41 | # isn't already running. 42 | # NOTE: `fan.value` returns 1 for "on" and 0 for "off" 43 | if temp > ON_THRESHOLD and not fan.value: 44 | fan.on() 45 | 46 | # Stop the fan if the fan is running and the temperature has dropped 47 | # to 10 degrees below the limit. 48 | elif fan.value and temp < OFF_THRESHOLD: 49 | fan.off() 50 | 51 | time.sleep(SLEEP_INTERVAL) 52 | --------------------------------------------------------------------------------