Fan GPIO and code control
-
Hi!, I wanted to put a fan to my rpi3 intentionally for overclock N64, PSP and Dreamcast games (anyway for metal slug or some PSX games...).
So, I read about to use GPIO pin 4 for red cable and pin 6 for black cable, but the fan will be always ON, and I think that its better and logical to run it at certain temperature...
Well, the question is if could be any problem with the fan always ON (using those 4 and 6 GPIO pins) AND if anyone knows some clear code for control it too.
In addition, I gather this webs/codes but I can´t value them (cause I´m not knowledged at this...):
https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=133251
https://github.com/TheDrHax/rpi-fan-control
Thx in advance, I´ll try to show this to a programmer friend too XDDD.
-
AWESOME POST! This is exactly what I need for my upcoming project!
-
I didn't watch the video intently but I highly recommend you do you wiring like in the video, it looks similar to my setup. If you DO NOT and you simply just plug your fan into your pi it will continue to run even after you shut it down!
I didn't do any fancy temperature shenanigans. I figure its more efficient to set it and forget it instead of running a script 24/7 constantly checking the temperature. I don't know how efficient that would be tbh, I'd like somebody else to chime in.
Python Code
- Save this code in a file called fan.py in /home/pi
-Edit line 16 to match your pin.
-http://pastebin.com/gusfQu4P
Run script at boot:
- Edit the /etc/rc.local file and add the following line ABOVE 'Exit 0'
- python /home/pi/fan.py &
That should be it if your wiring is correct. Also can somebody check my script, its been awhile.
- Save this code in a file called fan.py in /home/pi
-
It´s not a bad idea tehswiftone; if you check the link of https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=133251 that I posted before, one guy says that the cpu works always 30% more due to the temperature check, BUT, the people of the forum said to him to put "times" to the petition (bad programming issue!!!)...so with a "sleep" command it´s solved.
Anyway, it´s a good idea to know the efficiency due to the fan petitions, and if this is a stupid question, excuse me, I´m not knowledge at programming.
Well, tomorrow a big ultra programmer friend is going to check this posted codes and rewrite some lines, if it goes good, and you give me time (I think we need the transistor), I´ll share it with the community.
In addition, here you got a new Pi module called PiCoolFan: https://www.modmypi.com/raspberry-pi/breakout-boards/pi-modules/picoolfan-micro-fan-cooling-system-and-rtc
-
Ok, I got news, after a large chat with my super ultra programmer friend, we choose the code from:
https://hackernoon.com/how-to-control-a-fan-to-cool-the-cpu-of-your-raspberrypi-3313b6e7f92c#.9j5869iq2BUT, we need to implement some lanes, and, this code set that the fan script is going to boot with system but isnt going to shut down when the system do (this is pretty easy to fix); the temperature range it´s easy to change too, and the fan will be on at 100% after the system reachs to this temperature, if not, it will be off.
We´re going to put the implement code here when we test it.
So, we need the NPN transistor S8050, or any similar one, well... somebody knows if we can use another model of NPN transistor?, I don´t really know why should be S8050...maybe another model it´s more correct or equivalent.
Another thing to take in mind is to get a real biiiiiiiiiig heat sink, I saw temperature graphs pretty goods without fans! (care dont break the mother board with the weight of the heat sink!!!).
Bb ^_^.
-
More news, if you want to dissipate heat, look at your rpi case (temperature test with different cases):
https://www.pretzellogix.net/2015/09/02/the-best-raspberry-pi-2-cases-compared-and-reviewed/
So I choose the aluminium with fan, when it arrives I´ll test the fan and code.
See ya!.
-
Ok, I got the code and the fan automated, so, cause I´m windows user I´m going to put all the instructions to make it work (Linux users knows some instructions obvious, but Win users no! Xd).
First step, you need the fan and get it connected to rpi, with a transistor (we used NPN S8050) like the next image:
For get more information, this are the GPIO pins of rpi3:Second step, (CODE), use the program Putty or WinSCP (WinSCP looks easier for Win users), in the code you could change the GPIO pin, the Temp limit to put fan ON, and the time of lecture of the temp (for choose to put or not the fan ON or OFF), I putted fan ON at 50ºC and petitions at 10 secs (for not to get the fan always getting ON and OFF every 5 secs cause the CPU reaches less temp, modify this as you wish):
#!/usr/bin/env python3 # Author: Put any name here :P import os from time import sleep import signal import sys import RPi.GPIO as GPIO pin = 18 # The pin ID, edit here to change it maxTMP = 50 # 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(10) # Read the temperature every 10 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
-
For Win users: open a notepad (.txt) and put the code into it, name the archive as "run-fan", and save it with the extension .py and with UTF-8 codification (you can found this in "save as" archive option), put that archive in a folder of rpi, per example: home/pi/fan/run-fan.py; and finally you need to give privileges to that created archive, SO, open the rpi console, go to that folder and write:
sudo chmod +x run-fan.py
Ok, now if you wanna check that your code is OK, put this in the command line and the fan should start working:
python run-fan.py
If your fan makes much noise, you could change the GPIO voltage pin of 5v to 3.3v!.
Now, third step; create the boot script for get the script working everytime when we put own rpi ON:
Open Putty or the command tool of WinSCP, go to /etc/init.d, and type:
sudo nano
Then copy and paste this code (just right mouse click):
#!/bin/sh ### BEGIN INIT INFO # Provides: run-fan.py # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start fan script at boot time # Description: Enable service provided by daemon. ### END INIT INFO python /home/pi/fan/run-fan.py
And press CTRL+O for save the changes, giving the archive the name of "fan" and then CTRL+X for exit;
Fourth step, now just we neet to say the rpi to execute the code at boot, type:
sudo update-rc.d fan defaults
If you did all the stepts right, then you got it, reboot and you got you fan automated, anyway, you could make the stept 2 as the step 3 (with putty: open putty, type sudo nano......etc), in addition, try different Temps range and Sleep times, if you want to put always the fan ON just put a low temp, if you want to put the fan on only with hight temperatures, check the time sleep cause the fan will be going to ON and OFF every X secs and it could be a little chaos.
Well, Linux users get free of post recommendations or corrections of this post XD (I said it, I´m a Win User).
Finally, if you wanna check your rpi temp type:
/opt/vc/bin/vcgencmd measure_temp
BB ^_^.
-
-
After some test I get 45-55ºC with fan always ON and aluminium alloy rpi case (but with the fan above the micro), BUT, I think it´s not really necesary to automate the fan, cause making the fan Off and On continuously due to the Temp range choosen and Sleep time petitions is a truly chaos and it couldn´t be good for the Fan life time... so, I think in two variables, OR a real time fan with variable speed OR go to the easy method with a fan with a switch :p.
Bb ^_^
-
@tehswiftone said in Fan GPIO and code control:
I didn't watch the video intently but I highly recommend you do you wiring like in the video, it looks similar to my setup. If you DO NOT and you simply just plug your fan into your pi it will continue to run even after you shut it down!
I didn't do any fancy temperature shenanigans. I figure its more efficient to set it and forget it instead of running a script 24/7 constantly checking the temperature. I don't know how efficient that would be tbh, I'd like somebody else to chime in.
Python Code
- Save this code in a file called fan.py in /home/pi
-Edit line 16 to match your pin.
-http://pastebin.com/gusfQu4P
Run script at boot:
- Edit the /etc/rc.local file and add the following line ABOVE 'Exit 0'
- python /home/pi/fan.py &
That should be it if your wiring is correct. Also can somebody check my script, its been awhile.
Not sure if you are still monitoring @tehswiftone but I wanted to confirm with your code you are saying that when the pi is powered down (sleep) with a button then the fan does indeed also shut down? are you running a resistor as well to accomplish this since I am seeing that you are using Pin 15 and am assuming that you are using a 5v and ground pin as well?
- Save this code in a file called fan.py in /home/pi
-
@flop said in Fan GPIO and code control:
If your fan makes much noise, you could change the GPIO voltage pin of 5v to 3.3v!.
i dont think thats a good idea....fan draws too much current....
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.