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.
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