Friday, June 7, 2013

Arduino: Temperature Sensor LM35

Greeting Everyone,

Let us revisit Arduino and make a silly project. A very simple one day project using LM35 temperature sensor and an LCD display to show the temperature. The LM35 can be used to sense temperature from -55 Celsius to +150 Celsius. The data sheet can be downloaded from this link: Click Here

What would you need for the project:
1.) Arduino Uno (PC with cable and setup necessary)
2.) LCD display
3.) LM35 Temperature Sensor
4.) Wires

The figure below shows the basic setup and connections:
Here is the code:
For downloading the code please Click Here.
 

#include <LiquidCrystal.h>            // include the LCD driver library  
                                      // declare variables  
 float tempC = 0;                     // Variable for holding Celcius temp (floating for decimal points precision)  
 float tempf = 0;                     // variable for holding Fareghneit temp  
 int tempPin = 0;                     // Declaring the Analog input to be 0 (A0) of Arduino board.  
 float samples[8];                    // Array to hold 8 samples for Average temp calculation  
 float maxi = 0,mini = 100;           // Max/Min temperature variables with initial values. LM35 in simple setup only measures Temp above 0.  
 int i;  
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins  
 void setup()  
 {  
 pinMode(9, OUTPUT);   
 analogWrite(9, 45);  
 lcd.begin(16, 2);                    // Set up the LCD's number of columns and rows:  
 lcd.setCursor(6, 0);                 // Set LCD cursor position (column, row)  
 lcd.print("RnD's");                  // Print text to LCD  
 lcd.setCursor(3, 1);                 // Set LCD cursor position (column,row)   
 lcd.print("Thermometer");            // Print text to LCD  
 delay(5000);                         // Delay to read text  
 lcd.clear();                         // Clear the display  
 }  
 void loop()  
 {                                    // Start of calculations FOR loop.  
 for(i = 0;i<=7;i++){                 // gets 8 samples of temperature  
 samples[i] = ( 5 * analogRead(tempPin) * 100.0) / 1024.0;  
                                     // conversion math of LM35 sample to readable temperature and stores result to samples array.   
                                     // 5v is the supply volts of LM35. Change appropriatelly to have correct measurement. My case is 4.4Volts.  
                                     // If powered from USB then use value 4.4v to 4.6v. If power is 7v< to the Arduino then use 4.9v to 5.1v                                  
                                     // The voltage is critical for accurate readings  
                                     // ( LCD note: line 1 is the second row, since counting begins with 0):  
 lcd.setCursor(0, 0);                // Set LCD cursor position (column 0, row 0)  
 lcd.print("Current Temp is: ");     // Print text to LCD  
 lcd.setCursor(1, 1);                // Set LCD cursor position (column 1, row 1)  
 lcd.print(" Celcius  ");            // Print text to LCD  
 lcd.setCursor(12, 1);               // Set LCD cursor position (column 12, row 1)  
 lcd.print(samples[i]);              // print current Temp sample to LCD  
 tempC = tempC + samples[i];         // do the addition for average temperature  
 delay(800);                         // wait 800ms  
 }                                   // END of FOR loop  
 tempC = tempC/8.0;                  // calculated the averare of 8 samples in Celcius  
 tempf = (tempC * 9)/ 5 + 32;        // converts to fahrenhei  
 if(tempC > maxi) {maxi = tempC;}    // set max temperature  
 if(tempC < mini) {mini = tempC;}    // set min temperature  
 tempC = 0;                          // Set tempC to 0 so calculations can be done again  
 }  




1 comment: