Advertisement
talofer99

rpi temperature control script

Aug 4th, 2020
2,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. # code by talofer99@hotmail.com
  2. # temp control with cooling fan.
  3. # connect a fan via a npn transistor.
  4. # powe from the 5V of the rpi.
  5.  
  6. import os
  7. import time
  8. import RPi.GPIO as GPIO
  9. fanPin = 26
  10. GPIO.setmode(GPIO.BCM)
  11. GPIO.setup(fanPin, GPIO.OUT)
  12. threshHoldTempOn = 45
  13. threshHoldTempOff = 40
  14. pwmPrecentCycle = 75
  15. p = GPIO.PWM(fanPin, 200)  # frequency=200Hz
  16. p.start(0)
  17.  
  18. def measure_temp():
  19.         temp = os.popen("vcgencmd measure_temp").readline()
  20.         return float((temp.replace("temp=","").replace("\'C\n","")))
  21.  
  22. try:
  23.     while True:
  24.         currentTemp = measure_temp()
  25.         if currentTemp >= threshHoldTempOn:
  26.             p.ChangeDutyCycle(pwmPrecentCycle)
  27.             #GPIO.output(fanPin, GPIO.HIGH)
  28.             print ("hot")
  29.         elif currentTemp <= threshHoldTempOff:
  30.             p.ChangeDutyCycle(0)
  31.             #p.stop()
  32.             #GPIO.output(fanPin, GPIO.LOW)
  33.             print ("ok")
  34.         else:
  35.             print ("no chnage")
  36.         print(currentTemp)
  37.         print("----------------")
  38.        
  39.         time.sleep(30)
  40.  
  41. except KeyboardInterrupt:
  42.     GPIO.cleanup()
  43.     print("Press Ctrl-C to terminate while statement")
  44.     pass
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement