Last active
December 17, 2017 03:49
-
-
Save perlmonk/33761618c181b95d4b09e7d5f908ac15 to your computer and use it in GitHub Desktop.
Raspiberry Pi fan control by GPIO(with modification add cooling temperature range)
This file contains 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 | |
# Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com> | |
# https://hackernoon.com/how-to-control-a-fan-to-cool-the-cpu-of-your-raspberrypi-3313b6e7f92c | |
import os | |
from time import sleep | |
import signal | |
import sys | |
import RPi.GPIO as GPIO | |
pin = 18 # The pin ID, edit here to change it | |
maxTMP = 46 # The maximum temperature in Celsius after which we trigger the fan | |
stopTMP = 42 # Stop fan when temperature reach this | |
def setup(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.setwarnings(False) | |
return() | |
def getCPUtemperature(): | |
res = os.popen('vcgencmd measure_temp').readline() | |
temp =(res.replace("temp=", "").replace("'C\n", "")) | |
print("temp is {0}".format(temp)) #Uncomment here for testing | |
return temp | |
def fanON(): | |
setPin(True) | |
return() | |
def fanOFF(): | |
setPin(False) | |
return() | |
def getTEMP(): | |
CPU_temp = float(getCPUtemperature()) | |
if CPU_temp>maxTMP: | |
fanON() | |
elif CPU_temp<stopTMP: | |
fanOFF() | |
return() | |
def setPin(mode): # A little redundant function but useful if you want to add logging | |
GPIO.output(pin, mode) | |
return() | |
try: | |
setup() | |
while True: | |
getTEMP() | |
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want | |
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