Switching a cooling fan on and off through NPN transistor connected to Arduino

I'm working on a DIY gas sensor project and want to be able to ventilate a cavity with a fan in between taking readings. For the prototype, I got a small 5V cooling fan that draws about 220mA (according to the reading I took with my DMM), which I want to switch on and off through a 2N2222A transistor. The transistor's datasheet is here (https://www.onsemi.com/pub/Collateral/P2N2222A-D.PDF), but I confess that most of the information goes over my head!

I can't power the fan directly through the Arduino's digital output pins, because it doesn't supply enough power. But if I connect it to Arduino's VIN pin, I get a very satisfying whir, which leaves me with the switching problem. I want to avoid a bulky relay, hence the transistor as switch.

I've wired the fan as shown in the uploaded diagram. It's essentially a low-side switch, with the fan's black wire connected to the transistor's collector pin and the emitter pin tied to ground. My arduino code is listening for a button press on digital pin 3, and when the button press is detected, pin 6 is switched to HIGH in order to activate the transistor switch. The fan then runs for a predetermined time, and switches itself off.

Here is my code:

#define fanSwitchPin 6
#define btnPin 3
#define fanDuration 10000
boolean fanOn = false;

boolean getFanBtnState(){
  if(digitalRead(btnPin) == LOW){
    return true;
  } else {
    return false;
  }
}

void runFan(){
  digitalWrite(fanSwitchPin, HIGH);
  delay(fanDuration);
  digitalWrite(fanSwitchPin, LOW);
  fanOn = false;  
}

void setup() {
  pinMode(btnPin, INPUT_PULLUP);
  pinMode(fanSwitchPin, OUTPUT);
  digitalWrite(fanSwitchPin, LOW);
}

void loop() {
  fanOn = getFanBtnState(); 
  if(fanOn == true){
    runFan();
  }
}

Everything seems fine to me on paper, except that the fan doesn't switch on when I press the button! I've read in another post that the 2N2222A transistor is rated for more than 5V on the base, so on that basis I haven't put in a resistor. This might be a mistake, and I'm not sure if I've fried the transistor by now! I've tried to test the transistor's health with my DMM, but it doesn't have dedicated transistor testing inputs, so I'm not convinced I'm doing that right either. When testing it on the diode setting, I get an open circuit between base and emitter, but there's a resistance drop between base and collector - not sure if both should be open?

I'm pretty sure that the problem is not with my code, as I've double checked everything with outputs to the serial monitor, but I'm evidently doing something wrong. My guess is that the fault is with the wiring of the peripherals, because I'm a total newbie in with electronics.

I'd be grateful for any help!

1/ You haven't included a resistor in series with the transistor base - so you've probably burnt out the arduino output.

Suggest you use 270R and another output.

2/ The 2N2222 isn't really suitable for this . Use either a darlington (eg TIP121 ) or, better, a logic level MOSFET ( eg IRLZ44N )

Allan

The 2N2222A will switch 0.22A ok - but does the fan pull a lot more than this at startup?.

Thank you for the quick responses!

MarkT:
The 2N2222A will switch 0.22A ok - but does the fan pull a lot more than this at startup?.

Ha, didn't think of a power surge at start-up! My DMM reading responds in about a second or less, and mostly goes directly up to 0.22A, with an occasional surge to 0.23A, but immediately dropping back 0.22A. But I guess it's possible that it surges higher for a fraction of a second that's too short for the DMM to pick up? How do I handle a start-up surge, assuming there is one?

allanhurst:
1/ You haven't included a resistor in series with the transistor base - so you've probably burnt out the arduino output.

Thankfully, the Arduino output seems okay - voltage goes from 0 to 5 as expected when I press the button.

I'll pick up a TIP121 or IRLZ44N when I pass by the electronics shop - probably tomorrow. In the meantime, I might as well try again with the 2N2222A, this time with a resister connected to base. I've got a 360, which I assume is close enough to your recommended 270?

Just to follow up about testing the transistors. There were three in the pack. The one I used is probably fried - as I said, it shows an open circuit (infinite resistance) between base and emitter, but the resistance drops to about 0.87k between base and collector. The two unused ones confuse me. One shows open circuits on both sides, while the other gives a resistance of 0.87k on both sides. Neither have been used, so don't know what this means. Any thoughts?

The way you connect yours probes matter, read the link above.

Ciao, Ale.

Many thanks for the link! Very helpful!