Interesting.
First, I need to bring to your attention that some of your schematic diagrams are off a bit. For example, the 12 to 5 VDC converter is not drawn like that. What you show is the diagram for a transformer.
Transformers do not work with straight line DC. No big deal, just my OCD showing. LOL
As for controlling the fan, if you want it to run all the time you can simply use a 12 volt one and wire it to the monitor power, as you suggested. I think I would take it off the power from the Pi anyway, no need in sucking more power from the Pi, even though Im sure it would be ok.
But you can also use one of the GPIO pins to turn the fan on and off based on Pi temperature. But if you do this you will definitely need to use a transistor to switch the fan, because the current from a GPIO output is probably not enough to power the fan safely.
Here is a short script I wrote just for that purpose:
#!/usr/bin/env python2.7
#This checks temperature on Pi and controls a fan
#KRR 05/03/2016
# All lines with a # in front are ignored by the Pi.
import os, sys, stat
import RPi.GPIO as GPIO
import time
#set path
mypath= os.path.dirname(os.path.abspath(__file__))
if not mypath.endswith('/'):
mypath= mypath +'/'
#Pin that the fan is attached to. (PIN # not GPIO #)
fanpin = 32
polltime = 30
#Turn fan on at degrees (C) (Must be higher than off temp)
ontemp = 41 #Enter fan on temp here, in Celcius
#Turn fan off at degrees (C) (Must be lower than on temp)
offtemp = 40 #Enter fan off temp here, in Celsius
currentemp =0.0
monitor = 'False'
junk=''
#Get settings here.
if os.path.isfile(mypath + 'fantemp_settings.txt'):
setfile = open(mypath + 'fantemp_settings.txt')
for line in setfile:
if line[0:1] == '#': continue
#print line.rstrip() #use for troubleshooting
if line[0:7].lower()== 'fanpin=':
fanpin=int(line[8:].strip())
continue
if line[0:12].lower()== 'update time=':
polltime=int(line[13:].strip())
continue
if line[0:7].lower()== 'ontemp=':
ontemp=int(line[8:].strip())
continue
if line[0:8].lower()== 'offtemp=':
offtemp=int(line[9:].strip())
continue
setfile.close
else:
#If settings file dont exist, create it
print 'Setup file not found! Creating it now, and using default settings.'
setfile = open(mypath + 'fantemp_settings.txt', 'w')
setfile.write('# FanTemp by Ken Rager 11/17/2016\n')
setfile.write('\n')
setfile.write('# This is the settings file for FanTemp.\n' )
setfile.write('\n')
setfile.write('# All lines beginning with # are for info only, they are ignored by the program.\n')
setfile.write('\n')
setfile.write('# The GPIO pin that the fan connects to. This is pin number and not\n')
setfile.write('# GPIO number. Pins number from 1-40. Default pin number is 32.\n')
setfile.write('\n')
setfile.write('fanpin= 32\n')
setfile.write('\n')
setfile.write('# You can set how often FanTemp checks temperature.\n')
setfile.write('# This is number of seconds to do an update.\n')
setfile.write('# You can update 1 to 60 seconds. A small number may cause CPU to\n')
setfile.write('# to work harder. A larger number may allow CPU to heat up more\n')
setfile.write('# before the fan is turned on. Experiment until you get the right\n')
setfile.write('# value. 30 is the default, and a good number to start out with.\n')
setfile.write('\n')
setfile.write('update time= 30\n')
setfile.write('\n')
setfile.write('# You can set the temperture the fan turns on. Degrees in Celius. 20-75. default is 41\n')
setfile.write('\n')
setfile.write('ontemp= 41\n')
setfile.write('\n')
setfile.write('# You can set the temperture the fan turns off. Degrees in Celius. 20-75. default is 40.\n')
setfile.write('# Note: offtemp must be lower than ontemp. If not, program will set it one degree lower.\n')
setfile.write('\n')
setfile.write('offtemp= 40\n')
setfile.write('\n')
setfile.write('# Change the values above to what you desire, then save this settings file.\n')
setfile.write('# You must restart FanTemp after saving this file.\n')
setfile.write('# You can run FanTemp in monitor mode to see how it is working.\n')
setfile.write('# In the terminal prompt, type python fantemp.py monitor\n')
setfile.write('# That will print out to the screen to allow you to see what is going on.\n')
setfile.close
# change mode of settings file so anyone can read or write to it.
os.chmod("fantemp_settings.txt", stat.S_IRWXO)
# Check for proper values.
if fanpin < 1 or fanpin > 40: fanpin=32
if polltime < 1 or polltime > 60: polltime = 30
if ontemp < 20 or ontemp > 75: ontemp = 41
if offtemp < 20 or offtemp > 75: offtemp = 40
if offtemp >= ontemp: offtemp = ontemp -1
GPIO.setmode(GPIO.BOARD) #This allows me to access the I/O by pin numbers not GPIO ID
GPIO.setwarnings (False)
# Set output
GPIO.setup(fanpin, GPIO.OUT) #Fan control: True= on False= off
#Turn fan on for at startup
GPIO.output(fanpin,True);
def getCPUtemperature():
#Gets CPU temperature, and cleans up the degrees C at end.
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
currentemp = float(getCPUtemperature())
def monitormode():
print '\n Monitor mode! Press CTRL + C to exit'
print ' GPIO pin that fan circuit is attached to: ' + str(fanpin)
print ' Sample time = ' + str(polltime) + ' seconds.'
print ' Temperture to turn fan on: ' + str(ontemp)
print ' Temperture to turn fan off: ' + str(offtemp)
#print ' Ontemp = ' + str(ontemp) + ' offtemp= ' + str(offtemp)
print ' Current temp is ' + str(currentemp)
if GPIO.input(fanpin):
print ' fan is on'
else:
print ' fan is off'
print ' ******'
if sys.argv[1:]== ['monitor']:
monitor= True
print '\n Fantemp by Ken Rager 2016'
while True:
currentemp = float(getCPUtemperature())
if currentemp <= offtemp: GPIO.output(fanpin,False); # CPU temp is less than or equal to offtemp so turn fan off. DO NOT COMMENT OUT OR DELETE THIS LINE!
if currentemp >= ontemp: GPIO.output(fanpin,True); # CPU temp is equal to or higher than ontemp so turn fan on. DO NOT COMMENT OUT OR DELETE THIS LINE!
if monitor== True: monitormode()
time.sleep(polltime) # pause time before checking temp
Good luck with your project, I will be checking in to see how its going.