Skip to content

Instantly share code, notes, and snippets.

@YourFriendCaspian
Created June 26, 2019 13:32
Show Gist options
  • Save YourFriendCaspian/81d532ac0d9a6a83ac71b340d3017b3d to your computer and use it in GitHub Desktop.
Save YourFriendCaspian/81d532ac0d9a6a83ac71b340d3017b3d to your computer and use it in GitHub Desktop.
Raspberry Pi Fan Control Script
#Raspberry Pi Fan Control Script
import RPi.GPIO as IO
from gpiozero import CPUTemperature
import time
minspin = 10
IO.setwarnings(False)
IO.setmode(IO.BCM)
IO.setup(14,IO.OUT)
fan = IO.PWM(14, 100)
cpu = CPUTemperature()
fan.start(0)
oldtemp = cpu.temperature
while True:
temp = cpu.temperature
if abs(temp - oldtemp) < 1.5:
time.sleep(1)
print(str(temp)+" skipped")
continue
oldtemp = temp
if temp > 50:
speed = ((temp - 45)*4)+minspin
if speed > 100:
speed = 100
fan.ChangeDutyCycle(speed)
print(str(temp)+" "+str(speed)+" fan on")
else:
fan.ChangeDutyCycle(0)
print(str(temp)+" fan off")
time.sleep(1)
fan.stop()
IO.cleanup()
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment