serav
Posts: 5
Joined: Wed Oct 04, 2017 10:41 am

Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Wed Oct 04, 2017 11:48 am

Hi everyone,
I've been googling info about how to install a 5V fan on a RPI but when it comes to electronic and circuitry my mind just disconnects.

Things I've read but I don't know if they're true or not:
- It's a bad idea to plug the fan connectors directly to the RPI GPIO pins because the fan needs a burst of current when it starts running and it's also dangerous when it stops because it may send a harmful current to the ground pin.
- It's a good idea to use transistors and resistors if you want your RPI to be safe.
- GPIO 18 handles PWM so it's possible to connect a 3 PIN fan and configure the fan speed depending of the CPU's % utilisation
- I've read that its important to use 2 transistors, the second one between the fan connector and the GPIO Ground pin

The thing is, I don't know anything about circuitry and electronic. I could be able to solder with tin, but nothing else. That's why I would like to have your help.

I have 2 options, a 2 pin 5V fan or a 3 pin 5V fan and I would like to plug them in the 3.3V GPIO pin due to noise issues (If I can set the 3 pin fan speed at a low speed I could plug it in the 5V pin I guess). Now, here is where I get lost:
This is the RIP3B GPIO pinout
The fan will have 5V and a maximum of 0.2A
The transistor I've read in most forums is this one
FEATURES
*Collector current up to 700mA
*Collector-Emitter voltage up to 20 V
I couldn't find info about resistors used in those forums.

If turning on and off the fan every time the system gets hot can harm the RPI after this installation, I will think about having the fan always turned on, but if the transistors and resistors can handle these current issues, I will control the fans behavior with some scripts.

My questions are, How should I connect the RPI GPIO pins + the fan connectors (in both cases if possible) + the transistors + the resistors?
Is the NPN S8050 the correct transistor for this?
If so, Which resistor should I use?
Is it safe to connect the 3rd fan pin directly to the GPIO 18 pin?
If I have to solder some fan cables and cables to the transistors and resistors pins it won't be a problem.

This is not my native language so, excuse me if I wrote some grammatical errors :D

MarkR
Posts: 128
Joined: Fri Jan 25, 2013 1:55 pm

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Wed Oct 04, 2017 4:07 pm

- It's a bad idea to plug the fan connectors directly to the RPI GPIO pins because the fan needs a burst of current when it starts running and it's also dangerous when it stops because it may send a harmful current to the ground pin.
Yes, it is a very bad idea to plug a fan (or other, high-current inductive load) into a gpio pin. Doing so will probably break your Pi.
- It's a good idea to use transistors and resistors if you want your RPI to be safe.
Yes, but you still need to use them correctly :)

Personally I think a MOSFET (which is a type of transistor) would be good to turn a fan on/off. Your fan probably wants at least 5v, and the Pi GPIO pins output 3.3v, which would be enough to turn a mosfet on (if it's the correct type, of course)

However, as with anything, you need to ensure that the circuit is correct and that nothing exceeds the limits of what is allowed. If you take the power through the Pi's 5v rail, you must not take too much current. Remember that fans use a lot more current when starting, than when they are running steadily.

I'm not sure exactly why you want to do this (maybe a steady running fan would be easier?), but it's probably perfectly do-able thing, especially if it's a small fan.

asandford
Posts: 1996
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Wed Oct 04, 2017 11:47 pm

Are you finding that the Pi is getting so hot that thermal limiting is cutting in?

If not - you don't need a fan. If you are - heatsinks might be easier.

serav
Posts: 5
Joined: Wed Oct 04, 2017 10:41 am

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Thu Oct 05, 2017 10:10 am

I live in a place where, in some days (half of spring + summer + a few days in autum), thermometers reach between 38ºC and 45ºC and that means that temperature will be the lowest system temperature. I know the RPI can handle a higher constant temperature, but I would feel more at ease knowing that a little fan is lowering the temperature a little bit.

asandford
Posts: 1996
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Thu Oct 05, 2017 10:12 pm

serav wrote:
Thu Oct 05, 2017 10:10 am
I live in a place where, in some days (half of spring + summer + a few days in autum), thermometers reach between 38ºC and 45ºC and that means that temperature will be the lowest system temperature. I know the RPI can handle a higher constant temperature, but I would feel more at ease knowing that a little fan is lowering the temperature a little bit.
Have look here at various solutions

serav
Posts: 5
Joined: Wed Oct 04, 2017 10:41 am

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Oct 09, 2017 10:12 am

This is all I could do with my null electronics knowledge.

It's incompleted, and I'm sure everything is wrong xD.

I've read somewhere that transistor's base connector needs 0.7V, and most basic transistors have a ß=100 meaning that base current will be 100 times lower than collector current. Collector current is 0.05A if I'm not wrong, so base current will be 0.0005A. With these data I calculated the resistor needed between GPIO and transistor's base connector. Ib = (3.3V - 0.7V) / Rb = 5200­ ohms.

I don't know if I need a resistor between the fan and the transistor, if I need a resistor between the transistor and GPIO ground (I don't know if the emitter outputs a voltage higher than the collector voltage), and I don't know where to connect the yellow connector from the fan (Tachometric Signal).

