Inaccurate readings from LM35 (temp) while turning on a LED or FAN.

Hi everyone,

Thanks for such a wonderful resource! I’ve been working through the common temperature project using the LM35 to sense the ambient temperature and using if/then statements to turn on a fan/led. The project is working yet while using the serial monitor and/or LCD panel to view the temperature readings, I notice that once the threshold has been met to output a HIGH state to the pins connected to a LED and FAN, the temperature reading jumps up quite a bit and stay there. I’m assuming this may be related to a change is voltage and/or current, yet I simply don’t have enough experience to de-bug. Working code posted below.

Thanks for any guidance.

-Nicholas

/*
  Temperature Sensor
  Reading temperature with LM35 sensor.
  
  Coded by: arduinoprojects101.com
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//declare variables
float tempC;
float tempF;


int tempPin = 1;

int fan = 5; // fan is connected to pin 5
int led = 13;
/*int reading;
float referenceVoltage;*/

void setup(){
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.print("Temp1=");
  lcd.setCursor(0, 1);
  lcd.print("Temp2=");
 // Set Analog reference to 1.1V this gives more accuracy since the sensor will output 0-1 V
 // This only available on ATmega168 or ATmega328)
 // For more information see: http://arduino.cc/en/Reference/AnalogReference
 /*analogReference(INTERNAL);
 referenceVoltage = 1.1; //Set to 5, 3.3, 2.56 or 1.1 depending on analogReference Setting*/
 pinMode (led, OUTPUT);
 pinMode (fan, OUTPUT);
}

void loop(){
  Serial.begin(9600);
  tempC = analogRead(tempPin);           //read the value from the sensor
  tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature
  tempF = ((tempC*9)/5) + 32;            //convert celcius to farenheit

  // print result to lcd display
  lcd.setCursor(6, 0);
  lcd.print(tempC,1);
  lcd.print("'C");

  lcd.setCursor(6, 1);
  lcd.print(tempF,1);
  lcd.print("'F");
  Serial.print(tempC, 1); //Print one decimal, it's not accurate enough for two
  Serial.println(" C");
  Serial.print(tempF, 1); //Print one decimal, it's not accurate enough for two
  Serial.println(" F");
  
  Serial.println(" ");
  // sleep...
  delay(1000);

    
    if (tempC > 24.0)
     {
      digitalWrite (led, HIGH);
      digitalWrite (fan, HIGH);
     }
     else
     {
      digitalWrite (led, LOW);
      digitalWrite (fan, LOW);
     }
  }
  Serial.begin(9600);

This belongs in setup(), not loop().

What is connected to the fan and led pins? Electric motors typically need a lot of current. You can't expect the Arduino to power a fan.

Thank you, Paul. I'll give it a shot.

I should have noted I'm using a NPN with external supply for the fan.

Very helpful! -Nicholas