I just got a fan for my Raspberry Pi 2 model B. It uses the GPIO pins. The fan works well but makes a lot of noise. I want to get the fan to switch on and off depending on the temperature of the pi. I've spent the whole weekend googling it and tried everything i could find. I couldn't get it to work (I'm a noob). I'm using Raspbian Jessie.
-
If you have tried everything and it does not work then clearly your fan or the Pi is faulty. In that case there is no point in us regurgitating something you have already tried.– joanCommented Nov 24, 2015 at 4:54
-
1@joan I don't think sarcasm works!– MilliwaysCommented Nov 24, 2015 at 5:27
-
As you have been told before. You don't need a fan. None of the experienced users have a fan. Just put it down to experience, and put it in the bin.– MilliwaysCommented Nov 24, 2015 at 5:29
-
As Milliways says, unless you are overclocking the fan is totally superfluous. Some distributors include them because there's no harm in doing so, it makes the package look fancier, and the fan probably costs them ~$0.50. However, as Entrabiter says, getting it to turn on and off would be a good exercise. I've noticed there can be a bit of a range in resting temp, at least between models. If you start by setting it to go off at 50 °C you should find it's quiet most of the time and will let you know when something busy happens.– goldilocks ♦Commented Nov 24, 2015 at 12:28
2 Answers
I'd use a PID algorithm..And change the PWM duty cycle of the fan. Blog post using a PID loop to control a fan in python. The post used a add-on board but his weather_pwm code is the same as the GPIO.pwm library that comes with raspbian.For example of his PID python code HERE from github. The links to the PID source and a video example are in the blog post as well. This is a basic explanation of what it is doing
Basically you will read your temperature of your CPU using the code:
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
Make a method that starts, stops, and changes the pwm speed of your fan. python pwm raspbian gpio library here
You set your setpoint with the PID library for your desired CPU temperature you want to maintain
p = pid.PID(1,1,0.02, Integrator_max=100, Integrator_min=0)
p.setPoint(74.0) #74.0F
Then create a loop reading the temperature and passing the value into the pid algorithm to recalculate the speed of the fan.
while:
x = (p.update(int(getCPUtemperature())))*-1
'''
Then you increment/decrement your dutycycle(fan speed)
by adding the result of the pid calculation to your current
dutycycle
'''
cycle = 100 + int(x) #100 is your max duty cycle
if (cycle<0): # set you min speed here
cycle=0
if (cycle>100): # set your max speed here
cycle=100
print 'Setpoint: 74.0 \nTemp: '+str(getCPUtemperature())+' \nFan Speed: ',str(cycle)+'%'
fan.changeDutyCycle(cycle)
# add a delay so your algorithm doesn't run too fast
sleep(.5)
This is great exercise for python programming and controlling things.. Hope this helps. :-)
I've written a library to do this, see here for wiring diagram.
To install it open a terminal op your pi and run the following commands:
git clone https://github.com/IgniparousTempest/rpi-fan-controller
cd rpi-fan-controller
chmod u+x ./install.sh
./install.sh
cd ..
rm -rf rpi-fan-controller
You can change the temperature threshold by running the following command:
sudo nano /usr/share/rpifan/config.cfg