Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

[solved] Controlling a fan on a relay with python

Mon Jun 01, 2015 4:34 pm

I have a Raspberry pi Model B revision 2 and I've set up a mini 5v fan on a relay switch which i want to control with python now. I will be running a crontab every hour to check the temperature of the pi and if the temperature is above 50C i will be running the fan for like 10 min.
This is my current setup:
Image

The relay is connected like this diagram here:
Image

I've setup the relay this way, because it has a light on top of it (a red light) which i want it to be on when the fan is working...and off when its not.

Here is my script:

Code: Select all

#!/usr/bin/python3
import time
import os
import sys
import RPi.GPIO as GPIO

# Identify which pin controls the relay
FAN_PIN = 3
# Temperature check. Start fan after 50C, Shut down under 50C
FAN_START = 50

def GPIOsetup():
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(FAN_PIN, GPIO.OUT)
	GPIO.setwarnings(False)
	
def getCPUtemperature():
	res = os.popen('vcgencmd measure_temp').readline()
	return(res.replace("temp=","").replace("'C\n",""))
	
def fanON():
	GPIO.output(FAN_PIN, 0)
	print "fan on"
	return()

def fanOFF():
	GPIO.output(FAN_PIN, 1)
	print "fan off"
	return()
	
def getTEMP():
	CPU_temp = float(getCPUtemperature())
	if CPU_temp>FAN_START:
		fanON()
	else:
		fanOFF()
	return()

def main():
	GPIOsetup()
	getTEMP()
	
try:
	main()
finally:
	print ("Finish")
	#GPIO.cleanup()
Now the problem is that i am getting some warnings which i just cant get rid off:

Code: Select all

root@raspberrypi:/home/pi# python fan.py
fan.py:14: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  GPIO.setup(FAN_PIN, GPIO.OUT)
fan.py:14: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(FAN_PIN, GPIO.OUT)
fan off
Finish
The line in question as you can see is this:

Code: Select all

	GPIO.setup(FAN_PIN, GPIO.OUT)
Why am I getting this error ? i have GPIO.setwarnings(False) in my code.
And also should i be using that pin for controlling the relay ? Is there optional pin i should be using ?
Thanks.
Last edited by Sandbird on Mon Jun 01, 2015 5:34 pm, edited 1 time in total.

danjperron
Posts: 3520
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Controlling a fan on a relay with python

Mon Jun 01, 2015 4:42 pm

In GPIOsetup() the first command should be
GPIO.setwarnings(False)

You remove the warning after you set the GPIO and this is why you have them.

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: Controlling a fan on a relay with python

Mon Jun 01, 2015 4:44 pm

danjperron wrote:In GPIOsetup() the first command should be
GPIO.setwarnings(False)

You remove the warning after you set the GPIO and this is why you have them.
hi, thanks for answering. I did what you said but half the warning is still there. Now i only get:

Code: Select all

root@raspberrypi:/home/pi# python fan.py
fan.py:15: RuntimeWarning: A physical pull up resistor is fitted on this channel!
  GPIO.setup(FAN_PIN, GPIO.OUT)
fan off
Finish
Should i just try another pin for controlling the relay ? If so, any suggestions ? Obviously i cant use pin gpio3 so i wondering if i should avoid any other.

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: Controlling a fan on a relay with python

Mon Jun 01, 2015 5:31 pm

yup changing Pins fixed the errors. I am now using pin12 (GPIO018) and no more errors appear.

Here is the final script if anyone wants it.

Crontab check temperature every 1h5m. If temp > 50 then turn the fan on for 10 min.

Code: Select all

5 * * * * sudo python /home/pi/fan.py
fan.py

Code: Select all

#!/usr/bin/python3
import time
import os
import sys
import RPi.GPIO as GPIO

# Identify which pin controls transistor
FAN_PIN = 18 # the yellow box ex: GPIO18
# Temperature check. Start fan after 50C, Shut down under 50C
FAN_START = 50

def GPIOsetup():
	GPIO.setwarnings(False) 
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(FAN_PIN, GPIO.OUT)
	
def getCPUtemperature():
	res = os.popen('vcgencmd measure_temp').readline()
	return(res.replace("temp=","").replace("'C\n",""))
	
def fanON():
	GPIO.output(FAN_PIN, 0)
	#print "fan on"
	return()
def fanOFF():
	GPIO.output(FAN_PIN, 1)
	#print "fan off"
	return()
	
def getTEMP():
	CPU_temp = float(getCPUtemperature())
	if CPU_temp>FAN_START:
		fanON()
		count = 1
		while count <= 10:
			count = count + 1
			time.sleep(60) # Run the fan for 10min
	else:
		fanOFF()
	return()

def main():
	GPIOsetup()
	getTEMP()
	fanOFF()
	
try:
	main()
finally:
	#print ("Finish")
	GPIO.cleanup()
New configuration:
Image

danjperron
Posts: 3520
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: [solved] Controlling a fan on a relay with python

Tue Jun 02, 2015 6:45 pm

Pin 3 and 5 have a pull up resistor for the I2C. This is why you have that warning.
Anyway it is better that you let the I2C free for other project ;-)

Your schematic is confusing. I know that you are using a relay with an active driver but you choose a simple relay layout. Next times put a box around it and just specify the VCC, GND and IN.

