Skip to content

Instantly share code, notes, and snippets.

@zhouziqunzzq
Last active February 28, 2017 18:23
Show Gist options
  • Select an option

  • Save zhouziqunzzq/cf16f895f4b76a1de64ba36527025450 to your computer and use it in GitHub Desktop.

Select an option

Save zhouziqunzzq/cf16f895f4b76a1de64ba36527025450 to your computer and use it in GitHub Desktop.
Pi Fan Tem Ctl
#!/usr/bin/env python
# encoding: utf-8
import RPi.GPIO as GPIO
import time
fan_pin = 7
alert_temp = 48000
speed = 0
prv_temp = 0
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(fan_pin,GPIO.OUT)
pwm = GPIO.PWM(fan_pin,100)
try:
while True:
tf = open('/sys/class/thermal/thermal_zone0/temp')
cpu_temp = int(tf.read())
print(cpu_temp)
tf.close()
if cpu_temp >= alert_temp:
if prv_temp < alert_temp:
pwm.start(0)
pwm.ChangeDutyCycle(100)
time.sleep(0.1)
speed = max(10,min(cpu_temp / 20 - 2400, 100))
#print(speed)
pwm.ChangeDutyCycle(speed)
else:
pwm.stop()
prv_temp = cpu_temp
time.sleep(5)
except KeyboardInterrupt:
pass
pwm.stop()
GPIO.cleanup()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment