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 ?
- 62,573
- 32
- 113
- 225
- 3
- 1
- 1
- 2
2 Answers
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:
- the GPIO pin to use, and
- the temperature at which the fan will turn on
Assuming your choices are GPIO 23 and 50℃, your one-line overlay is this:
dtoverlay=gpio-fan,gpiopin=23,temp=50000
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:
- You may use either the 5V supply (shown), or the 3.3V supply.
- The diode D1 (1N4148) is optional, but preferable, to limit "back EMF" when the fan is switched off.
- 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.
- 23,558
- 5
- 42
- 83
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.
- 62,573
- 32
- 113
- 225