Daniel
Last edited by danjperron on Tue Jun 02, 2015 7:07 pm, edited 1 time in total.

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: [solved] Controlling a fan on a relay with python

Tue Jun 02, 2015 6:46 pm

yeah i used another gpio and its working fine now.
I am still a noob with all this...next project will be better detailed :)

TxRider
Posts: 16
Joined: Tue Aug 14, 2018 4:45 pm

Re: [solved] Controlling a fan on a relay with python

Thu Nov 15, 2018 7:18 am

Will the fan relay be controlled by the CPU temp, if not how could I make it read off of my CPU temp?

Thank you,

Ken

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: [solved] Controlling a fan on a relay with python

Thu Nov 15, 2018 10:09 am

Here you go: https://www.instructables.com/id/Automa ... an-for-Pi/
Temp functions are inside the script

raymate
Posts: 99
Joined: Sat Sep 28, 2013 3:56 pm
Location: Canada / UK
Contact: Website

Re: [solved] Controlling a fan on a relay with python

Mon Jan 21, 2019 12:22 am

Sandbird wrote:
Thu Nov 15, 2018 10:09 am
Here you go: https://www.instructables.com/id/Automa ... an-for-Pi/
Temp functions are inside the script
Want to build something like this but want to use an external temp sensor such as the DHT11, any tutorial you can recommend.

So using a replay to control external fan with external temp sensor

Thanks
_______________________________________
Pi model B / Pi 2 / Pi 3 / Pi 4 / Pi Zero W

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: [solved] Controlling a fan on a relay with python

Mon Jan 21, 2019 12:32 am

I know its doable with arduino, (even the arduino pro micro, you don't have to go big with the bulky arduino uno). Like this video here: https://www.youtube.com/watch?v=xD8wHXDzLkQ
Just ignore anything related to the TM1637 display, and you get exactly what you want...something to turn on a device (using a relay) over a fixed temperature and turn it off below that.

raymate
Posts: 99
Joined: Sat Sep 28, 2013 3:56 pm
Location: Canada / UK
Contact: Website

Re: [solved] Controlling a fan on a relay with python

Mon Jan 21, 2019 1:07 am

Sandbird wrote:
Mon Jan 21, 2019 12:32 am
I know its doable with arduino, (even the arduino pro micro, you don't have to go big with the bulky arduino uno). Like this video here: https://www.youtube.com/watch?v=xD8wHXDzLkQ
Just ignore anything related to the TM1637 display, and you get exactly what you want...something to turn on a device (using a relay) over a fixed temperature and turn it off below that.
Thanks for the reply, but I use Pi I dont want to start buying more controller boards, its just another thing to lean :)
_______________________________________
Pi model B / Pi 2 / Pi 3 / Pi 4 / Pi Zero W

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: [solved] Controlling a fan on a relay with python

Mon Jan 21, 2019 1:17 am

raymate wrote:
Mon Jan 21, 2019 1:07 am
Thanks for the reply, but I use Pi I dont want to start buying more controller boards, its just another thing to lean :)
I just found this library here for the pi: https://tutorials-raspberrypi.com/raspb ... t11-dht22/
This should work just fine, with my script.
Looking at the example script here: https://github.com/adafruit/Adafruit_Py ... pletest.py
all you have to do is import the library, set up a pin number and use : humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
to get the temperature value...then use that value to return it in my function (instead of return(res.replace("temp=","").replace("'C\n","")))
You might need to play with that return value a bit, maybe it's not formatted properly (int, float etc) but i dont see why it shouldnt work.

raymate
Posts: 99
Joined: Sat Sep 28, 2013 3:56 pm
Location: Canada / UK
Contact: Website

Re: [solved] Controlling a fan on a relay with python

Mon Jan 21, 2019 1:20 am

Sandbird wrote:
Mon Jan 21, 2019 1:17 am
raymate wrote:
Mon Jan 21, 2019 1:07 am
Thanks for the reply, but I use Pi I dont want to start buying more controller boards, its just another thing to lean :)
I just found this library here for the pi: https://tutorials-raspberrypi.com/raspb ... t11-dht22/
This should work just fine, with my script.
Looking at the example script here: https://github.com/adafruit/Adafruit_Py ... pletest.py
all you have to do is import the library, set up a pin number and use : humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
to get the temperature value...then use that value to return it in my function (instead of return(res.replace("temp=","").replace("'C\n","")))
You might need to play with that return value a bit, maybe it's not formatted properly (int, float etc) but i dont see why it shouldnt work.
Nice, that looks a bit more like what I can start with, cheers
_______________________________________
Pi model B / Pi 2 / Pi 3 / Pi 4 / Pi Zero W

Sandbird
Posts: 29
Joined: Tue Feb 10, 2015 12:45 pm

Re: [solved] Controlling a fan on a relay with python

Mon Jan 21, 2019 1:27 am

raymate wrote:
Mon Jan 21, 2019 1:20 am
Nice, that looks a bit more like what I can start with, cheers
Glad i could help :)
btw, you should really check the Arduino pro micro as well and/or WeMos D1 mini ESP8266. Just using the esp8266, a DHT22 and a usb cable i am sending values to thingspeak and have my own mini weather station (http://sandpi.tk), all that from a device the size of your thumb :P

Return to “Python”