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.


  Temperature Sensor
  Reading temperature with LM35 sensor.
  Coded by:

// 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.setCursor(0, 1);
 // 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:
 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(){
  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.setCursor(6, 1);
  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...

    if (tempC > 24.0)
      digitalWrite (led, HIGH);
      digitalWrite (fan, HIGH);
      digitalWrite (led, LOW);
      digitalWrite (fan, LOW);

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