lm35 turn on usb fan

hi i using lm35 when it detect above 16 degree the fan will turn but it never turn on. icouldn't find any error for the code but when i use the signal pin from the fan connect to the 3.3v the fan turn

#define fan 9

float reading=A0;
int tempPin = 0;

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

void loop()
{

reading = analogRead(tempPin);
int celsius = reading* 0.48828125;

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

if (celsius>16)
{
digitalWrite (fan,HIGH);

}
else
{
digitalWrite (fan,LOW);
}

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

}

That code doesn't compile. You didn't declare "RELAY1".... I suspect you got confused with calling it "fan"

what u mean how you go about it

slayter89:
what u mean how you go about it

Well when you hit the compile or upload button (circled below), what messages did you get?

couldn't find any error for the code

But the compiler clearly can.

Compare to your code from December's posts, where you defined RELAY2 as a pin number. In today's you defined fan as a pin number, but then used RELAY1.

Fix that, get it to compile. then we can talk about why the fan isn't behaving as you had hoped.

no error even serial monitor shows the temp, jus the fan cannot turn

define fan 9

float reading=A0;
int tempPin = 0;

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

void loop()
{

reading = analogRead(tempPin);
int celsius = reading* 0.48828125;

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

if (celsius>16)
{
digitalWrite (fan,HIGH);

}
else
{
digitalWrite (fan,LOW);
}

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

}

define fan 9

No error?
Really?

nope no error

It looks like you have a cut and paste error then.
Maybe try posting the code with code tags, and some idea of what the serial monitor output looks like.

Your compiler must be more lenient than ours then?

Ok let's assume that you have a version that compiles, but that you're not sharing it with us. Put these serial prints in as shown below, and see if the logic is actually taking you to the right branch of the if. You could also replace the relay fan etc setup with an led and see if it lights.

You might also let us see the circuit diagram?

if (celsius>16)
{
     digitalWrite (fan,HIGH);
     Serial.println("in the >16 leg"); //<<<<<<<<<<<<<<<<<
   
}
else
{
     digitalWrite (fan,LOW);
     Serial.println("in else leg"); //<<<<<<<<<<<<<<<<<
}

This code compiles and runs and the logic is good. I commented out the fan stuff and just used Serial.print to see where the logic goes.

My actual LM35's temp is 25C here now so I ran the sketch twice, one looking for >16 which took me to the "if" and once looking for >30 which took me to the "else".

So the logic is good: must be your circuit which is wrong. Let's see it.

//define fan 9

float reading=A0;
int tempPin = 0;

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

void loop()
{
 
  reading = analogRead(tempPin);
  int celsius = reading* 0.48828125;
  
  
  Serial.print("TEMPRATURE = ");
  Serial.print(celsius);
  Serial.print("*C");
  Serial.println();
   
   
  
 if (celsius>30)
 {
      //digitalWrite (fan,HIGH);
      Serial.println("in the if");
    
 }
 else
 {
      //digitalWrite (fan,LOW);
      Serial.println("in the else");
 }

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

ps.... then I changed that code to put pin 13 led on or off, works as expected

//define fan 9

float reading=A0;
int tempPin = 0;

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

void loop()
{
 
  reading = analogRead(tempPin);
  int celsius = reading* 0.48828125;
  
  
  Serial.print("TEMPRATURE = ");
  Serial.print(celsius);
  Serial.print("*C");
  Serial.println();
   
   
  
 if (celsius>16)
 {
      digitalWrite (13,HIGH);
      Serial.println("in the if");
    
 }
 else
 {
      digitalWrite (13,LOW);
      Serial.println("in the else");
 }

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

Let's see your circuit (assuming as I say you have a version of code that compiles: try mine)

yeah my serial monitor is working sorry i corrected my programming. is jus the usb cannot turn on when i connect the positive pin to pin 9 and negative pin to ground, when i test it on the pc fan it works . i not sure how to solve the as digital i/o pin from 0 to 13 does on 5v. i not sure how to solve the usb fan which can work on a 3.3v pin but not on a 5v pin

#define fan 9

float reading=A0;
int tempPin = 0;

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

void loop()
{

reading = analogRead(tempPin);
int celsius = reading* 0.48828125;

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

if (celsius>16)
{
digitalWrite (fan,HIGH);

}
else
{
digitalWrite (fan,LOW);
}

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

}

Hi,

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your sketch, using code tags?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

PLEASE read the link, then post using code tags, and PLEASE a copy of your circuit and PLEASE a picture of your project.
How are you powering your fan and what sort/model/make is it.

Tom...... :slight_smile: