connection of fan 5v to arduino

hi. i want to control this little fan (5V,0.2A) with arduino.
my project is this :
when temperature go >> 30 c , fan turn on.

but i donot know which pin from arduino should connected to the power of fan?
i connect a digital pin of arduino to power fan but it didnot work when temperature go >> 30.

my code:

#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

int fan=52;

void setup() {
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  pinMode(52, OUTPUT);

  dht.begin();
}

void loop() {
  delay(2000);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
   if(t > 30) 
   {
digitalWrite(fan,HIGH);
    }
    
}

i donot know which pin from arduino should connected to the power of fan?

None of them if the current consumption of the fan is 200 mA

    digitalWrite(fan, HIGH);

If it turned on do you ever intend to turn the fan off ?

can u tell me how i could control fan with arduino?

+connections?
+code?

The first thing to realise is that the Arduino can only supply limited current from its output pins so you need either a relay or a MOSFET controlled by an output pin to turn the fan on and off

Forget about the fan for now and write a sketch to make pin 13 go HIGH when the temperature is 30 or above and LOW when below 30. Print the temperature to the Serial monitor when you read it so that you can keep track of what is going on

are these connection and code true now?

connection:

code:

int Fan = 7;

int BAUD = 9600;
void setup() {
  // put your setup code here, to run once:
pinMode(Fan, OUTPUT);

Serial.begin(9600);
delay(1000);
Serial.println("START");
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(Fan , HIGH);
delay(5000);
digitalWrite(Fan, LOW);
delay(5000);
}

another question is how i could control the fan speed without using potentiometer and just with arduino pins?

You MUST have a resistor between output pin and transistor base, 330Ω should work, but not less than 220, and you should have a diode (1N4002 ~ 1N4004) in parallel with the fan, cathode (end with band) toward +5V to protect the transistor from back EMF. If you want to control speed, use an output pin with PWM (3,5,6,9,10,11 for UNO, Nano) and analogWrite a value between 50 and 255 to the pin, analogWrite 0 to stop.

analogWrite(outPin,127); // half speed
analogWrite(outPin,0); // stop

NPNMot-2.png

NPNMot-2.png

hadimargo:
another question is how i could control the fan speed without using potentiometer and just with arduino pins?

Once you have got the fan connected and powered properly you can use one of the PWM enabled pins and the analogWrite() function to control its speed

That's most likely a brushless fan, which may but more likely may not react well to PWM. These fans are designed to go on or off, not to be speed controlled.

If you want that, you usually need a 3 or 4 wire fan.

tnq all so much. :wink:

JCA34F:
and you should have a diode (1N4002 ~ 1N4004) in parallel with the fan, cathode (end with band) toward +5V to protect the transistor from back EMF.

Indeed, if it is a brushless fan (and basically all of these computer fans are), this is not necessary.

Brushless motors are vastly more reliable that brushed motors - they do not wear out. The bearings will require lubricating after some years.