Python Forum
If/ Or if/ temp controlled fan.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If/ Or if/ temp controlled fan.
#1
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
Reply
#2
As python requires proper indentation, copy/paste your code between the [python] and [/python] tags.
Reply
#3
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()
Reply
#4
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.
Reply
#5
Nah all the indentations are there (the script) I just copy/pasted my reply from the original post.
Reply
#6
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Deleting Windows temp folder Raysz 7 434 Apr-02-2024, 12:36 PM
Last Post: Raysz
  Print controlled with global-var fmr300 3 1,461 Feb-05-2022, 03:09 PM
Last Post: fmr300
  pyspark creating temp files in /tmp folder aliyesami 1 4,998 Oct-16-2021, 05:15 PM
Last Post: aliyesami
  Help with storing temp data for each day then recording min/max in app. trthskr4 3 2,414 Sep-10-2021, 10:51 PM
Last Post: trthskr4
  How to save Matplot chart to temp file? Morkus 2 4,531 Jun-12-2021, 10:52 AM
Last Post: Morkus
  Get system info from PI (cpu load and temp) korenron 2 2,099 Aug-04-2019, 08:45 AM
Last Post: korenron
  temp analysis Simba 13 5,902 Apr-25-2019, 09:13 PM
Last Post: Simba
  Temp folder creation ste1605 7 5,287 Oct-03-2018, 10:39 AM
Last Post: buran
  Problem with remove Temp Files karlo_ds 1 3,158 Oct-26-2017, 11:42 PM
Last Post: wavic
  How to create a value system with a temp/humidity sensor? Tacoon 2 3,437 Feb-13-2017, 03:11 PM
Last Post: Tacoon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020