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

The Fan runs continuously, but I don't want continuous operation. I want the fan to run only when the temperature of the Raspberry Pi exceeds 60degC. How can I do it?

How can I do it?

  • Found this nice tuto
    – Camille
    Commented Jan 27, 2021 at 16:13

4 Answers 4


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.
    Commented Mar 24, 2019 at 10:51
  • Thank, I found a couple basic tutorials as well. learn.adafruit.com/transistors-101?view=all learn.adafruit.com/…
    – T. M.
    Commented Mar 24, 2019 at 17:14
  • You don't even need to write the switching SW, there's a GPIO overlay for RPi that does this for you. Commented Nov 27, 2019 at 13:17

It is simple to control a small fan with the Pi GPIO.

I use a simple transistor circuit (with only 3 inexpensive components).

I use the Pi 5V pin to power my fan, but this could be used with an external 12V supply for 12V fans.

This is suitable for fans which draw up to 500mA. Substitution of a power transistor could be used to power larger fans.


simulate this circuit – Schematic created using CircuitLab

The software is even simpler - only a single line needs to be added to config.txt


to switch the fan on at 60℃ - 60000 millicelcius (the default is 55℃ but you can select any temperature). The fan turns off when the temperature falls by 10℃.

This uses the gpio-fan overlay (which has been available in the kernel since late 2018) and should be included in recent versions of Raspbian. (Read /boot/overlays/README for description of this and other Device Tree overlays.)

  • cmdline.txt - or /boot/config.txt ?
    – Seamus
    Commented Oct 14, 2020 at 6:37
  • The diode serves no purpose here. Any BLDC motor already has freewheeling diodes inside (and much more). Commented Oct 14, 2020 at 13:42
  • @DmitryGrigoryev: too bad they don't also include a small transistor :)
    – Seamus
    Commented Nov 10, 2020 at 1:05
  • Would components be the same for 12V fan? (1N4148, BC338, 470 Ohm)
    – Y2i
    Commented Jul 10, 2023 at 2:22
  • 1
    @Y2i "this could be used with an external 12V supply for 12V fans". The component values are not critical and you could use any of the common available transistors or diodes. Any resistor from 100Ω to 1kΩ should be OK.
    – Milliways
    Commented Jul 10, 2023 at 2:49

An alternative to making a DIY circuit is to get a fan with speed control input. These typically have 4 wires (color code may vary):

BLACK        - Ground
GREEN/YELLOW - Speed output signal
BLUE         - PWM input signal

enter image description here

PWM signal can be used to set the speed using a PWM (typically with 20-80% duty cycle). Some fans will allow you to use it as an ON/OFF signal without any modulation, but some will switch off if the duty cycle is outside of their valid range.

The pin can be driven by your script/application or the gpio-fan overlay. You'll have to check whether a 3.3V signal directly from GPIO is acceptable (unlikely on a 12V fan, possible with 5V).

  • Dmitry, With a 4-pin fan like you describe, I assume the Ground and +VDC pins go to Ground and 5V on the Rasberry pi as you'd do with a normal 2-pin fan. What GPIO pins should the other two wires on the fan be connected to (the "Speed output signal" and "PWM input signal")? Will the single white connector in your fan picture need to be split into 2 or more connectors? Eric Commented Apr 28, 2021 at 1:47
  • @EricClaeys PWM input should be connected to a pin capable of PWM generation. Speed output can be left disconnected, unless you want to detect that your fan is damaged and needs to be replaced. And yes, you'll need to make a custom connector or terminate the fan wiring with jumper cables. Commented May 5, 2021 at 7:57

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 <[email protected]>

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
  • 1
    If you want to do some custom actions in fanOn()/fanOff() (e.g. send a notification), this is good. If you only need to pull GPIO high/low, use gpio-fan instead: does the same thing, but at a much lower level: no worrying about installed OS, script management, etc. Commented Nov 27, 2019 at 13:19

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.