Working With LM35 Temperature Sensor Using Arduino Uno

In my previous article, I had explained how to control your led using serial monitor and in this article, I'll show you the working with temperature sensor. After that, we will look into how to connect the LM35 Temperature Sensor to your Arduino board and how we can sketch the program for that and finally using the temperatures how we can control your LEDs. It's quiet an interesting topic.
 

Introduction

 
Before using the temperature sensor we want to know how could we convert the values between Celsius and Fahrenheit 
 
Formula
 
°F = °C x 1.8 +32   //Fahrenheit
°C = (°F-32)/1.8  // Celsius   
 
(or) we can use the simple trick for the conversion °C * 2 and °F + 30, using this trick we can find the value of Celsius and Fahrenheit
 
STEP1: 
 
Requirements:
  • Arduino Uno
  • Bread board
  • LM35 Temperature Sensor
  • LED-2
  • Some Wires 
STEP2:
 
Connection:
 
 
Figure 1: LM35 Temperature Sensor
 
The Temperature Sensor has 3 pins:
  • +Vc to 5v
  • Vout to Analog-5
  • Gnd to Gnd
  • LED1 to 7
  • LED2 to 6
After the completion of the connection, we want to sketch the following code to the Arduino board to identify the current temperature in the room.
 
STEP3:
 
Programming:
  1. //Declare the variables  
  2. int temp;  
  3. int temppin = 5;  
  4. int led1 = 7;  
  5. int led2 = 6;  
  6. void setup()  
  7. {  
  8.     pinMode(led1, OUTPUT);  
  9.     pinMode(led2, OUTPUT);  
  10.     Serial.begin(9600); //BaudRate  
  11. }  
  12. void loop()  
  13. {  
  14.     temp = analogRead(temppin);  
  15.     float t = (temp / 1024.0) * 5000;  
  16.     float celsius = t / 10;  
  17.     float farh = (celsius * 9) / 5 + 32;  
  18.     //Celsius Temperature  
  19.     Serial.print("Temperature =");  
  20.     Serial.print(celsius);  
  21.     Serial.print("*C");  
  22.     Serial.println();  
  23.     delay(1000);  
  24.     if (celsius > 30)  
  25.     {  
  26.         digitalWrite(led1, HIGH);  
  27.     }  
  28.     else  
  29.     {  
  30.         digitalWrite(led2, LOW);  
  31.     }  
  32.     if (celsius < 30)  
  33.     {  
  34.         digitalWrite(led1, HIGH);  
  35.     }  
  36.     else  
  37.     {  
  38.         digitalWrite(led2, LOW);  
  39.     }  
  40.     //Farenhite Temperature  
  41.     /* 
  42.     Serial.print("Temperature ="); 
  43.     Serial.print(farh); 
  44.     Serial.print("*F"); 
  45.     Serial.println(); 
  46.     if(celius<30) 
  47.     { 
  48.     digitalWrite(led1,HIGH); //Sets LED ON 
  49.     } 
  50.     else 
  51.     { 
  52.     digitalWrite(led2,LOW);  / /Sets LED OFF 
  53.     } 
  54.     if(celius>30) 
  55.     { 
  56.     digitalWrite(led1,HIGH);//Sets LED ON 
  57.     } 
  58.     else 
  59.     { 
  60.     digitalWrite(led2,LOW);//Sets LED OFF 
  61.     } 
  62.     */  
  63. }  
OUTPUT: 
 
The following output of the room temperature is shown in the serial monitor screenshots below,
 
 
 
Explanation
 
Declare the variables first-named temp, temppin, led1, led2, then move on to the function mode setup() and initialize the variables, pinModes, and loop() function it is fully based upon your condition that how it wants to work. Inside the loop function, we have some values,
  1. temp=analogRead(temppin); //Read Vout from LM -35 at analog pin 5  
  2. float t=(temp/1024.0)*5000; //Convert analog data to the  Celsius Reading  
  3. float celsius=t/10;   
  4. float farh=(celsius*9)/5+32; //Convert Celsius Reading to the Fahrenheit 
    And then we want to glow your LED-based up on the room temperature. For that here we are using IF conditional statement if(celsius>30), then led1 want to glow, else led2 want to glow. Based upon your own conditions the led will work automatically.
     

    Conclusion

     
    And finally, we conclude that we made Automated temperature based led on and off the system.