Arduino output not stable

Hi all,

I am totally new to Arduino and getting my hands on a simple temperature sensor with LM35.
I connected the LM35 directly to Arduino based on the pin (5V, GND and A1). No pull up/down resistor, no capacitor involved.

I am powering up the Arduino via PC USB.
The issue that I faced here is I am getting intermittent reading. The reading fluctuates between 0 and approx 500mV of LM35.

I have tried:

  1. Measuring the PC USB port power using a USB power meter, It shows a stable 5.02V.
  2. Connecting to a AC-DC power source with 12V output using barrel jack on Arduino.
  3. Change another Arduino board.

Yet I am getting the same fluctuating output. Please help. Thank you so much in advance.

const int sensorPin = A1; 
float sensorValue;
float vOut;
float tempC;

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  vOut = (sensorValue * 5000) / 1024;
  
  tempC = vOut / 10;

  Serial.print("Temperature(ºC): ");
  Serial.print(tempC);

  Serial.print("  Voltage(mV): ");
  Serial.println(vOut);
  delay(1000);
}
Output : 

15:59:01.657 -> Temperature(ºC): 23.93  Voltage(mV): 239.26
15:59:02.643 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:03.659 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:04.644 -> Temperature(ºC): 25.88  Voltage(mV): 258.79
15:59:05.673 -> Temperature(ºC): 40.53  Voltage(mV): 405.27
15:59:06.657 -> Temperature(ºC): 41.99  Voltage(mV): 419.92
15:59:07.673 -> Temperature(ºC): 40.04  Voltage(mV): 400.39
15:59:08.661 -> Temperature(ºC): 36.62  Voltage(mV): 366.21
15:59:09.653 -> Temperature(ºC): 22.46  Voltage(mV): 224.61
15:59:10.670 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:11.659 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:12.657 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:13.677 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:14.666 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:15.687 -> Temperature(ºC): 25.39  Voltage(mV): 253.91
15:59:16.679 -> Temperature(ºC): 40.53  Voltage(mV): 405.27
15:59:17.667 -> Temperature(ºC): 41.02  Voltage(mV): 410.16
15:59:18.687 -> Temperature(ºC): 40.53  Voltage(mV): 405.27
15:59:19.676 -> Temperature(ºC): 32.23  Voltage(mV): 322.27
15:59:20.691 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:21.682 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:22.680 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:23.670 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:24.691 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:25.682 -> Temperature(ºC): 40.04  Voltage(mV): 400.39
15:59:26.705 -> Temperature(ºC): 41.02  Voltage(mV): 410.16
15:59:27.695 -> Temperature(ºC): 41.50  Voltage(mV): 415.04
15:59:28.684 -> Temperature(ºC): 36.62  Voltage(mV): 366.21
15:59:29.673 -> Temperature(ºC): 28.81  Voltage(mV): 288.09
15:59:30.693 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:31.710 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:32.673 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:33.690 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:34.676 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:35.697 -> Temperature(ºC): 40.04  Voltage(mV): 400.39
15:59:36.707 -> Temperature(ºC): 40.53  Voltage(mV): 405.27
15:59:37.692 -> Temperature(ºC): 40.53  Voltage(mV): 405.27
15:59:38.712 -> Temperature(ºC): 37.11  Voltage(mV): 371.09
15:59:39.690 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:40.712 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:41.693 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:42.682 -> Temperature(ºC): 0.00  Voltage(mV): 0.00
15:59:43.703 -> Temperature(ºC): 0.00  Voltage(mV): 0.00

Have you used a different sensor? Also try running a separate power supply to your sensor.
Also googled it and found a great guide. try reading it! LM35 Temperature Sensor Arduino Tutorial (3 Examples) (makerguides.com)

hi everone, thanks for your response.

Yes, i tried with another unit of LM35 also produce the same results. I am totally puzzled with this. I measure the voltage across LM35 Vout and GND from multimeter and it shows stable results. Just the Arduino output is fluctuating.

My guess would be a bad connection. Did you measure the sensor output voltage between the Arduino GND pin and the A1 pin?

Your code is a little too big

(Uncompiled untested)

const byte sensorPin = A1;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  float  vOut = (sensorValue * 5000UL) /  1024.0;
 
  float tempC = vOut / 10;

  Serial.print("Temperature(ºC): ");
  Serial.print(tempC);

  Serial.print("  Voltage(mV): ");
  Serial.println(vOut);
  delay(1000);
}

And yes, check your wiring.

  float  vOut = (sensorValue * 5000UL) /  1024.0;

Kudos for using the correct scale factor. But the UL is sort of a non sequitur here. 'sensorValue' is a float type so the whole expression is evaluated as a float expression.

My sensorValue is (correctly) an int.

In that case may I suggest

  float  vOut = sensorValue * (5000.0 / 1024.0);

which will work with either float or int.

Or maybe:

  float tempC = sensorValue * (5000.0 / 1024.0 / 10.0);

I think the issue when the reading goes to 0 is programmatic not connections or sensor.

I think the variations in readings (excluding the 0's) could be just noise. Maybe being aggravated by how you have it wired.

I would start with the analoginput example in the arduino IDE (see file/examples).

Then add a print statement printing out the raw analog read value.

If that all works start adding in your calculations.

Are you trying to take the temp of a liquid? Looks that way to me.

-jim lee

Sample code for the LM35 does not work for the most part. My students tried several, more like all, without success
Going to a digital temp sensor seemed the only solution until tried using the map function for the conversion.
If anyone is still interested in the LM35, this code works.

int value = analogRead(A0); //read analog value from LM35
int value2 = map(value,0,205,0,1023); //use map function to convert voltage
int celsius = value2/10, //divide by 10 for celsius
int farenhite = (celsius*9)/5 + 32; //conversion for farenhite

The output is stable using this code. Other examples jumped all around. Totally unusable

For the benefit of your students, sp. “Fahrenheit”

int value = analogRead(A0);                   //read analog value from LM35
int value2 = map(value,0,205,0,1023);    //use map function to convert voltage

That doesn't seem right. That 'map()' is equivalent to multiplying by 1023/205 which is 4.99. With a 4.99V reference voltage that will get you "Volts * 1024" which is sort of close to millivolts but not quite. The proper calculation is:

const float AnalogReferenceMillivolts = 5020.0;  // Measured at the AREF pin

float millivolts = (analogRead(A0) * AnalogReferenceMillivolts) / 1024.0;
int celsius = (millivolts / 10.0) + 0.5;           // 10 millivolts per degree celsius, rounded

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.