# problem with arduino mega and the temperature sensor lm35

hi ,
this is my first time that i use the arduino and i have no idea about programming also about every thing dealing with it so i refer to the forum to get learning and from youtube about how to use it so ,
i study master degree in mechanical and in my experiment i want to measure the temperature in many point using different sensor in the same time but at first to get some learn on how to use it i try with one sensor first ( lm35 )
i connect the circuit
and find the code while im google for such this case so i make copy and past then uploaded the code to the arduino but i get a wrong result i dont know why and where is the problem
actually i find more than 2 code and i tried them but also get wrong result …
the photo in the attachment show the code and the result that i get it
thank you

I think it is caused by integer math overflow, please post the whole code next time

Try this

``````void setup()
{
Serial.begin(9600): // why not 115200 ?
Serial.println("Start temp monitoring 0.1");

}

void loop()
{
int LM35 = analogRead(A0);
float celsius = LM35 * 500.0 / 1024.0;  // do the math in float;

Serial.println("RAW\tTEMP");
Serial.print(LM35);
Serial.print("\t");
Serial.println(Celsius, 1);

delay(2000);
}
``````

actually i have more than 3 code for measuring the temperature using the lm35
and ill list them below ,but the problem no one of them are work well with me
so i dont know what is the right > ????????
this is the full code

float LM35=0;
float input=0; // reading the voltage on pin A0
float celsius=0;
void setup(){
Serial.begin(9600);
pinMode(A0,INPUT);
}

void loop(){

## LM35 = analogRead(A0); input = (LM35*5000)/1024; // convert raw sensor value to millivolts celsius = input/10; // convert millivolts to Celsius Serial.println(" sensor read"); Serial.println(LM35); Serial.println(" --------------------------"); delay(1000); Serial.print("Temperature: "); Serial.print(celsius,2); Serial.println(" degrees C"); Serial.println("_ _ _ _ _ _ _ _ _ _ _ _ _ _ "); delay (2000); // wait a 2 second }

this is the second code that i use
int outputpin= 0;
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
}

//main loop
void loop()
{
float millivolts= (rawvoltage/1024.0) * 5000;
float celsius= millivolts/10;
Serial.print(celsius);
Serial.print(" degrees Celsius, ");

Serial.print((celsius * 9)/5 + 32);
Serial.println(" degrees Fahrenheit");
delay(1000);

## }

this is the third code that i use
int outputpin= 0;
//this sets the ground pin to LOW and the input voltage pin to high
void setup()
{
Serial.begin(9600);
}

## //main loop void loop() { int rawvoltage= analogRead(outputpin); float volts= rawvoltage/205.0; float celsiustemp= 100.0 * volts - 50; float fahrenheittemp= celsiustemp * 9.0/5.0 + 32.0; Serial.print(celsiustemp); Serial.println(" Celsius"); Serial.print(fahrenheittemp); Serial.println(" Fahrenheit"); delay(300000); }

all of them the same problem

itry the code sent :

void setup()
{
Serial.begin(9600): // why not 115200 ?
Serial.println(“Start temp monitoring 0.1”);

}

void loop()
{
int LM35 = analogRead(A0);
float celsius = LM35 * 500.0 / 1024.0; // do the math in float;

Serial.println(“RAW\tTEMP”);
Serial.print(LM35);
Serial.print("\t");
Serial.println(Celsius, 1);

delay(2000);
}
also i check for ( 9600) and (115200)
and no different in reading the serial monitor i attach it below ,
i just also want to know …is there any way to know that the lm35 damage or not ??

thank you for help

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

celsius
Celsius
different variables
Tom…

abdullahmohamed:
actually i have more than 3 code for measuring the temperature using the lm35
and ill list them below ,but the problem no one of them are work well with me
so i dont know what is the right > ????????

What is the raw reading from A0?

for 25C it should be about 51

(51/1024.0)*500 = 24.90C

What is your Raw A0 value?

``````void setup (){
Serial.begin(115200);
float f;
int i = 51;// LM35 output 10.0mV/C
/* this value is with VCC=5v on a UNO, 5v/1024 =0.0048828125 volt per digit resolution
at a temperature of 25C = .250v /0.0048828125 = 51.2
*/
f=(i/1024.0)*500.0;
Serial.print("analog 51 = 25C\nCalc'd with float const=");
Serial.println(f,1);
Serial.print("Calc'd with one float const, one int const =");
f=(i/1024.0)*500;
Serial.println(f,1);
Serial.print("Calc'd with two int const =");
f=(i/1024)*500;
Serial.println(f,1);

}

output

analog 51 = 25C
Calc'd with float const=24.9
Calc'd with one float const, one int const =24.9
Calc'd with two int const =0.0
``````

Chuck.

An LM35 outputs 10mV per degree C.
If you read that with the default 5volt Aref, the resolution is about 0.5°C per digital value.
That, and the maths error, and trying to display 1/100°C could get messy.
Lots of gaps between printed values.

You could start by using the internal 1.1Vref of the Arduino.
That changes resolution to ~0.1°C per digital value.

You could read the sensor several times, to get an averaged and more stable reading.
Leo..

Try this code. It reads the sensor with 1.1volt Vref, and it averages 20 analogue reads.
!One line needs changing for a Mega! Read the code…
Not tested with a sensor, but tested with a voltage source.
Leo…

``````// LM35 temp sensor connected to Analogue-in 0, +5volt and ground
// ~2 to ~102 degrees C
//
unsigned int total;
float tempC;
float tempF;
//
void setup() {
analogReference(INTERNAL); // use the internal ~1.1volt reference, change INTERNAL to INTERNAL1V1 for a Mega
Serial.begin(9600);
}
//
void loop() {
for (int x = 0; x < 20; x++) { // 20 readings for averaging
total = total + analogRead(0); // add each value to a total
}
Serial.print("Raw average = ");
Serial.print(total * 0.05, 1); // 1/20 of 20 readings, one decimal place
if (total == 20460) { // max value with 20 readings
Serial.println("   ----too hot----");
}
else {
tempC = total * 0.00519043; // calibration maths, depends on actual Vref, (0.0048828125 * Vref in volts)
tempF = tempC * 1.8 + 32; // Fahrenheit conversion
Serial.print("   The temperature is  ");
Serial.print(tempC, 2); // two decimal places
Serial.print(" Celcius  ");
Serial.print(tempF, 1); // one decimal place
Serial.println(" Fahrenheit");
}
delay(1000); // slows readings
total = 0; // reset value
}
``````

edit:
Added code for a TMP36.
Running, but not tested with a sensor.

``````// TMP36 temp sensor connected to Analogue-in 0, +5volt and ground
// ~-50 to ~+150 degrees C
//
unsigned int total;
float tempC;
float tempF;
//
void setup() {

// comment out next line for other Arduinos
analogReference(INTERNAL2V56); // ---leave this line for MEGA ONLY---

Serial.begin(9600);
}
//
void loop() {
for (int x = 0; x < 20; x++) { // 20 readings for averaging
total = total + analogRead(0); // add each value to a total
}
Serial.print("Raw average = ");
Serial.print(total * 0.05, 1); // 1/20 of 20 readings, one decimal place

// leave ONE of these lines!!!
//tempC = (total * 0.01611328) - 50; // calibration maths, depends on actual 3.3volt supply, (3.3volt * 5 / 1024) ---this line is for 3.3volt Arduinos---
//tempC = (total * 0.02441406) - 50; // calibration maths, depends on actual 5volt supply, (5.0volt * 5 / 1024) ---this line is for 5volt Arduinos---
tempC = (total * 0.0125) - 50; // calibration maths, depends on actual 2.56Vref, (2.56volt * 5 / 1024)  ---this line is for the MEGA ONLY---

tempF = tempC * 1.8 + 32; // Fahrenheit conversion
Serial.print("   The temperature is  ");
Serial.print(tempC, 2); // two decimal places
Serial.print(" Celcius  ");
Serial.print(tempF, 1); // one decimal place
Serial.println(" Fahrenheit");
delay(1000); // slows readings
total = 0; // reset value
}
``````

i also try to use this code and the result was :
int t=0;
int vcc=A0; // sets analog input A0 as +5V source for LM35
int sensor=A1; // sets A1 as the sensor input
int gnd=A2; // sets analog input A2 as ground for LM35
float temp;
float tempc;
float tempf;
void setup()
{
pinMode(vcc,OUTPUT);
pinMode(gnd,OUTPUT);
pinMode(sensor,INPUT);
digitalWrite(vcc,HIGH); // sets analog input A0 HIGH
digitalWrite(gnd,LOW); // sets analog input A2 LOW
Serial.begin(9600); // sets the baud rate at 9600

}
void loop()
{ delay(2000); // calls a 2 second delay
t=t+2; // increments the time by 2 every two seconds
tempc=(temp5)/10; // converts the digital value into temperature degree C
tempf=(tempc
1.8)+32; // converts degree C to degree F
Serial.println("…");
Serial.println(“Temperature logger”);
Serial.print("Time in sec = "); // prints the time on serial monitor window
Serial.println(t);
Serial.print("Temperature in deg C = "); // prints the temperature in degreeC
Serial.println(tempc);
Serial.print("Temperature in deg F = "); // prints the temperature in degreeF
Serial.println(tempf);
}
and the result was

MY connection is just like this one but i use Arduino mega instead of UNO

actually got these error when i use the code you send to me

My code clearly states that you have to change "INTERNAL" to "INTERNAL1V1" for a MEGA.

Leo..

you mean i should use 1.1v instead of 5v??
actually im mechanical engineering i have no idea please explain more ..

and which code i should use

You could try the code from post#7

The code is written for an Arduino UNO.
An Arduino MEGA is different.
You have to change ONE line of code....

analogReference(INTERNAL);

change that line to:

analogReference(INTERNAL1V1);

Leo..

when i try to use the code you sent to me this error happened

i check it on Arduino uno and this reading get from it

and this photo show when i change

analogReference(INTERNAL);

change that line to:

analogReference(INTERNAL1V1);

exactly as you said when using Arduino mega2560

Post#16, the UNO

"too hot" means that it's hotter than 102 degrees celcius in your room.

Or.. you have connected your sensor the wrong way.

Or.. you had connected your sensor the wrong way, and it's blown.

Ground pin of the LM35 to ground.
Supply pin of the LM35 to 5volt.
Output pin of the LM35 to analogue 0 pin.

That picture of the data in the serial monitor tells me that the program works fine.
Leo..

Post#17, the Mega.

If I change INTERNAL to INTERNAL1V1

And change the board from UNO to MEGA in the IDE.

It compiles, uploads, and runs without a problem.

I get the same "too hot" in the serial monitor if I don't connect a sensor to the board.
Leo..