lm 35 and ac fan

hi i having some problem i am using lm35 to control household fan as my fyp. when i set the temperature above 20 degree it turn on the fan. when actual the fan doesn't turn on having different result each time i upload the program but if i use pc fan no problem is works perfectly well. i connect the fan using a relay

#define RELAY2 13

float temp;
int tempPin = A0;

void setup()
{
pinMode(RELAY2,OUTPUT);
Serial.begin(9600);
}

void loop()
{

temp = analogRead(tempPin);
temp = temp * 0.48828125;

Serial.print("TEMPRATURE = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();

delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);
delay(1000);

if ((temp)>25){
digitalWrite (RELAY2, HIGH);

}else{
digitalWrite (RELAY2, LOW);
}
}

two tips:

delay(11 * 1000); ?

// prevent on/off resonance by using a "dead zone"
if (temp > 25)
{
      digitalWrite (RELAY2, HIGH);
}
else if (temp < 24)
{
      digitalWrite (RELAY2, LOW);
}

how did you connect the relay? it should have its own power supply...

Check this tutorial - http://www.electroschematics.com/8975/arduino-control-relay/ -

finally:

  • CTRL-T does an autoformat of your code ==> more readable
  • use of [code]  [/code] looks also better

hi did something like that

Where is the diode across your relay? You need diode connected in reverse (non conducting) across the relay to remove spikes when it turns off.

It is better to drive the relay via a transitor rather than direct from the Arduino pin.

What type of relay are you using? Most only have two connections. Or is it a relay shield?

Weedpharma

That is the worst thing you can do. The relay should be switched with a NPN transitor (with proper resistors too), have a feedback diode and be on its own separate power supply (with a common ground -> Arduino ground to Ext power ground)

*Actually the worst thing you can do is power the relay from another digital pin.

hi what type of diode or relay to use

diode = 1N4001
relay= Relay SPDT Sealed - COM-00100 - SparkFun Electronics (or similar)

Hi, your picture says the relay (=relay module, I suspect) is connected to pin 7, but your sketch says pin 13?

Paul