Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Raspberry PI Fan Controller

To install run:

curl -Ls "" |bash -
curl -Ls "" -o /usr/local/bin/ && \
curl -Ls "" -o /etc/systemd/system/raspi-fan-control.service && \
sudo systemctl enable raspi-fan-control.service && sudo systemctl start raspi-fan-control.service
#!/usr/bin/env python3
import subprocess
import syslog
import time
from gpiozero import OutputDevice
ON_THRESHOLD = 64 # (degrees Celsius) Fan kicks on at this temperature.
OFF_THRESHOLD = 56 # (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.
def get_temp():
output =['vcgencmd', 'measure_temp'], capture_output=True)
temp_str = output.stdout.decode()
return float(temp_str.split('=')[1].split('\'')[0])
except (IndexError, ValueError):
raise RuntimeError('Could not parse temperature output.')
if __name__ == '__main__':
# Validate the on and off thresholds
raise RuntimeError('OFF_THRESHOLD must be less than ON_THRESHOLD')
fan = OutputDevice(GPIO_PIN)
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:
syslog.syslog("Case fan turned ON, current cpu temperature is: %0.0f c " % 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:
syslog.syslog("Case fan turned OFF, current cpu temperature is: %0.0f c " % temp)
Description=Raspberry Pi Fan Controller
ExecStart=/usr/bin/python3 /usr/local/bin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment