FanGPIO - PWM fan control script
-
FanGPIO
A small script to turn your fan on and off via GPIO and PWM
Through this together real quick last night, it's crude but does the job. HAPPY PI DAY!
How does it work?
FanGPIO starts when your pi boots up and runs in the background to adjust the pi's fan speed to your needs
- At 40°C the Fan turns on spinning at 25%RPM
- At 50°C the Fan goes up to 50%RPM
- At 60°C the Fan goes up to 75%RPM
- At 70°C the Fan fan is set to full 100%RPM
- The Fan wont turn back off until it cools down to 38°C (this keeps it from turning on and off if your at that 40°C threshold)
Will this hurt my RetroPie performance?
FanGPIO is a super small script written in C to avoid any additional stress on your pi. I have not completed thorough tests yet however so far the max CPU usage I have noticed is 0.9% across one of the 4 CPU threads.
Instructions:
Do not use this directly connected to a fan, you will likely blow your gpio pin! (read below)
How to install the script:
To install this simple script on your raspberry pi copy and paste the following
git clone https://github.com/soulgriever/FanGPIO cd FanGPIO sudo sh install.sh
Reboot your pi and the script is installed, yup its that simple!
How to wire up the Pi:
I recommend using either a Logic Level Power Mosfet such as the one Here, or using a transistor such as a TIP31C.
- Identify the base, collector, and emitter pins of your transistor/power mosfet.
- Attach the Base Pin on the transistor to Pin 12 or GPIO18.
- Attach the Collector to your common ground (Any ground pin on your GPIO or an external power supply).
- Attach the Emitter to the negative terminal on your fan.
- Finally attach the positive terminal of your fan to the 5v pin of your pi or external power supply
-
Hi,
Will this work with a fan connected to a retroflag nespi case?
The fan is hooked up to the mainboard of the case. You can see the fan pins on the right here:Thank you.
-
@soulgriever I can't get this working. I followed your instructions using a TIP31C transistor. My fan will not turn on. My Pi got up to 46°C and the fan never turned on. On the chance that I connected the wires to the transistor backwards, I tried reversing the order they were connected. Then my fan started running, but it seemed like it was at full speed all the time. Then the Pi cooled down to 36°C but the fan kept going. Then I shut the Pi down and let it sit and the fan kept going until I unplugged the Pi. I'm pretty sure I connected the wires right initially (not reversed) so I don't know why the fan wouldn't turn on when the Pi started heating up. Can you help me figure out what is wrong?
-
@soulgriever I figured it out. There were 2 problems. One might just be unique to me but the other is a problem with your install script that will affect anyone trying to use this.
The line:
echo "sudo /home/pi/.FanGPIO/FanGPIO" >> /etc/rc.local
will just add the command to start FanGPIO to the end of rc.local. The problem is the last line of rc.local must be "exit 0" and anything after "exit 0" won't run. The way your script works, the command is added at the very end after the preexisting "exit 0" so FanGPIO never runs at boot unless you manually edit rc.local to move "exit 0" to the end.The other problem, possibly just my fan is that 25% power isn't enough to actually get it running. Even running FanGPIO manually before I figured out the issue with rc.local my fan would not start spinning after my Pi hit 40°C. But then I noticed when I would try running it manually the fan would jerk slightly for a split second like it was trying to start and it would emit a faint buzz until I killed FanGPIO. That told me it wasn't getting enough power. I edited FanGPIO.c to raise where the fan starts at to 50% and that was enough to get the fan going. I don't know if 25% is generally a bit too low of a starting point for most fans used with the Pi or if my fan is just picky.
-
I would like to recommend the following reading, which I have applied to my Raspberry pi
https://descubriendolaorangepi.wordpress.com/2017/06/20/gpio-en-c-control-automatico-de-ventilador-segun-la-temperatura/
I usually use this other script in Python
https://pastebin.com/EsbQEJzw
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.