Lesson 42: Using LM35 Temperature Sensor with Arduino
Part 4: Temperature Sensors
In this lesson we learn how to use LM35 three pin sensor to measure temperature and display it on the Serial Monitor. There is separate code to use it with LCD display.
/*
* Robojax Arduino Step By Step Course
* Part 4: Temperature Sensors
* Lesson 42: Introduction to LM35
* This Arduin sketch is to use LM35 to measure temperature
* This code has two way to get temperature
* 1-To print the temperatuere either in C, F or K on the serial monitor
* 2-To return the value in C, F and K
*
Please watch video instruction here https://youtu.be/DRIC4wDu878
This code is available at http://robojax.com/course1/?vid=lecture37
with over 100 lectures Free On YouTube Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
* Written by Ahmad Shamshiri on May 08, 2020 at 02:45 in Ajax, Ontario, Canada
* in Ajax, Ontario, Canada. www.robojax.com
*
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
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 <https://www.gnu.org/licenses/>.
*/
const int inPin =A0;//can change
const int iteration = 1000; //can change (see video)
const float LM35_FACTOR =0.01;// do not change
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
// Robojax.com Code YouTube Watch it here http://robojax.com/L/?id=338
Serial.begin(9600);
Serial.println("Robojax LM35 for Arduino");
delay(500);
}
// the loop routine runs over and over again forever:
void loop() {
//robojax.com LM35 Code for Arduino
printTemperature('C');
Serial.println();
printTemperature('F');
Serial.println();
printTemperature('K');
Serial.println();
Serial.println();
// Serial.print(" Temperature: ");
// printDegree();
// Serial.print(getTemperature('C'));
if(getTemperature('C') >87)
{
// do somethig here (watch video)
}
// Serial.println();
delay(1000);//Wait for 1000ms (change it if you want)
}
/*
* getTemperature()
* @brief gets the average temperature
* @param average temperature
* @param "type" is character
* C = Celsius
* K = Keliven
* F = Fahrenheit
* @return returns one of the values above
* Written by Ahmad Shamshiri for robojax.com
* on May 08, 2020 at 02:36 in Ajax, Ontario, Canada
*/
float getTemperature(char type)
{
// Robojax.com Code YouTube Watch it here http://robojax.com/L/?id=338
float value,voltage,temperature;//define variables
int sensorValue;
float averageTemperature =0;
for(int i=0; i< iteration; i++)
{
sensorValue = analogRead(inPin);//read analog value
voltage = sensorValue * (5.0 / 1023.0); //convert it to voltage
temperature = voltage / LM35_FACTOR; //convert voltage to temperature
averageTemperature += temperature;//add
}
averageTemperature /=iteration;
if(type =='F')
{
value = averageTemperature *9/5 + 32;//convert to Fahrenheit
}else if(type =='K')
{
value = averageTemperature + 273.15;//convert to Kelvin
}else{
value = averageTemperature;// return Celsius
}
return value ;
}//getTemperature()
/*
* printTemperature()
* @brief prints temperature on serial monitor
* @param charact type
* @param "type" is character
* C = Celsius
* K = Keliven
* F = Fahrenheit
* @return none
* Written by Ahmad Shamshiri for robojax.com
* on May 08, 2020 at 02:45 in Ajax, Ontario, Canada
*/
void printTemperature(char type)
{
// Robojax.com Code YouTube Watch it here http://robojax.com/L/?id=338
float value;
float temp = getTemperature(type);
Serial.print(temp);
printDegree();
if(type =='F')
{
Serial.print("F");
}else if(type =='K')
{
Serial.print("K");
}else{
Serial.print("C");
}
}//printTemperature()
/*
* @brief prints degree symbol on serial monitor
* @param none
* @return returns nothing
* Written by Ahmad Shamshiri on July 13, 2019
* for Robojax Tutorial Robojax.com
*/
void printDegree()
{
Serial.print("\xC2");
Serial.print("\xB0");
}