![]() |
If/ Or if/ temp controlled fan. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: If/ Or if/ temp controlled fan. (/thread-7168.html) |
If/ Or if/ temp controlled fan. - clueless - Dec-24-2017 Good afternoon. I am trying to turn on a fan automatically when it reaches a certain temperature and have borrowed this code to the letter. I really don't know what I'm doing. To me, it looks like in this code the fan will turn on and off repeatedly as the temperature is lowered below the threshold every 5 seconds. It would be much more tolerable if it were to turn on at say 75°C and stay on until it drops to 60°. Now, instead of using else: fanoff, could I use or if CPU_tmp<minTMP fanoff() Would this get the desired result if I add minTMP = 60 to the beginning Also, the section at the bottom, is that intended as is? Is it all supposed to follow try: and is it saying that pressing ctrl+c will turn off the fan until the next scheduled CPU_tmp check? Thank you all for your time. pin = 18 # The pin ID, edit here to change it maxTMP = 75 # The maximum temperature in Celsius after which we trigger the fan def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT) GPIO.setwarnings(False) return() def getCPUtemperature(): res = os.popen(‘vcgencmd measure_temp’).readline() temp =(res.replace(“temp=”,””).replace(“’C\n”,””)) #print(“temp is {0}”.format(temp)) #Uncomment here for testing return temp def fanON(): setPin(True) return() def fanOFF(): setPin(False) return() def getTEMP(): CPU_temp = float(getCPUtemperature()) if CPU_temp>maxTMP: fanON() else: fanOFF() return() def setPin(mode): # A little redundant function but useful if you want to add logging GPIO.output(pin, mode) return() try: setup() while True: getTEMP() sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt GPIO.cleanup() # resets all GPIO ports used by this program RE: If/ Or if/ temp controlled fan. - squenson - Dec-24-2017 As python requires proper indentation, copy/paste your code between the [python] and [/python] tags. RE: If/ Or if/ temp controlled fan. - clueless - Dec-24-2017 pin = 18 # The pin ID, edit here to change it maxTMP = 75 # The maximum temperature in Celsius after which we trigger the fan def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT) GPIO.setwarnings(False) return() def getCPUtemperature(): res = os.popen(‘vcgencmd measure_temp’).readline() temp =(res.replace(“temp=”,””).replace(“’C\n”,””)) #print(“temp is {0}”.format(temp)) #Uncomment here for testing return temp def fanON(): setPin(True) return() def fanOFF(): setPin(False) return() def getTEMP(): CPU_temp = float(getCPUtemperature()) if CPU_temp>maxTMP: fanON() else: fanOFF() return() def setPin(mode): # A little redundant function but useful if you want to add logging GPIO.output(pin, mode) return() try: setup() while True: getTEMP() sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt GPIO.cleanup() # resets all GPIO ports used by this programNow, instead of using else: fanoff, on row 23, could I use or if CPU_tmp<minTMP fanoff() RE: If/ Or if/ temp controlled fan. - bogo10 - Dec-24-2017 It looks like you used an standard editor (no identations at all). For Beginners it should be better to use a special code editor, it will help the first steps. RE: If/ Or if/ temp controlled fan. - clueless - Dec-24-2017 Nah all the indentations are there (the script) I just copy/pasted my reply from the original post. RE: If/ Or if/ temp controlled fan. - DeaD_EyE - Dec-25-2017 You need something like this: https://python-forum.io/Thread-Relay-switches-On-and-Off-every-cylcle?pid=31756#pid31756 Temperature has the behavior, that it's changing slowly the value. With the wrong implementation you can destroy the relay. In the case when it's switching on-off in a very short time, the whole time, at some point the relay will stop working. I know from real life a case, where a company implemented the temperature control with a relay to control a heater for Plastic (high power consumption). They tried really to implement PWM with a relay :-D The whole building burned. A much better approach is, to control your small fan with a PWM module, called H-Bridge. You can drive your fan faster, when the temperature is higher and slower, when it's getting colder. I'm using this for our radar systems to control the DC-Drive: https://photos.app.goo.gl/iZ2KqBI3jrMnCP2c2 You can buy it here for example: https://www.sparkfun.com/products/9457 But you'll find it in other shops also. |