I would be very grateful if you could lend me a hand to make a safe circuit for my PI, I can't do it by myself.

pcmanbob
Posts: 4570
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Oct 09, 2017 11:30 am

Here you go simple 2n2222 transistor fan driver.
included diode to protect transistor from back emf.
base current will be about 5ma, which is well within the GPIO limits.
do not connect the fan yellow wire it will be outputting 5v signal which will damage you GPIO, if you really need to measure the full fan speed then you will need to interface it with a potential divider circuit. But as soon as you use pwm to control the fan speed the output on the yellow wire will be useless as you will also be applying pwm to that output.

Image
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

serav
Posts: 5
Joined: Wed Oct 04, 2017 10:41 am

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Oct 09, 2017 7:48 pm

I didn't manage to find a decent 5V fan with a 4 pins connector, so I will forget about setting different speeds using PWM. I read about using a diode to protect the circuit but i forgot to mention it in the previous post.

Could you explain to me the reason of using that specific resistor and diode please? Knowledge is always welcome, I would be happy if I could understand a bit about this.
I guess the diode only has to cover the maximum possible voltage and amperage, which the 4001 does, but the calculations for the resistor kills me xD. Which equation gives the result of 470ohms?

If I decide to connect the fan to a 3.3V GPIO (hopefully it doesn't harm the fan too much) Could I use the same circuit you posted (pcmanbob) or should I use another resistor due to the difference of voltage in the collector?

Thanks so much for all the help. I have bought a bigger heatsink (so bad I couldn't find copper heatskins) that will come in a few weeks, so I will install both the bigger heatsink and the fan.

User avatar
Imperf3kt
Posts: 1496
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Oct 09, 2017 8:45 pm

A fan might run on 3.3v but it probably won't start without help. Use the 5v line, it'll draw less current @5v anyway..

Personally, I use the above diagram without the diode and resistor. Some scripting turns the fan on and off at specific temperatures and a desired speed (50% once I hit 70℃, 100% if it reaches 80℃)

You can use pin 18 or pin 12 and a 2pin fan and an NPN 2N2222 is all you require. The diodes and resistors are there for extra protection if you want them.

It may be worth mentioning however, that I don't use a 99cent Chinese fan, but a salvaged 5v maglev laptop fan which blows to the side.
Stop plugging your fan directly into the GPIO 5v
https://www.electronics-tutorials.ws/power/transient-suppression.html

pcmanbob
Posts: 4570
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Oct 09, 2017 10:19 pm

The resistor on the base leg is there to limit the current drawn from the gpio. I decided on the value by experimentation started at 1K but you did not get full 5V across the fan so changed it to 470 ohms to get full 5V but still limit base current. Diode was selected because it's a readily available one that will do the job. Again to protect transistor and pi from back emf on fan switch off.
Leaving both these components out of the circuit could result in damage to your pi. For the few penny's they cost why risk it.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

asandford
Posts: 1996
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Tue Oct 10, 2017 12:27 am

serav wrote:
Mon Oct 09, 2017 7:48 pm
I read about using a diode to protect the circuit but i forgot to mention it in the previous post.
Once (some) motors are no longer powered, but still turning, they become generators - the diode shorts it in a safe way.

serav
Posts: 5
Joined: Wed Oct 04, 2017 10:41 am

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Tue Oct 10, 2017 8:05 am

Imperf3kt wrote:
Mon Oct 09, 2017 8:45 pm
Some scripting turns the fan on and off at specific temperatures and a desired speed (50% once I hit 70℃, 100% if it reaches 80℃)
How do you control the speed of a 2 pins fan? I thought only 3-4 pins fans could be controlled.

pcmanbob
Posts: 4570
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Tue Oct 10, 2017 9:24 am

No most DC motors of the type used in a pc fan can be speed controlled by using pwm applied to the supply voltage.

The problem becomes reliable starting of the fan at lower speeds, you get round this problem by starting the fan at 100% duty cycle ( full speed ) then after 2 seconds you reduce to the level you actually want say 50% duty cycle ( half speed ).

you will probably find that 50% duty cycle is about as low as you want to go as the fan will not produce much air flow at speeds below this.


this is a perfect example were a little experimentation will reveal a lot .
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
Imperf3kt
Posts: 1496
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Tue Oct 10, 2017 8:49 pm

The above reply is mostly correct except it isn't uniform across all fans. Some (like mine) will start from as low as 1.5 volts.
Stop plugging your fan directly into the GPIO 5v
https://www.electronics-tutorials.ws/power/transient-suppression.html

pcmanbob
Posts: 4570
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Wed Oct 11, 2017 8:28 am

If you use good quality fans form the likes of cooler master you will find that using PWM they will actually start at just 12% duty cycle, but I found that some of the cheaper makes don't which is why I suggested running at full speed at start just to make sure the fan starts. ( many pc fan controllers do just that )

Like all projects you should build it and test it using the components you have and then adjust the hardware/software to work in the best way with the components you have.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

akisg
Posts: 3
Joined: Sat Dec 16, 2017 4:57 pm

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Sun Dec 17, 2017 8:56 pm

Hi,
I am interested into placing a fan into my pi 3 case and i came up across this thread when searching. I read this thread and i am a bit confused. Will i damage my pi board if i place a 5v fan directly into pin 4 (red) and pin 6 (black)? Do i have to use the transistor method connection or is it just used for a different configuration? I just found a fan from an old laptop and i want to use it with my pi, but reading this thread made me think twice before messing with my new pi. Any help to clear things out would be appreciated :D

User avatar
Burngate
Posts: 5529
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK
Contact: Website

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Dec 18, 2017 10:30 am

Assuming the fan is a 5v one and doesn't take too much current:

If you don't want to be able to control it, just put it across pins 4 & 6. When the Pi is powered, the fan will suck / blow / whatever.
If you want to control the fan, use the transistor circuit.

How to know if it's a 5v one? It may say on its label. If not, it may be, or it may be a 12v one, in which case it may just go slowly.
How much current is too much? Depends on the PSU feeding the Pi, what else is plugged in, ... I'd be willing to try - if it's taking too much, you will get the lightning bolt top right of the screen.

akisg
Posts: 3
Joined: Sat Dec 16, 2017 4:57 pm

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Dec 18, 2017 1:01 pm

Thanks, that's what i wanted to know. I will start with a normal operative fan and then maybe make a controlable one. I got a scavenged 5v fan from an old laptop that i wanna make use of.

lcamilo
Posts: 5
Joined: Mon Dec 18, 2017 6:59 pm

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Dec 18, 2017 7:07 pm

pcmanbob wrote:
Mon Oct 09, 2017 11:30 am
Here you go simple 2n2222 transistor fan driver.
included diode to protect transistor from back emf.
base current will be about 5ma, which is well within the GPIO limits.
do not connect the fan yellow wire it will be outputting 5v signal which will damage you GPIO, if you really need to measure the full fan speed then you will need to interface it with a potential divider circuit. But as soon as you use pwm to control the fan speed the output on the yellow wire will be useless as you will also be applying pwm to that output.

Image
Hi. I have a very very basic knowledge about electronics, but with few I know, I'm wondering this can work to have to fan speeds, full and not full (I'd like half) speed. I think about write some code that monitor cpu temp and when above x turn on half speed. if above y turn off half speed and turn on full speed. when come back below y turn off full and turn on half speed, when come back below x turn off fan.
Please take a look and say me if I'm right or wrong?

Image

Thank you in advance!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 11104
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Dec 18, 2017 7:13 pm

No! This is not the right way to do it, it might work, but it depends heavily on the characteristics of the transistor, its better to simply place a low ohmic resistor over the collector/emitter of the transistor, so some current always flows through the fan.
Even better is to drive the transistor with a PWM signal, so you can tune the amount of current running through the fan.

User avatar
Imperf3kt
Posts: 1496
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Mon Dec 18, 2017 9:42 pm

I suggest reading this, I found it very helpful:
viewtopic.php?t=186439
Stop plugging your fan directly into the GPIO 5v
https://www.electronics-tutorials.ws/power/transient-suppression.html

pcmanbob
Posts: 4570
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Tue Dec 19, 2017 12:03 pm

Using my original diagram to connect your fan you can control the fan speed using PWM.

try this simple example code it will drive you fan at various speeds as a demonstration, it uses gpio 25 but if your fan is connected to another gpio just change the gpio number in the code.

Code: Select all

#written for python2 

import RPi.GPIO as GPIO 
from time import sleep  
      
GPIO.setmode(GPIO.BCM)  
      
GPIO.setup(25, GPIO.OUT)

 

fan = GPIO.PWM(25, 100)  
print "start"  
fan.start(0)              
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)
sleep(0.5)
speed = [15,25,50,75,100]
for sp in speed:
    print "Run", sp,"%"
    fan.ChangeDutyCycle(sp)      
    sleep(10)
