Controlling a Pc fan with Arduino

Hello Everybody

As first step of many i have to get working for my thesis project, I have to control a little fan.
What I am actually trying to do is to turn on and off a little pc fan when a button is pressed...It seemed to me very easy, but when i started trying i was "pleased" to know I was wrong!!

Actually i have been looking on this little tutorial:

http://www.liquidware.org/view.php?id=75

Now this one in particular turns on and off the fan through serial port...

Do you have any idea how this could work with simple button?
this is my code

int fan = 7;
int btn = 13;
int val;

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(btn, INPUT);
  Serial.begin(9600);
  }
  
  void loop() {
val = digitalRead(btn);
if ( val == HIGH ) {
  digitalWrite(fan,HIGH);
  Serial.print("on");
  delay(5000);
    } else {
      digitalWrite(fan,LOW);
      Serial.print("off");
      delay(5000);
      }
   }

It should turn HIGH the pin to which the fan is connected, giving the 5V that make the transistor turn on.

Thanks 4 helping

The code seems ok. Some boards have a resistor and LED on pin 13, why not swap the fan and button pins so you can see the LED light when the button is pressed and any internal circuit on pin 13 won't interfere with the button

Mem's advice to break the project into smaller components is excellent. First make sure the button code works, then integrate the fan.

I'm a little concerned that you are only polling the button every 5 seconds. It seems to me that it will be very easy to miss a button press if it happens to occur in between the samples.

Mikal

As Mikal said the 5 second delay is a bit extreme

You should greatly reduce it or remove the delay completely

Also Pin 13 on most boards is for an LED which can be used to tell you what your Arduino is doing, so use another pin for your button and put an LED on Pin 13

:slight_smile:

Thanks for your fast replies

@mem : sorry but i didn't really understood what you mean...but maybe your telling me to put a led where the fan should be?
@mikal & gnu_linux:

I did as you told, no delay, the button on pin9 and a led on pin 13...so now it seems like it ALMOST works...when i push the button the led turns on and the fan makes a slight movement...But I can't understand how to make it turn properly! it should start spinning instead of just wave a little...But it's a start...

Moreover in the link i posted before it controls the speed somehow through serial port, but i do not understand it ( the code i mean )...maybe the solution is there :-?

Any idea is very welcome

Are you using a fan with 2 wires or 3 wires?

Is it a 5VDC or 12VDC fan?

:slight_smile:

Sanspot, if you have the Arduino Diecimila or equivalents, there is a tiny LED connected to pin 13 on the circuit board. I was suggesting that you connect the switch to pin 7 and use pin 13 for the fan.

Sorry i must correct myself...it works!!

Don't Know why it nedds a little help tp start spinning..the fan i mean...Give a little push with the finger, while pressing the button and it starts spinning!...

Now i will try with a potentiometer...Do You think I will have to use PWM?

Thanks again for yuor preciuos help

@ mem:
ooohh now i understand. Did you suggest to put the fan in pin13 in order to se if the led turned on when pressing the button? so that if the led was on the the fan should had to work, right?

@ gnu_linux:
it has three wires one for +V one for GND and another that i don't know what it there for.
It works with 5Volts.

If it's a 12VDC fan is may spin very slowly or may not spin at all with 5V from the Arduino so you should feed it 12VDC from an external power supply

If it's a 5VDC fan it should spin with 5V from the Arduino but the Arduino cannot supply very much power to it so you should use an external power supply

:slight_smile:

A 3pin / 3 wire fan usually takes +12V -GND and SPEED_CTRL

So if it really is a 12VDC fan use a 12VDC power supply to power it ( the fan will start spinning on its own when you plug it in )

The SPEED_CTRL wire / pin is usually yellow and should be connected to one of your Arduino pins to control the speed

You probably need to use a common ground

:slight_smile:

Hi Everyone,
I am also trying to connect a small PC fan to the Arduino but it only seems to work when I set a blinking state (High, Low) making the fan spin for a 1/4 turn left and then right. If I try to set it to always go high to spin completely it doesn't work.

I hope you can help me! this must be really simple but I am missing something!

Fan specs:

120 mm thermaltake
started V 6V/7V Max
current:0.38 A
Pwr input: 4.56W
two wires (+ and -)

Code: (Blinking example from Arduino):

int ledPin = 13; // LED connected to digital pin 13
int fan = 11;
// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(fan, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
digitalWrite(fan, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
digitalWrite(fan, HIGH); // set the LED on
delay(1000); // wait for a second
}

Thanks!
Denise