LM35 Wrong Temperature

Hello,
Im using an LM35 in my project to show the temperature, it was working correctly with Arduino UNO when i was prototyping the project, but then i connected everything to an Arduino NANO, and its not giving me the correct temperature.

Every time its different when i turn the Arduino on, sometimes very high number, sometimes very low, and sometimes its correct! and every time the value changes very fast about 5 or 6 degrees, like its 10 degrees, then 16 then 9 then 17 and so.

The code didn't change, neither the wiring or reference voltage(5v), it just went wrong. I also tried 3 different sensors.

my code is this:

void loop()
{
  val = analogRead(tempPin);
  float mv = ( val/1024.0)*5000;
  float cel = mv/10;
  float farh = (cel*9)/5 + 32;
  Serial.print("TEMPRATURE = ");
  Serial.print(cel);
  Serial.print("*C");
  Serial.println();
  delay(1000);
}

And you know the circuit there's no need for that,
Do you have any ideas about this? where's the problem?

My two cents:

It sounds like a grounding problem. Did you connect the Aref pin to the ground? Check the wires, Check the signal.

Do you have an oscilloscope?

“Did you connect the Aref pin to the ground?”

did it right now, it went to 500-_- disconnected that quickly.
Its the next pin to the 3.3v and A0 on Nano right? depending on pin outs on google.

“Do you have an Oscilloscope?”
no i dont have an Oscilloscope.

Hmmm, I replicated your result on a 3.3V BLE board. It looks like the single analogRead is not very stable even when you connect it to a voltage divider.

A little averaging solved it.

int tempPin = 1;
void setup()
{
  Serial.begin(9600);
  pinMode(tempPin,INPUT);
}
void loop()
{  float val=0;
   for (int i=1;i<1000;i++)
   { val += analogRead(tempPin);
     delay(1);
   }
   val /=1000; 
   float mv = ( val/1024.0)*3300;
   float cel = mv/10;
   Serial.print("TEMPERATURE = ");
   Serial.print(cel);
   Serial.println("°C");
}

femmeverbeek:
Hmmm, I replicated your result on a 3.3V BLE board. It looks like the single analogRead is not very stable even when you connect it to a voltage divider.

A little averaging solved it.

int tempPin = 1;

void setup()
{
 Serial.begin(9600);
 pinMode(tempPin,INPUT);
}
void loop()
{  float val=0;
  for (int i=1;i<1000;i++)
  { val += analogRead(tempPin);
    delay(1);
  }
  val /=1000;
  float mv = ( val/1024.0)*3300;
  float cel = mv/10;
  Serial.print("TEMPERATURE = ");
  Serial.print(cel);
  Serial.println(“°C”);
}

is for() loop just a delay here? can i change it with

if(currentMillis - previousMillis >= 1000){
previousMillis = currentMillis;
analogRead(tempPin);
}

In you quote, don’t quote the whole posting but just the text you are referring to.

is for() loop just a delay here?

no, the for loop adds 1000 measurements and the sum is divided by 1000.

can i change it with

your code does not make sense to me.

   unsigned long count=0, start=millis();
   while (millis()-start<1000) 
   { val += analogRead(tempPin);
     count++;
   }
   val /=count;

would do the trick

femmeverbeek:

   unsigned long count=0, start=millis();

while (millis()-start<1000)
  { val += analogRead(tempPin);
    count++;
  }
  val /=count;




would do the trick

Thanks, it works but now my buttons dont work good, i have to hold them about a second.
The code you gave me uses millis, whats the delay for? how to fix that?

Setrik_aZ:
float mv = ( val/1024.0)*5000;

it was working correctly with Arduino UNO when i was prototyping the project, but then i connected everything to an Arduino NANO, and its not giving me the correct temperature.

That line of code might work on an Uno (poorly), but could give more problems on a Nano were the supply usually isn’t 5.000volt.

Forget about ‘volts’. You’re not building a voltmeter.
Use the more stable 1.1volt Aref (not the default ~5volt supply) in setup .
And just convert A/D value to temperature.
See this sketch.

// connect LM35 to 5volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"

const byte tempPin = A0;
float calibration = 0.1039;
float tempC; // Celcius
float tempF; // Fahrenheit

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use internal 1.1volt Aref (INTERNAL1V1 on a Mega)
}

void loop() {
  tempC = analogRead(tempPin) * calibration; // get temp

  tempF = tempC * 1.8 + 32.0; // C to F

  Serial.print("Temperature is  ");
  Serial.print(tempC, 1); // one decimal place is all the resolution you get
  Serial.print(" Celcius  ");
  Serial.print(tempF, 1);
  Serial.println(" Fahrenheit");

  delay(1000); // use a non-blocking delay when combined with other code
}

What good will measuring for one second do (10000 measurements).
With the above code you only need one, or a few dozen averaged measurements if you get picky.
That will shorten the block-time, so your buttons??? will still register.
Leo…

Wawa:
See this sketch

Thanks! its working very well.

Now, another problem :’(
This works fine when an LCD and an RTC are connected in circuit.

But when I connect an HC-05(which was there before, non of these parts added after the problem), before it connect to another device like my phone, the board LED keep blinking fast, you probably know what im talking about,
in that time the temperatur keeps jumping about 10 degrees very fast.

I measured the current drawn by HC-05, after and befor connecting to another device, both is 0.02Amps.

But the voltage between 5V and GND pin of Arduino which is powered by a 5V 1A phone charger will keep changing from 5 to 4.8 Volts like a wave i guess, I dont have an oscilloscope, Im reading it from multimeter. The phone charger output is very stable, and there’s definitely not more than 1Amp drawned from it.

What do think? is it related to wrong measurements? and why this happens?

Thanks, it works but now my buttons dont work good, i have to hold them about a second.
The code you gave me uses millis, whats the delay for? how to fix that?

The delay is in the code you posted yourself.

You did not ask for the use of a button. That changes a lot, because now the sketch must be written in non blocking code. e.g.

I connected a pull down pushbutton to digital pin 3. When pushed, the led blinks.

int tempPin = A1;
void setup()
{
  Serial.begin(9600);
  pinMode(3,INPUT);    // connect the switch to digital pin 3 and the ground
  pinMode(LED_BUILTIN,OUTPUT);   // use the onboard led as indicator
}
void loop()
{  
  static unsigned long start = millis();  // start gets assigned only the first time
   if (millis()-start>1000)    //print every second
   { start=millis();
     float Temp = (analogRead(tempPin)/10240.0)*3300;
     Serial.print("TEMPERATURE = ");
     Serial.print(Temp);
     Serial.println("°C ");
   }
  digitalWrite(LED_BUILTIN,!digitalRead(3) && (millis()/100 % 2));  //one line led blinker
}

explanation:
The loop is constantly polling, never blocked.
The code between the brackets of the "if" statement is executed only once every second.
See if you can work out hat happens in the one line led blinker.

Forget about 'volts'. You're not building a voltmeter.
Use the more stable 1.1volt Aref (not the default ~5volt supply) in setup .

Interesting, It works on an UNO but not on a Nano 33 BLE sense. The hardware isn't there

"Nano" and "Nano 33 ..." are two completely different boards.
The makers confusingly gave them similar names.
The 'old' Nano is a 5volt board with the same processor as the Uno.
It's more simple USB supply backflow arrangement results in a lower (~4.6volt) supply on USB.

Ratiometric sensors, like pots, work fine on any MCU supply.
But sensors with a voltage output (LM35) need an absolute/voltage reference.
You can switch to that in setup().
This page lists the options for different processors.
For a "33 BLE ..." I would use the 1.0volt or 1.65volt option.
The lower voltage has a slightly higher temp resolution, the higher one a higher max temp (50C vs. 82.5C).
Leo..

Wawa:
"Nano" and "Nano 33 ..." are two completely different boards.

So is the BLE (Sense). The only thing it shares with the 33 is the pinout. It is a completely new architecture based on embedded. The nRF52840 ADC processor does not have a Vref input and the function
analogReference has not been implemented.

The onboard MPM3610 regulator gives off a voltage ripple at 2MHz which disturbes the measurement.

There is a topic about it. If you know a software solution for this please say so there.

Wawa:
It's more simple USB supply backflow arrangement results in a lower (~4.6volt) supply on USB.

Whats the reason for that?
And its not only when its powered by USB, as I said:

But the voltage between 5V and GND pin of Arduino which is powered by a 5V 1A phone charger will keep changing from 5 to 4.8 Volts like a wave i guess