fan.ChangeDutyCycle(0) 
print "Stop"
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)
sleep(0.5)  
for sp in range(15,101,5):
    print "Run", sp,"%"
    fan.ChangeDutyCycle(sp)      
    sleep(2)
sleep(5)
print "Stop"
fan.stop()     
GPIO.cleanup()
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

lcamilo
Posts: 5
Joined: Mon Dec 18, 2017 6:59 pm

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Sat Dec 23, 2017 10:39 pm

pcmanbob wrote:
Tue Dec 19, 2017 12:03 pm
Using my original diagram to connect your fan you can control the fan speed using PWM.

try this simple example code it will drive you fan at various speeds as a demonstration, it uses gpio 25 but if your fan is connected to another gpio just change the gpio number in the code.

Code: Select all

#written for python2 

import RPi.GPIO as GPIO 
from time import sleep  
      
GPIO.setmode(GPIO.BCM)  
      
GPIO.setup(25, GPIO.OUT)

 

fan = GPIO.PWM(25, 100)  
print "start"  
fan.start(0)              
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)
sleep(0.5)
speed = [15,25,50,75,100]
for sp in speed:
    print "Run", sp,"%"
    fan.ChangeDutyCycle(sp)      
    sleep(10)
fan.ChangeDutyCycle(0) 
print "Stop"
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)
sleep(0.5)  
for sp in range(15,101,5):
    print "Run", sp,"%"
    fan.ChangeDutyCycle(sp)      
    sleep(2)
sleep(5)
print "Stop"
fan.stop()     
GPIO.cleanup()
Hi. Thank you. Worked properly, but only if I remove the resistor. With the resistor, with dc at 100%, the fun runs very slow. Without the resistor, runs at full speed.
Can I leave without or I must find another with less ohms?
Thanks

pcmanbob
Posts: 4570
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Sat Dec 23, 2017 11:05 pm

It should work with a 470 ohm resistor. You do need a resistor you could try 220 ohm. But don't go to low or you could end up drawing to much current from the gpio pin.

What's the rating of the fan you are using ?

EDIT........
Just checked and 2N2222 and 470 ohm resistor should power 5V 0.3A fan at full speed. So check you have every thing connected correctly
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

lcamilo
Posts: 5
Joined: Mon Dec 18, 2017 6:59 pm

Re: Adding a 5V Fan to a Raspberry Pi 3 Model B and having control of it

Sat Dec 23, 2017 11:56 pm

pcmanbob wrote:
Sat Dec 23, 2017 11:05 pm
It should work with a 470 ohm resistor. You do need a resistor you could try 220 ohm. But don't go to low or you could end up drawing to much current from the gpio pin.

What's the rating of the fan you are using ?

EDIT........
Just checked and 2N2222 and 470 ohm resistor should power 5V 0.3A fan at full speed. So check you have every thing connected correctly
I'm using 2N2222a, I can't find 2N2222. There's a problem?
I have tested with 220ohms, runs a little better, but not so good as can be.
Last edited by lcamilo on Sun Dec 24, 2017 12:09 am, edited 1 time in total.

Return to “Other projects”