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.
    – joan
    Commented Nov 24, 2015 at 4:54
  • 1
    @joan I don't think sarcasm works!
    – Milliways
    Commented 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.
    – Milliways
    Commented 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 2


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()

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.

  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 
  cycle = 100 + int(x) #100 is your max duty cycle
  if (cycle<0):  # set you min speed here
  if (cycle>100): # set your max speed here
  print 'Setpoint: 74.0 \nTemp: '+str(getCPUtemperature())+' \nFan Speed: ',str(cycle)+'%'
  # add a delay so your algorithm doesn't run too fast 

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
cd ..
rm -rf rpi-fan-controller

You can change the temperature threshold by running the following command:

sudo nano /usr/share/rpifan/config.cfg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.