I have Raspberry Pi 3b+ with the case, which has a small 5v fan, which is connected to GPIO. Look to picture below:

enter image description here

Fan works all the time. But I don't want it. I want it to work only in case, when the temperature of the Raspberry Pi more than 60C'. How can I do it?

How can I do it?


The fan is not connected to the GPIO. The GPIO are 3V3 and only supply a few tens of milliamps of current. The fan is connected to the 5V and ground rail.

You will need to add circuitry to switch the fan on and off. The simplest is probably to add a transistor. The transistor would sit between the 5V supply and the fan. A GPIO could then be used to switch the transistor on and off.

You would need to write the control software to switch the GPIO/fan on when the Pi is too hot.

Hobby electronics shops should be a good source of typical transistors.

I'd probably aim for something which could switch at leas 500mA of current.

E.g. http://www.hobbytronics.co.uk/electronic-components/transistors/tip31c is overkill, but the others on that site only switch 100mA.

Once you find a suitable transistor its specs will show how much current is needed to the base to fully switch it on. Then calculate the needed resistance to allow that current to flow from a 3V3 GPIO.

  • Ok that seems really simple but after searching for a bit I can’t find any tutorial to show how to hook up a transistor to a raspberry pi 5v and control a 5v loaf such as a fan by gpio. What transistor to use what resistor if needed, etc. I think it’s obvious enough to someone that knows how that it doesn’t seem to be covered much. – T. M. Mar 24 at 10:51
  • Thank, I found a couple basic tutorials as well. learn.adafruit.com/transistors-101?view=all learn.adafruit.com/… – T. M. Mar 24 at 17:14

I'M USING THIS SCRIPT SAVED ON /home/runfan.py And the circuit is very simple using only a BD159, connect from GPIO to center of BD159 (connector number 2), and the negative to number 1 connector of BD159, and the negative fan wire to the number 3 connector of BD139.

#!/usr/bin/env python3
# Author: Edoardo Paolo Scalafiotti <edoardo849@gmail.com>

import os
from time import sleep
import signal
import sys
import RPi.GPIO as GPIO

pin = 2  # The pin ID, edit here to change it
maxTMP = 70 # The maximum temperature in Celsius after which we trigger the fan

def setup():
    GPIO.setup(pin, GPIO.OUT)

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():
def fanOFF():
def getTEMP():
    CPU_temp = float(getCPUtemperature())
    if CPU_temp>maxTMP:
def setPin(mode): # A little redundant function but useful if you want to add logging
    GPIO.output(pin, mode)

    while True:
    sleep(5) # Read the temperature every 5 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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