shawn-pi-noob
Posts: 10
Joined: Mon Jun 10, 2013 6:37 am
Location: Meridian, MS, USA

fan pi in-cooperation

Wed Jun 12, 2013 12:41 am

My plan is to create a python program that when the pi gets past 50 degrees is will turn on a cpu fan. I need help on getting python to read the code

Code: Select all

cat /sys/class/thermal/thermal_zone0/temp
and when it gets below 40 it will turn the fan off. I need ideas on how to wire it and code it. please keep simple for i am only 14 and not knowledgeable with python.

dimecoin
Posts: 2
Joined: Tue Jun 11, 2013 7:42 pm

Re: fan pi in-cooperation

Wed Jun 12, 2013 5:05 am

I never programmed in python before, but here is a quick example.

The basic logic is:

Open file and read in data.
Parse the data into a useable format; in this case we want to convert from String type to Integer [number].
Divide by 1000 to get degrees in Celsius.
Use some flow control statements to determine logic.

Code: Select all

#!/usr/bin/python

fo = open("/sys/class/thermal/thermal_zone0/temp") # opens the file
rawTemp = fo.read();  # reads contents to string
fo.close()  # cleans up our file handler

# To get temperature in Celsius we must divide value by 1000
tempInC = int(rawTemp)/1000 # casts raw data to Integer, then divides by 1000

#print(tempInC); # out puts data, uncomment to debug.

if tempInC < 40:
	print "Below 40 degrees, turning fan off. Current Temp: " +str(tempInC)
	# code to turn fan off
elif tempInC > 50:
	print "Above 50 degrees, turning fan on. Current Temp: " +str(tempInC)
	# code to turn fan on
Maybe someone more familiar with python can give us some pointers, but let me know if you have any questions on that.

I have a question for you though, what if the temperature is 41 or 48 degrees? In our logic, that isn't accounted for so that is a case we probably want to define, should the fan be on or off or we don't care or other action?

User avatar
rpdom
Posts: 17041
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: fan pi in-cooperation

Wed Jun 12, 2013 5:29 am

I don't know any Python at all, but I do know coding.

I'd add something to remember the current state of the fan, "on", or "off", with an initial state of "dunno", then in the program do something like "if temp is < 40 and state not = 'off', turn it off and set state to 'off'" and "if temp > 50 and state not = 'on', turn it on and set state to 'on'". Thi will stop the code doing the "turn it off" when it is already off.

As for the temperature range 40 to 50 being ignored, that is a good idea. The fan will stay in its current state until the temperature goes outside that range. If you had, for example "turn on if temp >45 and off if temp <45" then as soon as the temperature reaches 46 the fan will cut in for a second or two to cool down to 44 then turn off again. The temperature will rise back to 46 within a few seconds and the fan will cut back in again. Repeat forever. Having a big gap in the middle of the range will mean the fan stays on for some time, then stays off for some time.

There's still no real need for a fan on a Pi though ;)

shawn-pi-noob
Posts: 10
Joined: Mon Jun 10, 2013 6:37 am
Location: Meridian, MS, USA

Re: fan pi in-cooperation

Wed Jun 12, 2013 12:52 pm

My main plan is to overclock it extremely high which will make it get hotter than usual and keeping it cool. Now all i need to know is how to wire it. :)

User avatar
rpdom
Posts: 17041
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: fan pi in-cooperation

Wed Jun 12, 2013 1:31 pm

Ok, but set your temperature ranges to something more reasonable like fan cuts in at 75°C and off at 65°C. Anyway, unless you force it in the config.txt the overclock will be automatically throttled back if the CPU hits 85°C, which is it maximum "normal" temperature.

You can add a heatsink instead, if you think it'll look pretty ;)

Check the operating temperature range of an Intel or AMD processor - you'll find many of them run hotter than the Pi can get even with a massive heatsink and stonking great fan. I know some of my AMD based systems have an alarm set for 110°C :o

Return to “General discussion”