Python Forum

Full Version: If/ Or if/ temp controlled fan.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
As python requires proper indentation, copy/paste your code between the [python] and [/python] tags.
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
Now, instead of using else: fanoff, on row 23, could I use
or
if CPU_tmp<minTMP
fanoff()
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.
Nah all the indentations are there (the script) I just copy/pasted my reply from the original post.
You need something like this: https://python-forum.io/Thread-Relay-swi...6#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.