talofer99

rpi temperature control script

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