am a programmer with no hardware knowledge background and defently no physics background.
I have a RPi4B and bought a fan bundled with its heatsink.
I realized the fan is basic and has 2 female pins to the 5V and ground.
I want to control the fan depending on CPU temperature. am well aware it's impossible with a software solution only because the 5V is always on. the 5V pin is not a GPIO pin, and only GPIO pins can be controlled from software.
so additional hardware is needed!
I found many articles and posts here how to do this with the help of a 2N222 transistor and a resistor. (which I just ordered from aliexpress lol. alongside the soldering iron and wire)
I noticed the fan is very quiet on the 3V pin so I got the idea to control the watt output too while keeping always on the 5V pin!(just like the gears in your car)
can this be acheived from software only ?
or can I use two different resistors to the transistor (each resistor is connected to a different GPIO) and control which GPIO is on ?

2 Answers 2


Your Question is rather confused, but the following may help (if I understand what you are trying to ask).

You can not power a fan from GPIO pins. It is possible to power from 5V or 3.3V pins, but this will run all the time.
NOTE powering a 5V fan from 3.3V is not recommended.

Fan control is about the simplest workable solution. I use this on my Pi4 and the fan rarely operates, and only activates if it is stressed.

It is possible to control speed (over a limited range) using PWM. The hardware remains the same, you need to program the PWM duty cycle to control speed.

NOTE your question about different resistors is a bad idea. Operating a transistor in linear mode dissipates excessive heat; you would need to use trial and error to find a resistor which works (as Hfe of transistors varies) and motors are unreliable at low voltage.

  • 1-first part is reaffirmations of what i said. 2- in 2nd paragraph i have to disagree about "is about simplest workable solution". 3- you answered my question with PWM ! I guess that's what am looking for. Now am wondering how that is achieved? Since Voltage is constant (5v) PWM works by changing the amps on the GPIO pin ? How does the speed change happen ? Thank you Aug 10, 2021 at 5:50
  • @AmineTbaik I still maintain that this is the simplest solution; it uses minimal hardware (3 components) and software (1 line). If you find something simpler I would love to hear it. There are hundreds of posts about PWM using various libraries on this site. I suggest you try the suggested solution and only explore PWM if you find it does not meet your needs. It is simple enough if you have programming experience, but I have never bothered. In fact if you really want variable speed a 3 terminal fan is probably better.
    – Milliways
    Aug 10, 2021 at 9:08
  • just since you asked: the easiest solution in the planet is to just plug it and let the thing spin forever. But am not looking for easy. Am looking for: power efficient (meaning it only spins when needed) and looking for silence (slow speed and goes faster the higher the temperature gets). But thanks, you already answered the question. Dont know why you removed that part from your answer then challenged me here on comments. Aug 10, 2021 at 13:09
  • @AmineTbaik You asked "I want to control the fan depending on CPU temperature". Doing nothing is obviously simpler. "Don't know why you removed that part from your answer" - I removed nothing! If you need clarification ask.
    – Milliways
    Aug 11, 2021 at 2:27

Perhaps the easiest way to do this is to add the one-line dtoverlay=gpio-fan facility to your /boot/config.txt file. You will still need to add some hardware, but the transistor and resistor may be all the hardware required.

You can learn a bit about the Raspberry Pi's device tree, and the pre-packaged overlays in this document. Scroll down or search this file until you find the dtoverlay=gpio-fan section.

Define the dtoverlay parameters:

You'll need to make two decisions to set this up:

  1. the GPIO pin to use, and
  2. the temperature at which the fan will turn on

Assuming your choices are GPIO 23 and 50℃, your one-line overlay is this:


To "install" this overlay, open the file /boot/config.txt in your editor, and insert this dtoverlay line somewhere above the [pi4] section near the end of the file.

The components and interconnections required are shown in the following schematic:


simulate this circuit – Schematic created using CircuitLab

Some notes re this schematic:

  1. You may use either the 5V supply (shown), or the 3.3V supply.
  2. The diode D1 (1N4148) is optional, but preferable, to limit "back EMF" when the fan is switched off.
  3. See this page for a "translation" between GPIO numbers and physical pin numbers

reboot and test

The dtoverlay will take effect after the next reboot. When the CPU temperature reaches 50℃, the designated GPIO will go "HIGH", thereby turning the transistor Q1 "ON", and causing the fan to start. When the temperature falls to approx 45℃, the fan will be switched "OFF" again.

Let us know if you have further questions.

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.