Skip to content

Instantly share code, notes, and snippets.

Created January 2, 2017 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ff99881224e6bb0bd52d9ff60f639788 to your computer and use it in GitHub Desktop.
Save anonymous/ff99881224e6bb0bd52d9ff60f639788 to your computer and use it in GitHub Desktop.
Python Code for Controlling Fan
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO
pin = 40 #GPIO Pin that controls fan
maxTemp = 58 #Temp in celcius fan starts spinning faster
minTemp = 56 #Min temp before fan starts slowing down
dc = 20 #Starting Duty Cycle
time = .25 #Polling time
step = .25 #Amount to change duty cycle by
spin = 39 #GPIO Pin checking for voltage
def setup(): #Initial Setup
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
GPIO.setup(spin, GPIO.IN)
global p
global time
p=GPIO.PWM(pin, 60)
p.start(dc)
return()
def getTemp(): #Get output of "vcgencmd measure_temp" into a normal number
res = os.popen("vcgencmd measure_temp").readline()
temp =(res.replace("temp=","").replace("'C\n",""))
return temp
def setFan(): #Sets Duty Cycle of PWM to control fan speed based on temperature
global dc
global CPUTemp
CPUTemp = float(getTemp())
if CPUTemp>maxTemp:
dc = dc + step
if CPUTemp<minTemp:
dc = dc - step
return()
def writeInfo(): #Function to write current temp and speed to file for reference
global CPUTemp
global dc
info = open("faninfo", "w")
info.write("Temperature is {0}".format(CPUTemp) + "'C. Fan Speed is {0}".format(dc) +"%")
info.close()
return()
def checkPower() #Function to check for DC source power, and power off when it is cut
if GPIO.input(spin) = 0
os.system("sudo poweroff")
return()
try:
setup()
while True:
global p
setFan()
if dc>=100:
dc = 100
if dc <= 0:
dc = 0
p.ChangeDutyCycle(dc)
writeInfo()
#checkPower()
sleep(time)
except KeyboardInterrupt:
GPIO.cleanup()
@Crono141
Copy link

Crono141 commented Mar 2, 2017

I've updated this script per the below. Added a few bells and whistles.

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO

freq=60 #PWM frequency
pin = 12 #GPIO Pin that controls fan
maxTemp = 60 #Temp in celcius fan starts spinning faster
minTemp = 58 #Min temp before fan starts slowing down
dc = 100 #Starting Duty Cycle
time = .25 #Polling time
step = .25 #Amount to change duty cycle by
spin = 36 #GPIO Pin checking for voltage
dcmin = 35 #Min fan speed percentage. Lower values result in no fan spinning

def setup(): #Initial Setup
	GPIO.setwarnings(False)
	GPIO.cleanup()
	GPIO.setmode(GPIO.BOARD)
	GPIO.setup(pin, GPIO.OUT)
	GPIO.setup(spin, GPIO.IN)
	global p
	global time
	p=GPIO.PWM(pin, freq)
	p.start(dc)
	global connected
	connected = GPIO.input(spin) #Check if power sensor pin is connected at start
	return()
	
def getTemp(): #Get output of "vcgencmd measure_temp" into a normal number
	res = os.popen("vcgencmd measure_temp").readline()
	temp =(res.replace("temp=","").replace("'C\n",""))
	return temp

	
def setFan(): #Sets Duty Cycle of PWM to control fan speed based on temperature
	global dc
	global CPUTemp
	CPUTemp = float(getTemp())
	if CPUTemp>maxTemp:
		if dc <= 0:
			dc = dcmin
		dc = dc + step
	if CPUTemp<minTemp:
		dc = dc - step
		if dc <=dcmin:
			dc = 0
	return()
	
def writeInfo(): #Function to write current temp and speed to file for reference
	global CPUTemp
	global dc
	info = open("faninfo", "w")
	info.write("Temperature is {0}".format(CPUTemp) + "'C. Fan Speed is {0}".format(dc) +"%" + " Sensor is {0}".format(connected))
	print("Temperature is {0}".format(CPUTemp) + "'C. Fan Speed is {0}".format(dc) +"%" + " Sensor is {0}".format(connected))
	info.close()
	return()
	
def checkPower(): #Function to check for DC source power, and power off when it is cut
	global p
	if GPIO.input(spin) == 0:
		p.stop
		os.system("sudo halt")
	return()
	
try:
	setup()
	while True:
		global p
		global connected
		setFan()
		if dc>=100:
			dc = 100
		if dc <= 0:
			dc = 0
		p.ChangeDutyCycle(dc)
		writeInfo()
		if connected:
			checkPower()
		sleep(time)
		

except KeyboardInterrupt:
	GPIO.cleanup()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment