LM35 temperature sensor interfacing with Arduino code
Hello, welcome back. In this tutorial, we will learn what an LM35 sensor is and how it works with an Arduino. Also, the shape of this sensor is similar to the shape of a transistor. We can get the temperature as an analog value through this sensor. The main advantage of this sensor is that it can get very accurate values at a very low cost.
How does this LM35 sensor work?
This sensor operates according to a method called linear scale factor (10mv / ° C). That is, 10 millivolts is 1 degree centigrade. For example, if a 100 millivolt output is received through the sensor, the temperature is 10 degrees Celsius. In this way, the temperature range can be obtained on the output voltage. We can get a temperature range of -55 degrees to 150 degrees through this sensor. Also, the input voltage can be given as 4 to 30.
We will use the LM35 sensor module for this tutorial.
The PIN structure of this sensor module
Okay, let’s learn how to connect this sensor module with Arduino. The required components are as follows.
- Arduino Nano board x 1 — Our Store / Amazon
- LM35 sensor module x 1 — Our Store / Amazon
- LCD display x 1 — Our Store / Amazon
- I2C module x 1 — Our Store / Amazon
- Breadboard x 1 — Our Store /Amazon
- Jumper wires — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
Secondly, connect these components. To do this, use the circuit diagram below.
Step 3
Thirdly, let’s create the program for this tutorial. It is as follows.
/*Temperature sensor with Arduino.
created by the SriTu Hobby team.
Read the code below and use it for any of your creations.
Home Page
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define sensor1 A0
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop() {
int value1 = analogRead(sensor1);
double tempC = value1 * (5.0 / 1023.0) * 100;
double tempF = tempC * 9 / 5 + 32;
lcd.setCursor(0, 0);
lcd.print("..Temperature..");
lcd.setCursor(0, 1);
lcd.print("C:");
lcd.print(tempC);
lcd.setCursor(8, 1);
lcd.print(" F:");
lcd.print(tempF);
Serial.print("Temperature C: ");
Serial.print(tempC);
Serial.print("'C");
Serial.print("\t");
Serial.print("Temperature F: ");
Serial.print(tempF);
Serial.println("'F");
}
Code explanation
Firstly, the I2C library is included and object is created for this library.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Secondly, the sensor pin is defined.
#define sensor1 A0
In the setup function, the serial monitor and LCD begin.
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
In the loop function,
void loop() {
//This code gets the analog values and puts them into the integer variable
int value1 = analogRead(sensor1);
//This code Converts analog value to centigrade degrees
double tempC = value1 * (5.0 / 1023.0) * 100;
//This code Convert centigrade to the Fahrenheit
double tempF = tempC * 9 / 5 + 32;
//The centigrade and Fahrenheit values are displayed on the LCD
lcd.setCursor(0, 0);
lcd.print("..Temperature..");
lcd.setCursor(0, 1);
lcd.print("C:");
lcd.print(tempC);
lcd.setCursor(8, 1);
lcd.print(" F:");
lcd.print(tempF);
//The centigrade and Fahrenheit values are displayed on the Serial monitor
Serial.print("Temperature C: ");
Serial.print(tempC);
Serial.print("'C");
Serial.print("\t");
Serial.print("Temperature F: ");
Serial.print(tempF);
Serial.println("'F");
}
Step 4
Lastly, select the board and port. After, upload this code to the Arduino board.
OK, enjoy this tutorial. The full video guide is given below. So, we will meet in the next video. Have a good day.
LM35 temperature sensor interfacing with Arduino code