How To Use Temperature Sensor With Arduino Uno: 2 codes

Explore the fundamentals of using temperature sensors with Arduino Uno for various projects.

How to use temperature sensor with Arduino Uno

The temperature sensor is the most popular measuring device which is capable of sensing the intensity of heat generated by the object to which it is connected. These sensors are most frequently used by us through our household devices such as microwaves, refrigerators, air conditioners, and so on. In this tutorial, we will learn about the LM35 (LM means linear monolithic) temperature sensor which is an integrated circuit analog temperature sensor.

It is an easily available, less costly, and accurate result-providing sensor without external calibration or trimming. Its output voltage is linearly proportional with Centigrade temperature which is preferred over other temperature sensors calibrated in Kelvin in which extra additional calculation is needed to convert a measured temperature into centigrade.

LM35 is manufactured by Texas Instruments and is capable of measuring temperatures in the range of -55°C to +150°C. Its output voltage increases by 10 millivolts for every degree Celsius, an increment in temperature means its scaling factor is 10 millivolts per degree Celsius (10 mV/°C). For example, if the temperature being measured is 30°C, the LM35 output voltage will be 300 millivolts. LM 35 temperature sensor is an analog temperature sensor which means it provides output in analog signal form.

Some microcontrollers do not accept analog signals, therefore, they require additional circuitry like analog to digital converter (ADC) to convert the analog signal into digital.

In this tutorial, I will use Arduino Uno which has an inbuilt analog-to-digital converter. Arduino Uno has six analog input pins (A0 to A5). I will use one of these analog pins to convert the analog output of the LM35 sensor into the digital output.

In this tutorial, I will explain to you two projects. In the first project, you will understand, how a temperature sensor can be operated with Arduino Uno with the serial monitor.

In the second example, you will learn how to show the output temperature on LCD using Arduino Uno. I will show you how to calculate output temperature in Celsius as well as Fahrenheit in both projects.

How to use a Temperature sensor with Arduino Uno (with serial monitor)

How to use a Temperature sensor with Arduino Uno (with serial monitor)

In this example, I will show you how a temperature sensor can be operated with Arduino uno to generate output on a serial monitor. It will show the real-time measured temperature values in Celsius and Fahrenheit.

Required component

1. 1 x LM35 temperature sensor: BUY ON AMAZON

2. 1 x Arduino Uno board: BUY ON AMAZON

3. 1 x Breadboard: BUY ON AMAZON

4. Jumper wires: BUY ON AMAZON

Circuit description

LM 35 temperature sensor has three terminals power supply (VDD), output analog voltage (Vout), and ground (GND). As shown in the connection diagram the middle terminal of the sensor is connected with an analog pin A2 pin of the Arduino Uno board. The first terminal is connected to the 5-volt power supply pin of the Arduino board and the third terminal is connected to GND.

LM35 temperature sensor (flat top view)
LM35 temperature sensor (flat top view)
LM35 temperature sensor (round top view)
LM35 temperature sensor (round top view)

Connection diagram

How to use a Temperature sensor with Arduino Uno (with serial monitor) connection diagram

Code

/*
*How to use temperature sensor with Arduino Uno (with serial monitor)
*This code describes how temperature sensor works with the help of Arduino Uno to display temperature in celsius and fahrenheit on serial monitor.
*
*https://www.arduinounomagic.com/2019/01/how-to-use-temperature-sensor-with.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/


const int LM_35=A2; // LM_35 is connected to pin A2. 

float sensor_value; // variable to store temperature value.
float temp_f; // Variable to store temperature in fahrenheit.
float temp_c; // Variable to store temperature in celsius.


void setup() 
{  
Serial.begin(9600); 
}

void loop() {
 
sensor_value=analogRead(LM_35); //read data from sensor

temp_c=(sensor_value*5*100)/1024; //calculates temperature in celsius.


temp_f=(temp_c*9/5)+32; // calculates temperature in fahrenheit.

Serial.print("In celcius=");
Serial.println(temp_c); //prints temperature in celsius on serial monitor
Serial.print("In fahrenheit=");
Serial.println(temp_f); //prints temperature in fahrenheit on serial monitor.


delay(1000); //delay

}

Serial Monitor Output

Check out the screenshot of the serial monitor’s output screen. It shows the temperature in Fahrenheit and Celcius.

Temperature sensor serial monitor output

Output

Check out the video, it will help you to understand the working of the code uploaded with the Arduino.

How to use a temperature sensor with Arduino Uno (With LCD)

In this example, you will learn how to display real-time temperature values in Celsius and Fahrenheit on the LCD screen.

Required component

1. 1 x LM35 temperature sensor: BUY ON AMAZON

2. 1 x USB cable

3. 1 x Arduino Uno board: BUY ON AMAZON

4. 1 x Breadboard: BUY ON AMAZON

5. 1 x LCD (16 x 2) screen: BUY ON AMAZON

6. 1 x preset (10 k): BUY ON AMAZON

7. Jumper wires: BUY ON AMAZON

Connection diagram

How to use a temperature sensor with Arduino Uno (With LCD) connection Diagram

Circuit description

I have used a 16 x 2 LCD compatible with the Hitachi HD44780 driver. The 10k preset is used with LCD to adjust the brightness of the LCD screen. The RS pin of the LCD is connected to pin 8 of the Arduino board. The VEE pin of the LCD module is connected to the middle terminal of the preset. 

GND, RW, and LED- pin of the LCD module connected with the ground pin of the Arduino board. VDD and LED+ pin of the LCD module is connected with a 5-volt power supply pin of the Arduino board. Enable pin E of the LCD module is connected with pin 2 of the Arduino board. Data pins D4, D5, D6, and D7 are connected with pins 7, 6, 5, and 4 respectively.

The temperature sensor is a three-terminal device first terminal is connected to a 5-volt power supply pin of the Arduino, the middle terminal is connected to the A2 analog pin of the Arduino board and the third terminal is connected to the GND pin of the Arduino board.

Code

/*
*How to use temperature sensor with Arduino uno (with LCD)
*This code will describes how to use temperature sensor with arduino uno to display measured temperature in Celsius and Fahrenheit on LCD screen.
*
*for more detail please visit:https://www.arduinounomagic.com/2019/01/how-to-use-temperature-sensor-with.html
*
*Copyright (C) 2007 Free Software Foundation, Inc. <[email protected]>
*for more projects based on Arduino uno please visit: https://arduinounomagic.com/
*/

#include<LiquidCrystal.h>
LiquidCrystal ARDUINO_LCD(8, 2, 7, 6, 5, 4);// Set RS at pin=8, Enable at pin=2, data pins at: D4=7, D5=6, D6=5, d7=4.  
const int LM_35=A2;// LM_35 is connected to pin A2. 

float sensor_value; // variable to store temperature value.
float temp_f; // Variable to store temperature in fahrenheit.
float temp_c; // Variable to store temperature in celsius.


void setup() 
{  

Serial.begin(9600);
ARDUINO_LCD.begin(16, 2);//set up number of colomn and rows of LCD
delay(400);

}

void loop() {
  
sensor_value=analogRead(LM_35); //read the data from sensor

temp_c=(sensor_value*5*100)/1024; //calculates temperature in celsius.


temp_f=(temp_c*9/5)+32; // calculates temperature in fahrenheit.

 ARDUINO_LCD.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed (at colomn=0 and row=0)
ARDUINO_LCD.print("In c=");
ARDUINO_LCD.print(temp_c); //display the temperature in celsius on LCD screen


ARDUINO_LCD.setCursor(0,1); // Sets the location at which subsequent text written to the LCD will be displayed (at colomn=0 and row=1)
ARDUINO_LCD.print("In f=");
ARDUINO_LCD.print(temp_f); //display the temperature in fahrenheit on LCD screen

delay(1000); //delay

}

Output

Check out the output video to understand the workings of the circuit in real-time.

Hope you like the Aricle. 

Now you know how to use the temperature sensor using Arduino uno. The temperature sensor is a very important electronic sensor. It can be used in higher-level applications to make a huge difference in the real world.

If you have any queries, let me know in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *