Temperature sensors are widely used in domestic and industrial applications such as: refrigerators; ovens; HVAC environmental control; in the automotive industry to monitor air intake, coolant or cylinder head temperatures. Even your television has a temperature sensor to shut it down if the internal temperature rises above a certain limit.
The goal of these sensors is to measure heat and ensure that a process is either: staying within a certain range, providing safe use of that application, or meeting a mandatory condition when dealing with extreme heat, hazards, or inaccessible measuring points.
For this project I used a LM35CAZ temperature sensor from Texas Instruments (datasheet). The LM35 series are precision integrated-circuit temperature sensors with an output voltage linearly proportional to the Celsius temperature: 0mV + 10mV/ºCelsius. That is, for each 1ºCelsius increase, the temperature sensor output voltage will raise 10mV. This is when the Arduino and one of its analog inputs comes in.
This is why I love working with this development tool. To build a thermometer you just need:
– 1x Arduino Nano 3.0
– 1x LM35 temperature sensor
– Breadboard and wires
Now you have to replicate the circuit below:
The code follows below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/***************************************************************************** * Copyright (C) 2012 by Vasco Ferraz. All Rights Reserved. * * * * This program is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see <http://www.gnu.org/licenses/>. * * * * Author: Vasco Ferraz * * Contact: http://vascoferraz.com/contact/ * * Description: http://vascoferraz.com/tutorials/lm35-temperature-sensor/ * *****************************************************************************/ const int analogtemp=0; // This is the analog pin which is measuring the input voltage from the LM35 temperature sensor double temp=0, Vin=0, samples[250]; unsigned int j=0; const double Vref=1100.0; // The setup function runs only once when you power or reset the board void setup () { Serial.begin(9600); analogReference(INTERNAL); } // The loop function runs indefinitely after the setup function ends void loop () { Vin=0; temp=0; for(j=0 ; j<=249 ; j++) { samples[j] = (analogRead(analogtemp)); // Each sample is a value from 0 to 1023. Reading "j" values will help making the reading more accurate. Vin = Vin + ( samples[j] * Vref/1024.0); // Here you convert from ADC to miliVolt -> Voltage = ADC*Vref/1024. ADC = [0;1023] // For a 10-bit ADC with an 1100mV reference, the most you can measure without saturating the ADC would be (1100mV - 1100mV/1024) = 1098.9mV. // In other words, a reading of 1023 from your ADC equals (1023 * 1100/1024mV) equals 1098.9 mV. // Remember, 10 bits can only represent a value as large as b1111111111 or decimal 1023. } Vin=Vin/250.0; // Calculate the average value from all "j" readings. temp = (Vin/10.0); Serial.print(temp,1); Serial.println(" degrees Celsius"); } |
Now open the serial monitor and watch the magic.
Keep in mind that, depending on which Arduino Nano hardware revision you own, there are two analog pin sequences. Confused? Well, an image worth more than a thousand words:
If you prefer to work with temperatures in Fahrenheit, you can use the formula below to convert the temperature from Celsius to Fahrenheit:
°F = °C x 1.8 + 32