Automated Cooling Fan for Pi

21,655

209

56

Introduction: Automated Cooling Fan for Pi

Description
A simple design to control a mini 5v fan with python, without the need of a breadboard, transistors etc. All you need are a few cables and a 1 channel relay. I had a 2 channel relay which i recommend, since its almost the same price plus you get an extra controller. A script executing every hour will check the pi's temperature and turn on/off a fan until the desired temperature is reached.

Introduction
Since summer is upon us my secondary pi is running a bit too hot for my taste so i wanted to cool it down a bit every now and then. Of course i could have a fan blowing at it 24/7 but a) that would require a breadboard, and some resistors etc because i cant just plug it in on the GPIO pins directly b) writing some code that interacts with the 'outside' world is more fun :) and c) its really cheap...all you need are some cables and relay.

Enjoy, and feel free to comment !

Step 1: Materials Needed

- crontab / python installed on your pi
- A raspberry pi with a case
- 5v mini fan (link)
- 2 channel relay (link)
- Couple of female to female cables (link). I only had 1, so i used an old DVD audio cable for the PC and that extra f2f cable for the relay controller pin.

Note:
The hole on top of my raspberry case was a bit smaller than usual, so i had to use a drill to make it a bit wider. Maybe you should get a case with a 5v fan already attached to it, if you are not sure about the size of it on your case.

Step 2: The Circuit - Assembly

As you can see from the image above i am using pins 2,6,12, which are all the same for any type of raspberry pi you might have, so you don't have to worry about the fact that i am using a Pi Model B rev2.

Plug in the cables the same way i am doing it.
- The 5v(pin2) goes to VCC
- The GND(pin6) to the GND
- The GPIO18(pin2) goes to the IN1
Make sure the jumper on your relay is set to : JD-VCC VCC.

Now to the relay...My setup is a bit weird, i know. I didnt want to use an external power source to start the fan, so i kinda of attached it to the pi as well. You will have to make a small cut to both the 5v and GND cable coming from the pi to attach the fan cables the same way i am doing it. You can use a soldering iron and some tape like i did.
I dont think there is a reason to worry about bricking your pi, because only the ground cable is directly connected to the pi. If you see carefully at the image, the relay switch is connected to the power cable, meaning when the fan is off, the direct connection with the pi is 'open' as well. So any electrical current the fan might produce when it goes off, will have no way of returning back to the pi.

The reason why i am using those gates on the relay, is because the relay has a tiny led on top of it. I wanted to see the red light when the fan in ON, so i know when the pi is cooling down.
If you want, you could use the other gates so that the reverse thing happens.
But you would probably need to reverse the GPIO commands in the python script (in functions fanON fan OFF shown in the next step) to make it work...You'll see what i mean when we get to it.

Step 3: The Script

Now don't freak out about scripting... Last week i didn't know python and yet i managed to write this script by reading and testing various examples out there. I know other programming languages though so dont worry, i am not gonna blow up your pi.
If i used (which i probably did) some functions that someone else wrote, i apologize for no credits given, but i've tweaked this script so much, its basically new.

Instructions

1) Download the attachement, or just copy/paste it from pastebin here and name it fan.py

2) Place the fan.py script in /home/pi/ folder

3) Execute: sudo chmod +x /home/pi/fan.py and sudo chown pi:pi /home/pi/fan.py

4) Assuming you have crontab installed on your pi execute: crontab -e

5) At the bottom copy / paste: 5 * * * * sudo python /home/pi/fan.py and save the file. (Ctrl+X and Y)

This crontab will execute the script every 1h5m.
The script will work as a standalone script as well...meaning besides the automated crontab action you can manually turn on/off the fan yourself. You do this using parameters like so:

  • sudo python /home/pi/fan.py on or
  • sudo python /home/pi/fan.py off

I also wrote a failsafe function in case you press Ctrl+C while the script is running. If you do, the fan will shut down before the script exits.


So, how this works ?

Every hour the script executes and checks the pi's temperature.
If the temperature is above an X value the fan will turn on and stay on until it cools down to a Y value. When it does, the script will exit. If for some reason it never reaches that low temperature and an hour passes, the next time the script executes it will 'see' that the fan is still on and the second script will quit...so use realistic temperature values if you want the fan to actually turn on/off.
If not, just set some ridiculous values (like Y = 0 degrees Celsius) so that the fan will always stay on.)
If your pi is operating between those 2 values (X,Y), it means that its operating under 'accepted' temperatures so the script will just exit when it checks the temperature every hour.

Explaining some values

At the top of the script there are some global variables. There are the variables you have to change.

# Identify which pin controls the relay
FAN_PIN = 18 # the yellow box ex: GPIO18
# Temperature check. Start fan if temp > 49C
FAN_START = 49
# Temperature check. Shut down under 28C
FAN_END = 28

Its pretty obvious what to do here. If you used GPIO18 like i did, then leave 18 here, otherwise change that value to the gpio you used.
FAN_START and FAN_END are the high/low temperatures you want use. You can even put float numbers there if you want, like 49.2


Note:

Remember the way i set up my relay ? If you see functions fanON and fanOFF you'll notice that i am setting the gpio output to False to turn the fan on and True to turn it off. If you chose another gate configuration on the relay, you'll probably need to reverse those values.

Attachments

Step 4: Final Notes

I apologize for the detailed instructions. I wanted to cover all types of users.
There is probably a better way to do all this but i did the best i could with the materials in hand and with my noobness in circuitry and python.

A small bonus code

If you want a fast way to check the temperature of your pi, create a file called temp in /usr/local/bin folder and then copy paste this script inside:

http://pastebin.com/rUYqGjV5

execute: chmod +x /usr/local/bin/temp to make it executable.

Then just type temp from whichever folder you are to see your pi's temperature.

1 Person Made This Project!

Recommendations

  • For the Home Contest

    For the Home Contest
  • Make It Bridge

    Make It Bridge
  • Game Design: Student Design Challenge

    Game Design: Student Design Challenge

56 Comments

0
mickael.vc
mickael.vc

Question 2 years ago

Hello :)
First of all I wanted to thank you for this tutorial. Really cool!
I need a little helping hand to finish it.
When I use this command line the relay is activated and everything is fine. When I activate the crontab nothing happens. When I try the python fan.py or sudo python fan.py command I get this message "Fan is on, script must be running from another instance ...".
The only differences with your tutorial are:
- the wiring is slightly different. I am connected to the gpio 37 (BCM 26).
- I inverted the values of the functions fanON () & fanOff ()
I also used the temp script which works great.
Do you have an idea?
Thanks for reading me

0
Sandbird
Sandbird

Answer 2 years ago

Hi there,
That's a tricky one..I think that value inverse you did might did the bug. My guess is when you start the script manually, the pin stays on so when the cronjob runs it says script must be running (due to the 'if check_fan(FAN_PIN)==0:')
To verify, do a manual: fan.py off
then run cronjob and wait and see if it will start.

0
mickael.vc
mickael.vc

Reply 2 years ago

Thank you for your answer!
Unfortunately when I try sudo python fan.py off I get this message:

Turning fan off
Fan is on, script must be running from another instance ...

For another test I put the original values of the functions fanON and fanOFF.
when I use the functions python fan.py on I get the following message:

Turning fan on
Fan is on, script must be running from another instance ...

and nothing happens.

when I use the functions python fan.py off

I hear the relay "click",
the console displays:
Turning fan off
55.0 @ 2020-08-20 09: 29: 45.470680
Fan is Off ... Starting Fan
55.0 @ 2020-08-20 09: 29: 45.480493
Fan is ON
54.0 @ 2020-08-20 09: 29: 50.495534
.
.
.


But the fan is not spinning. I modified the temperatures of the script so that it activates at a lower temperature

0
Sandbird
Sandbird

Reply 2 years ago

You hear a clicking sound and nothing happens? That is weird.
Why did you change the values in the fanOn and fanOff part? Any particular reason ? What happens when you revert everything back to normal and just change the PIN value.? Also...why change the pin in the first place ? Some pins dont support this kind of signals i think...Thats why i used the 'yellow' marked gpios. I am not sure what the 26 is...but try putting the pin to a 'yellow' marked one...not an 'orange' like you did.
Actually just set it up first the way it is now..and see if it works..If it does then start tweaking to your liking :)

0
mickael.vc
mickael.vc

Reply 2 years ago

Hello :)
Everything is working now. I started my wiring so that it was more consistent with your diagram, changed gpio, restored the default values.
Thank you again for your work and for your precise help! A good day :)

0
Sandbird
Sandbird

Reply 2 years ago

awesome :) enjoy buddy

0
fredondem
fredondem

2 years ago

Hello,
The script "sudo python /home/pi/fan.py" work fine manually (but I have to enter '[sudo] password for pi:') and it seem not working with CRON (same passwd pb ?)
To verify execution i made a log all 15 minutes as is suggested :

*/15 * * * * sudo python /home/pi/fan.py >> /home/pi/fanLog.log 2>&1

Here is the contain of the log :
"sudo: no tty present and no askpass program specified"

An idea ? Thank's

0
Sandbird
Sandbird

Reply 2 years ago

hmm, did you do: "sudo chown pi:pi /home/pi/fan.py"
if your running the crontab under that user (pi) then try without the "sudo python" just plain "python /home..."
What i mean is if you log in as root and then do 'su pi' to switch to the pi user and then do crontab....then you will be running the crontabs under user pi.
If you stay under root then the script will be running from the root crontab.
If you give permissions to the script to only be run under user 'pi' with chown then only that user can run the script (and root ofc).
I think this is the reason why you are getting this error.
Worst case scenario try "sudo -s python /home/pi/fan.py"

0
fredondem
fredondem

Reply 2 years ago

Hello, Thank's for your reply.
In the meantime, I found a lot of information on how crontab and python scripts work and i find a solution : Create contab with "sudo crontab -e" and after that its working.
Before, I scrupulously followed all the steps that you describe (well, I think). I'm loggin under (pi). Indeed it is a story of launch rights. I am "newbie" and I admit I do not understand all the subtleties of his commands. I'm probably missing some basics. But I will continue to try to understand what you have answered. Thanks for your help. cordially.

0
Sandbird
Sandbird

Reply 2 years ago

uh you got stuck before all that...yeah ofc, that was step 4.Good that you managed to get it working :)

0
fredondem
fredondem

Reply 2 years ago

Hello. It's me again.
In parallel, i have configured "Enclosure Plug-in" to force fan ON if i want (It cmd Gpio18). sometimes I see the green bouton "ON " even when I don't activate. It looks like a feedback from the script.
And in the log, i see many lines with "Fan is on, script must be running from another instance..."
I don't know if the two things have anything to do with each other. I try to understand what can happen in fact because i don't thing it's normal. Your opinion ? Thank's

Fan ON Enclosure Plugin.png
0
Sandbird
Sandbird

Reply 2 years ago

Did you decide to run it manually ? Did you forget to delete the cronjob then ? Because if you still have it active it will check first to see if the fan is already on...hence the msg you see..If it is, it wont fire the Fan ON function and will just wait for the next run to check again. Same thing will happen if you have manually activated the fan. Basically if the fan is on, the script will show that message and exit.

ps: if the fan gets ON on its own, then definitely the cronjob is still active. It will turn the fan on if the temperature is higher than the FAN_START value and turn it off when it's bellow FAN_END. If for some reason that value can never be reached, the script will run forever in a loop. So when the next cronjob happens it will see there is another script still running and the 2nd one will exit.

0
fredondem
fredondem

Reply 2 years ago

Hello, no i want the script work automatically. I don't activate manually in Enclosure plugin. In fact, I discover the Plugin simultaneously and i note it's seem a return of the GPIO is done inside because it displays the green button by itself. So, thanks for your explanation. I think i understand. I will survey.

0
fredondem
fredondem

Reply 2 years ago

I think i have solutioned my problem.
I deleted the launch line of the script created with "sudo crontab -e", and I created the script launch line with "crontab -e" (as you actually wrote it)
By cons, I modify the line like this : "5 * * * * python /home/pi/fan.py>> /home/pi/fanLog.log 2>&1"
You notice that i deleted "sudo" after the fourth star "*" and the script " python etc..." (Contrary to what is written in point N ° 5)
Is that what you suggested in your first response ? : "if your running the crontab under that user (pi) then try without the "sudo python" just plain "python /home..."
If that's right, I understood why it didn't work first.
I found a solution by myself... which was the one you proposed at the beginning and i have understood afterwards in fact ?
Many thank's for your help.

0
Sandbird
Sandbird

Reply 2 years ago

Yup thats what i meant...usually i run things with root privileges because some scripts need permissions to execute..it is much simpler than constantly doing sudo..and besides this script isnt doing anything dangerous so it is ok to run with root...

1
ThiagoS69
ThiagoS69

6 years ago

Hey, I follow your instructions. Worked well, except the script. Actually, if I start it by command line it work, but if it is executed by cron at, lets say, reboot, the fan will start no matter the temp, also, will never stop unless I reboot the Pi. Any thoughts?

0
jezner.gamos
jezner.gamos

Reply 3 years ago

same problem here. has anyone found any solution ?

0
Sandbird
Sandbird

Reply 3 years ago

Did you try to put a log file in your cron ?
Edit it and do something like this:
5 * * * * sudo python /home/pi/fan.py > /home/pi/fanLog.log 2>&1
then either reboot, or try to run the cron manually, copy pasting everything after the **** to your prompt, then check the log for any errors and report back

0
jezner.gamos
jezner.gamos

Reply 3 years ago

just solve the problem, thanks!

0
Sandbird
Sandbird

Reply 6 years ago

hmm, any errors when you start the script manually ?
Also are you running the cron with sudo ? I think it needs root permissions to run properly.
Also, to stop the fan, did you try the: 'sudo python /home/pi/fan.py off'
That should stop the fan, if not then probably you didnt select the proper slots on the relay. There are 3 slots. If i remember correctly depending on which slots you used, the ON functions in the script would actually do the opposite (turn the fan off) if you used the wrong setting. And then the OFF functions would turn it on instead...Basically the logic gate on the fan is reversed, if you put the cables in the wrong slots.
One last thing i could think of....when you ssh to your pi (the normal way) and type: vcgencmd measure_temp
Does it give the temperature in Celsius? (ex: temp=48.7'C)
If not then you need to change something in the code.
In the function 'get_temp_from_system' , i am 'reading' that value, but i am expecting to find the 'C part. If yours gives fahrenheit, then you need to change that 'C\n to 'F\n.
And probably recalculate the temperature values, for switching on/off the fan.