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!
