arduino code for temperature sensor lm35

Connection of LM35 with Arduino


int Sensor_Pin = 0;
int raw_val;
float final_temp;

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

void loop()
{
raw_val = analogRead(Sensor_Pin);
final_temp = raw_val / 9.31;
Serial.println(final_temp);
delay(1000);
}

The Sensor LM35 required 5 volt to work but it can be upto 20 volt, but in our case we are taking power from the arduino board, so its 5 Volt.

Once you are ready with the connection, now its time to program it.

The first section of the program is initializing section where we initialize the variable we required in our setup. As we know we have only one sensor in our setup, so we need to give the name to the sensor pin where we are going to attach the sensor.

Description of Program :

  1. int Sensor_Pin = 0;
  2. int raw_val;
  3. float final_temp;
  4. void setup()
  5. {
  6. analogReference(INTERNAL);
  7. Serial.begin(9600);
  8. }
  9. void loop()
  10. {
  11. raw_val = analogRead(Sensor_Pin);
  12. final_temp = raw_val / 9.31;
  13. Serial.println(final_temp);
  14. delay(1000);
  15. }
  • Line 1 is the initialization of the pin on which we are going to attach our sensor
  • Line 2 is for taking the value from sensor in raw condition before processing it.
  • Line 3 is for after processing.
void setup()
{
analogReference(INTERNAL);
Serial.begin(9600); //This is for Baud rate
}

This is a setup section and it is called when a sketch(Program) starts. This is for initialize variables, pin modes, start using libraries, etc. This section only run once, after each power up or reset of the Arduino board.Serial.begin starts the serial communication at the baud rate of 9600.

 

void loop()
{
raw_val = analogRead(Sensor_Pin);
final_temp = raw_val / 9.31;
Serial.println(final_temp);
delay(1000);
}

This section runs in an infinite loop or you can say until the board is switched off. All the program which requires continuous processing comes under this section. As we know our sensor need to read the values continuously, so we put our code in this section.

raw_val = analogRead(Sensor_Pin);

This line Read the analog value from the sensor pin and assign the same to the raw_val variable

final_temp = raw_val / 9.31;

Here we have the raw value but its shows the wrong output, so we need to process it before displaying here we divide the value by 9.31 and the reason behind is, The LM35 only produces voltage from 0 to +1v and the ADC of the Arduino board uses 5 volt as the highest possible value because of this it wasted 80% of the possible range, but if you change analog Reference to 1.1 volt, it will give almost highest resolution as possible.

We can understand  by this way,

                                 If we devide 1.1 volt over 1024, each steps of the analog values will give the output of 0.001074 volt or 1.0742 millivolt and here if we says 10 millivolt = 1℃ that is 10/1.0742 = 9.31 approx. So we can map for every changes of 9.31 in the analog reading, there is 1℃ of temperature changes.

Serial.println(final_temp);

delay(1000);

And finally we print this value on the serial monitor with delay of 1 second which is equal to 1000 milliseconds.

Be connected with cloudyrathor and be the cloudyrathor…...Remember we can and we will make difference and Simplicity is the beauty…...