Arduino-Measuring Temperature using LM35:
The LM35 temperature sensor produces an analog voltage directly proportional to temperature with an output of 1 millivolt per 0.1°C (10 mV per degree). The sketch converts the analogRead values into millivolts and divides this by 10 to get degrees. The sensor accuracy is around 0.5°C, and in many cases you can use integer math instead of floating point. The following sketch turns on pin 2 when the temperature is above a threshold:
This recipe displays the temperature in Fahrenheit and Celsius (Centigrade) using the popular LM35
heat detection sensor. The sensor looks similar to a transistor and is connected shown below:
const int inPin = 0; // sensor connected to this analog pin
const int outPin = 13; //2; // digital output pin
const int threshold = 25; // the degrees celsius that will trigger the output pin
void setup()
{
Serial.begin(9600);
pinMode(outPin, OUTPUT);
}
void loop()
{
int value = analogRead(inPin);
long celsius = (value * 500L) /1024; // 10 mV per degree c, see text
Serial.print(celsius);
Serial.print(" degrees Celsius: ");
if(celsius > threshold)
{
digitalWrite(outPin, HIGH);
Serial.println("pin is on");
}
else
{
digitalWrite(outPin, LOW);
Serial.println("pin is off");
}
delay(1000); // wait for one second
}
The sketch uses long (32-bit) integers to calculate the value. The letter L after the number causes the calculation to be performed using long integer math, so the multiplication of the maximum temperature (500 on a 5V Arduino) and the value read from the analog input does not overflow. If you need the values in Fahrenheit, you could use the LM34 sensor, as this produces an output in Fahrenheit, or you can convert the values in this recipe using the following formula:
float f = (celsius * 9)/ 5 + 32 );
lm35 sketch prints the temperature to the Serial Monitor:
const int inPin = 0; // analog pin
void setup()
{
Serial.begin(9600);
}
void loop()
{
int value = analogRead(inPin);
Serial.print(value); Serial.print(" > ");
float millivolts = (value / 1024.0) * 5000;
float celsius = millivolts / 10; // sensor output is 10mV per degree Celsius
Serial.print(celsius);
Serial.print(" degrees Celsius, ");
Serial.print( (celsius * 9)/ 5 + 32 ); // converts to fahrenheit
Serial.println(" degrees Fahrenheit");
delay(1000); // wait for one second
}
The LM35 temperature sensor produces an analog voltage directly proportional to temperature with an output of 1 millivolt per 0.1°C (10 mV per degree). The sketch converts the analogRead values into millivolts and divides this by 10 to get degrees. The sensor accuracy is around 0.5°C, and in many cases you can use integer math instead of floating point. The following sketch turns on pin 2 when the temperature is above a threshold:
This recipe displays the temperature in Fahrenheit and Celsius (Centigrade) using the popular LM35
heat detection sensor. The sensor looks similar to a transistor and is connected shown below:
const int inPin = 0; // sensor connected to this analog pin
const int outPin = 13; //2; // digital output pin
const int threshold = 25; // the degrees celsius that will trigger the output pin
void setup()
{
Serial.begin(9600);
pinMode(outPin, OUTPUT);
}
void loop()
{
int value = analogRead(inPin);
long celsius = (value * 500L) /1024; // 10 mV per degree c, see text
Serial.print(celsius);
Serial.print(" degrees Celsius: ");
if(celsius > threshold)
{
digitalWrite(outPin, HIGH);
Serial.println("pin is on");
}
else
{
digitalWrite(outPin, LOW);
Serial.println("pin is off");
}
delay(1000); // wait for one second
}
The sketch uses long (32-bit) integers to calculate the value. The letter L after the number causes the calculation to be performed using long integer math, so the multiplication of the maximum temperature (500 on a 5V Arduino) and the value read from the analog input does not overflow. If you need the values in Fahrenheit, you could use the LM34 sensor, as this produces an output in Fahrenheit, or you can convert the values in this recipe using the following formula:
float f = (celsius * 9)/ 5 + 32 );
lm35 sketch prints the temperature to the Serial Monitor:
const int inPin = 0; // analog pin
void setup()
{
Serial.begin(9600);
}
void loop()
{
int value = analogRead(inPin);
Serial.print(value); Serial.print(" > ");
float millivolts = (value / 1024.0) * 5000;
float celsius = millivolts / 10; // sensor output is 10mV per degree Celsius
Serial.print(celsius);
Serial.print(" degrees Celsius, ");
Serial.print( (celsius * 9)/ 5 + 32 ); // converts to fahrenheit
Serial.println(" degrees Fahrenheit");
delay(1000); // wait for one second
}
No comments:
Post a Comment