How do I (easily) power a 5 V 3-pin fan?
You are correct in your assumption that you can not power a fan from an Arduino directly.
Use a transistor, for example 2N2222 or BC548, which is switched by the Arduino, using a single pin, in order to provide sufficient current to the fan.
Such as this simple circuit (note that the 12V supply would be 5V in your case):
Source: Driving a PC fan - transistor with or without an optocoupler?1
However, as chrisl has pointed out, you fan has three pins and not two. The above circuit will only work for a two pin fan. In your case, for the reasons stated in Chrisl's answer, one pin goes to 5 V supply, one to GND
and then third goes directly to the Arduino - let's say pin 3. Then you can use the code below.
Assuming that the fan is connected to pin 3, then the code will look like this:
const int fan = 3;
void setup() {
pinMode(fan, OUTPUT);
}
void loop() {
int fanSpeed = 255;
analogWrite(fan, fanSpeed);
}
This will run the fan at full blast. However, you can thereafter modify your code to vary the value of fanSpeed
.
Furthermore, by reading an analog input pin (let's assume that you have a potentiometer hanging off A0
), using analogRead()
, you can use the read value to modify the value of fanSpeed
directly (by dividing the value returned from analogRead()
by four2 and then sending to analogWrite()
, like so:
int potPin = A0;
fanSpeed = analogRead(potPin)/4;
analogWrite(fan, fanSpeed);
or even more succinctly, do away with fanSpeed
completely and use:
int potPin = A0;
analogWrite(fan,analogRead(potPin)/4);
1 Note that the code in the link in incorrect as it attempts to write values greater than 255 using analogWrite()
. As stated here, Arduino reference - analogWrite(), analogWrite()
takes values of 0-255
, whereas analogRead()
returns values of 0-1023
2 See the loop()
function in Arduino reference - analogWrite() for another example.
EDIT:
As VE7JRO pointed out the fan you are using does not have a native PWM pin. The third pin is actually reporting back the RPMs of the fan. So you need to follow Greenonline's answer by using a MOSFET. For now I will leave my answer here for reference. If wanted I will delete it later.
A 3-pin fan is controlled like a servo. The current for actually driving the motor flows through the power pins (5V and Ground). The third pin takes a PWM (pulse width modulation) signal to determine the wanted fan speed. This is just a signal line, there is only very little current flow, so that the Arduinos pin will not be damaged. So the motor driver is already included.
From the programming side you can use analogWrite()
, which outputs a PWM signal at one of the PWM-capable pins. The speed of the fan is controlled via the duty cycle of the signal. 100% (value of 255 for UNO) means full speed, 0% (value of 0) stops the motor.
About the power supply: Since your fan uses 100mA at full speed (I assume), it is still possible to power it over the 5V pin of the Arduino. You can source about 500mA over it (including the current for the Arduino itself). When sourcing higher currents over the USB plug, the polyfuse will be fried. This is the value for the UNO r3. A Nano can source only 200mA, because the diode will be fried.
So be carefull how much current you draw from the 5V pin of the Arduino. Or, if you have greater current requirements you have to supply an extra voltage to drive the fan.