Controlling a fan via PN2222

Hello,

First of all I'm kinda new to the microcontroller environnement. I'm more of a developper background so the electronic part is quite tricky for me :).

I have spent a lot of time reading and improving on the different components I use and why (transitor instead of relay in my case etc...) but I'm kinda stuck and I need your help as I'm sure it will be trivial for you :).

My goal : I have a fan with a 3-pin and I want to control it with an arduino. I managed to find which pin are the +/-. I connected them to my external 12V source (fan is a 12V) and it works perfectly.

Then I follow this tutorial (Breadboard Layout | Arduino Lesson 13. DC Motors | Adafruit Learning System), replaced the motor by my fan (that shouldn't be an issue right?) and added an external power source. The only output of the arduino is the control pin (3 in my case on the digital side).
This is what I'm trying to replicate.

The thing I'm afraid of is frying my arduino (I don't think that's possible cause it's no where near the fan nor the power source and to protect the motor I added a reverse diode).

For the code, I tried two things. The code of the motor on the tutorial :

int motorPin = 3;
 
void setup() 
{ 
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
} 
 
 
void loop() 
{ 
  if (Serial.available())
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255)
    {
      analogWrite(motorPin, speed);
    }
  }
}

And then in the COM interface I have type 0,100,255 etc... nothing happened

int motorPin = 3;
 
void setup() 
{ 
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
} 
 
 
void loop() 
{ 
  digitalWrite(motorPin, HIGH); // sets the digital pin 3 on
  delay(5000);            // waits for a second
  digitalWrite(motorPin, LOW);  // sets the digital pin 3 off
  delay(5000); 
}

I thought maybe it's because I'm doing analogWrite on a digital pin (I also wondered why in the tutorial that's what they were doing for the motor?). But again nothing happened. The only weird this is a buzzing sound as sound as I plugged the external source... I read somewhere that the transistor is keeping some of the current so maybe the fan doesn't have enough to start ? I don't know I'm kinda lost :). If you have any idea ! Thank a lot!

My setup
Classic : Imgur: The magic of the Internet
Zoomed : Imgur: The magic of the Internet

Specs :
Arduino : MEGA2560
External Power Source : HW-DY02
FAN : upHere 12BK3-3
Resistor : 330 Ohm
Transistor : PN2222A

EDIT: Change the code tag
EDIT: Finally it will be done with a MOSFET

We need to know how much current the fan draws. Either look up the fan data, or use your multimeter to measure it. If you don't have a multimeter, now is the time to buy one.

The problem with using a bipolar transistor like the PN2222 is the current gain, which is pretty low. You would be much better off using a logic level MOSFET, using the circuit below.

When posting code, please use code tags ("</>" button).

You should find the Serial Input Basics tutorial informative.

You are right I didn't have a multi-meter :). I knew I'll have to buy one I should have done it earlier, I'll know the current in a few days. It's pretty complicated to find some info on this fan, other than 12V.

For the MOSFET, I saw some tutorial mentioning it but because I didn't have one I decided to go with a transistor but I'm gonna buy a few of them I'll need them anyway.

Concerning your diagrams I just have two questions:

  • If I'm correct that's a capacitor in C1, why is it needed?
  • Same thing but for the R1 at 10k, I don't see it's purpose

I'm gonna check the serial tutorial you just linked me, it's easier for me. It's about coding :smiley:

Thank you again for your answers and your time

The capacitor reduces radio frequency interference, needed for a brushed DC motor. Not needed for a brushless fan. But if your fan is brushless, you may not be able to control the speed by PWM (if that is your intent).

R1 is required for a MOSFET, to prevent the gate from floating when the Arduino pin is floating (e.g. at startup, when all port pins are INPUT by default).

Controlling the speed of the fan was not my intent. I just wanted for it to run and stop :).

Just a quick note to say that it works perfectly with the MOSFET and the code. I bought the IRF540N (33A / 100V). The current of the fan is around 270mA (mesure taken while the fan is running alone directly connected to the power supply).

I have one more question :). If I where to plug another fan to this system or more. Can I add them in parallel with the first one (another straight line between the diode and the first fan) or do I have do duplicate all the system?
My Power supply is said to have a max current of 800mA and If I remember correctly when in parallel you sum up the current of each branch to get the total. That would mean I could only plug like 3 of them and nothing else, no light or what so ever ?

If you need more than 800 mA, choose a higher current power supply. Thrift and computer recycling shops usually have bins of cheap 12V adapters.

The IRF540 is a poor choice for Arduino, as it is not a logic level MOSFET and is not guaranteed to turn fully on with gate voltage = 5V.

Use a logic level MOSFET, like the IRL540 instead.