Last active
September 7, 2019 19:30
-
-
Save erijpkema/fedf790bedf2989de5bc6187f2738976 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Script to regulate a 5v fan. | |
For wiring, see https://hackernoon.com/how-to-control-a-fan-to-cool-the-cpu-of-your-raspberrypi-3313b6e7f92c | |
#Author: Edoardo Paolo Scalafiotti <[email protected]> | |
# Modified by Egon Rijpkema | |
""" | |
import os | |
from time import sleep | |
import RPi.GPIO as GPIO | |
pin = 18 # The pin ID, edit here to change it | |
max_temp = 75 # When to start fan. | |
shutoff_temp = 50 # Temperature at which we stop the running fan | |
def setup(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.setwarnings(False) | |
def get_temp(): | |
res = os.popen('vcgencmd measure_temp').readline() | |
temp = (res.replace("temp=", "").replace("'C\n", "")) | |
print("temp is {0}".format(temp)) | |
return float(temp) | |
def fan_on(): | |
GPIO.output(pin, True) | |
def fan_off(): | |
GPIO.output(pin, False) | |
def regulate_temp(): | |
CPU_temp = get_temp() | |
if CPU_temp > max_temp: | |
fan_on() | |
elif CPU_temp < shutoff_temp: | |
fan_off() | |
if __name__ == '__main__': | |
try: | |
setup() | |
while True: | |
regulate_temp() | |
sleep(5) | |
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt | |
GPIO.cleanup() # resets all GPIO ports used by this program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment