What is Temperature sensor:

Temperature sensor is the Device which is used to measure temperature. It gives an analog value which is proportional to temperature. Higher the value, more the temperature.

Code:

int temp_val;

void setup()

{

Serial.begin(9600);

}

void loop()

{

temp_val = analogRead(A0);

float temp_in_celcius = ( temp_val/1023.0)*5000; //multiplied with 5000 because we get values ranging from 0 to 5v

float temp_in_fahrenheit = (temp_in_celcius*9)/5 + 32;

Serial.print(“TEMPERATURE in Celcius is “);

Serial.print(temp_in_celcius);

Serial.println(” C”);

Serial.print(“TEMPRATURE in fahrenheit is  “);

Serial.print(temp_in_fahrenheit);

Serial.println(” F”);

delay(500);

}

Code explaination:

Here, before void setup, we initialized temp_val to store temperature value. In void setup, we did serial.begin to start program. After completing void setup, we come to void loop. We have written temp_val to store analog temperature value; temp_in_celcius to store celcius value of temperature;temp_in_fahrenheit to store fahrenheit temperature value.We are printing temperature in celcius and fahrenheit for every 500 milli seconds.

Connecting LM35 sensor:

There are 3 pins for LM35 sensor, I.e., one for Vcc, one for ground and one for temperature. When we hold LM35 and face towards flat surface, the one to the left should be connected to Vcc, the one in the center is output, which actually gives the voltage value of the temperature, and the last one is connected to the ground. The connections are shown in then figure below, And the values come in the serial port are as shown in the figure below.


Categories: ArduinoTutorials

0 Comments

Leave a Reply