How to Use the LM35 Temperature Sensor with Arduino?
1.Introduction
LM35 is a commonly used and easy-to-use temperature sensor. It does not require other hardware, only needs an analog port. The difficulty lies in compiling the code and converting the analog values to Celsius temperature. In this project, we use a temperature sensor and 3 LEDs to make a temperature tester. When the temperature sensor touches objects with different temperature, the LED will show different colors.
2.Components Required
|
|
|
|
|
---|---|---|---|---|
Keyestudio Plus Mainboard*1 |
LM35 Temperature Sensor*1 |
220Ω Resistor*3 |
Red LED*1 |
Yellow LED*1 |
|
|
|
|
|
F-F Dupont Wires |
Green LED*1 |
Breadboard*1 |
Jumper Wires |
USB Cable*1 |
3.Component Knowledge
Working principle of LM35 temperature sensor:
LM35 is a widely used temperature sensor with many different package types. At room temperature, it can achieve the accuracy of 1/4°C without additional calibration processing. LM35 temperature sensor can produce different voltage by different temperature. When the temperature is 0 ℃, it outputs 0V. If increasing 1 ℃, the output voltage will increase 10mv.
The output temperature is 0℃ to 100℃, the conversion formula is as follows.
4.Read the Temperature Value
We first use a simple code to read the value of the temperature sensor, print it in the serial monitor. The wiring diagram is shown below.
LM35 output is given to analog pin A1 of the Plus mainboard. This analog voltage is converted to its digital form and processed to get the temperature reading.
/*
Read_the_LM35_temperature_value
http//www.keyestudio.com
*/
#define sensorPin A1
void setup()
{
Serial.begin(9600);
}
void loop()
{
long reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (500 * reading) /1024 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(voltage); Serial.print(" volts - ");
Serial.print(temperatureC); Serial.print(" degrees C - ");
Serial.print(temperatureF); Serial.println(" degrees F");
delay(100);
}
Upload the code to the Plus Mainboard, connect the wires and power on first. Then turn on the serial monitor , set the baud rate to 9600 and you can read the current temperature value.
5.Circuit Diagram and Wiring Diagram
Now we use a LM35 temperature sensor and three LED lights to do a temperature test. When the LM35 temperature sensor senses different temperatures, different LED lights will light up. Follow the diagram below for wiring.
6.Code
Note: The value of “temperature F” in the code can be adjusted appropriately according to the local temperature value.
/*
Temperature_measurement
http//www.keyestudio.com
*/
#define sensorPin A1
#define greenLED 8
#define yellowLED 9
#define redLED 10
void setup()
{
pinMode(greenLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(redLED, OUTPUT);
Serial.begin(9600);
}
void loop()
{
long reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (500 * reading) /1024 ;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(voltage); Serial.print(" volts - ");
Serial.print(temperatureC); Serial.print(" degrees C - ");
Serial.print(temperatureF); Serial.println(" degrees F");
if (temperatureF >= 75) {
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
}
else if (temperatureF >= 70 && temperatureF < 75) {
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
}
else {
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
}
delay(3000);
}
7.Result
Upload the code to the Plus Mainboard, wire up and power on. Then open the serial monitor, set the baud rate to 9600. The monitor displays the current temperature value. When the LM35 temperature sensor senses different temperatures, different LED lights will light up.