One of the easiest and inexpensive ways to add temperature sensing in your Arduino project is to use TMP36 Temperature Sensor. These sensors are fairly precise and needs no external components to work. So, with just a few connections and some Arduino code you’ll be sensing temperature in no time!
TMP36 Temperature Sensor
The TMP36 is a low voltage, precision centigrade temperature sensor manufactured by Analog Devices. It is a chip that provides a voltage output that is linearly proportional to the temperature in °C and is, therefore, very easy to use with an Arduino.
The TMP36 temperature sensor is fairly precise, never wears out, works under many environmental conditions and requires no external components to work. In addition, the TMP36 sensor does not require calibration and provides a typical accuracy of ±1°C at +25°C and ±2°C over the −40°C to +125°C temperature range.
The sensor can be powered with a 2.7V to 5.5V power supply and consumes only 50µA during active temperature conversions, providing very low self-heating (less than 0.1°C in still air). In addition, a shutdown function is provided to reduce the supply current to less than 0.5µA.
Here are the complete specifications:
Power supply | 2.7V to 5.5V |
Current draw | 50µA |
Temperature range | -40°C to 125°C |
Accuracy | ±2°C |
Output scale factor | 10mV/°C |
Output range | 0.1V (-40°C) to 1.75V (125°C) |
Output at 25°C | 750mV |
For more information, please refer below datasheet.
Working Principle
The TMP36 uses a solid-state technique to measure the temperature. It makes use of the fact that the voltage drop between the base and emitter (forward voltage – Vbe) of the Diode-connected transistor decreases at a known rate as the temperature increases. By precisely amplifying this voltage change, it is easy to generate an analog signal that is directly proportional to temperature.
This linear relationship between forward voltage and temperature is the reason why diode-connected transistors are used as temperature measurement devices. Essentially this is how temperature is measured, although there have been some improvements in this technique over the years. More information about this technique can be found here.
The good news is that all these complex calculations are done inside the TMP36. It just outputs a voltage that is linearly proportional to temperature.
How to Measure Temperature
The TMP36 is easy to use, just connect the left pin to power (2.7-5.5V) and the right pin to ground (assuming the flat side of the sensor is facing you). Then the middle pin will have an analog voltage that is directly proportional (linear) to the temperature in °C. This can be easily seen in the output voltage vs temperature characteristic. Note that the analog output voltage is independent of the power supply.
To convert the voltage to temperature, simply use the basic formula:
Temp (°C) = (Vout – 0.5) * 100
So for example, if the voltage out is 1V that means that the temperature is (1 – 0.5) * 100 = 50 °C
Want to know how this formula is derived?
Before calculating the temperature reading, we subtract 0.5V from the output voltage because the TMP36 has a 500mV offset. This offset is what gives the sensor the ability to read negative temperatures.
Now to convert this voltage in to temperature, we simply multiply it by 100 because the TMP36 has a scaling factor of 10mV/°C. As easy as ABC.
Testing the TMP36 Sensor
Testing the TMP36 is pretty easy, just connect the left pin to 2.7-5.5V power supply (Two AA batteries work great) and the right pin to ground (assuming the flat side of the sensor is facing you). Now connect your multimeter in DC voltage mode to ground and the middle pin. At the room temperature (25°C), the voltage should be about 0.75V.
Try squeezing the plastic case of the sensor gently to see a rise in temperature.
Or try touching the sensor with an ice cube (in a plastic bag so your circuit doesn’t come into contact with water) and watch the temperature drop.
TMP36 Sensor Pinout
The TMP36 comes in three different form factors, but the most common type is the 3-pin TO-92 package, which looks just like a transistor. Let’s take a look at its pinout.
+Vs is the power supply for the sensor which can be anywhere between 2.7V to 5.5V.
Vout pin produces an analog voltage that is directly proportional (linear) to the temperature. It should be connected to an Analog (ADC) input.
GND is a ground pin.
Connecting the TMP36 Temperature Sensor to an Arduino
Hooking up the TMP36 to an Arduino is super simple. You only need to connect three pins: two for power and one for reading the sensor value.
The sensor can be powered from 3.3 or 5V output. The positive voltage connects to ‘+Vs’ and ground connects to ‘GND‘. The middle pin ‘Vout’ is the analog signal output from the sensor and connects to the A0 analog input of an Arduino.
Below is the hookup for the experiments with the TMP36:
To measure air temperature leave the sensor in the open air or attach it to an object you want to measure the temperature of, such as a hit sink.
Reading the Analog Temperature Data
As you can see in the wiring diagram above, the output of the TMP36 is connected to one of the analog inputs of the Arduino. The value of this analog input can be read with the analogRead()
function.
However, the analogRead()
function does not actually return the output voltage of the sensor. Instead it maps the input voltage between 0 and the ADC reference voltage (technically it is the operating voltage i.e. 5V or 3.3V unless you change it) to 10-bit integer values ​​ranging from 0 to 1023. To convert this value back to the sensor’s output voltage, use this formula:
Vout = (reading from ADC) * (5 / 1024)
This formula converts the number 0-1023 from the ADC into 0-5V
If you’re using a 3.3V Arduino, you’ll want to use this:
Vout = (reading from ADC) * (3.3 / 1024)
This formula converts the number 0-1023 from the ADC into 0-3.3V
Then, to convert volts into temperature, use this formula:
Temperature (°C) = (Vout – 0.5) * 100
Arduino Code – Simple Thermometer
The following sketch shows a quick way to read a TMP36 temperature sensor and can serve as the basis for more practical experiments and projects. It simply reads the value from the TMP36 using analog port A0 and prints the current temperature (in both °C and °F) on the serial monitor. Go ahead and upload it to your Arduino.
// Define the analog pin, the TMP36's Vout pin is connected to
#define sensorPin A0
void setup() {
// Begin serial communication at 9600 baud rate
Serial.begin(9600);
}
void loop() {
// Get the voltage reading from the TMP36
int reading = analogRead(sensorPin);
// Convert that reading into voltage
// Replace 5.0 with 3.3, if you are using a 3.3V Arduino
float voltage = reading * (5.0 / 1024.0);
// Convert the voltage into the temperature in Celsius
float temperatureC = (voltage - 0.5) * 100;
// Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C | ");
// Print the temperature in Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");
delay(1000); // wait a second between readings
}
You should see the following output in the serial monitor.
Code Explanation:
The sketch starts by defining the Arduino pin to which the sensor’s Vout pin is connected.
#define sensorPin A0
In the setup, we initialize the serial connection with the computer.
void setup() {
Serial.begin(9600);
}
In the loop, we first read in the analog signal from the TMP36 using the analogRead()
function.
int reading = analogRead(sensorPin);
Next, we will use the formulas we discussed earlier in the article to convert the analog reading into voltage and then into temperature.
float voltage = reading * (5.0 / 1024.0);
float temperatureC = (voltage - 0.5) * 100;
Next, the results are printed on the Serial Monitor.
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C | ");
The temperature value we get is in Celsius (°C). It is converted in to Fahrenheit (°F) using a simple formula and printed on the Serial Monitor.
T(°F) = T(°C) × 9/5 + 32
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");
Improving the Accuracy of the TMP36 Sensor
Because we didn’t configure the reference voltage (ARef) used for analog input (the default analog reference on 5V Arduino boards is 5 volts), the maximum resolution we get from the ADC is 5/1024 = 4.88 mV or 0.49°C.
For better results, using the 3.3v reference voltage as ARef instead of the 5V will be more precise and less noisy. With 3.3V as the reference voltage, we get a resolution of 3.3/1024 = 3.22 mV or 0.32°C.
To use the 3.3v pin as your analog reference, connect it to the AREF (Analog Reference) input like this.
Also you need to make some changes to the code. I have highlighted the lines you need to add/change in the code below:
// Define the analog pin, the TMP36's Vout pin is connected to
#define sensorPin A0
// Tie ARef to 3.3V
#define aref_voltage 3.3
void setup() {
// Begin serial communication at 9600 baud rate
Serial.begin(9600);
// If you want to set the aref to something other than 5v
analogReference(EXTERNAL);}
void loop() {
// Get the voltage reading from the TMP36
int reading = analogRead(sensorPin);
// Convert that reading into voltage
float voltage = reading * (aref_voltage / 1024.0);
// Convert the voltage into the temperature in Celsius
float temperatureC = (voltage - 0.5) * 100;
// Print the temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.print("C | ");
// Print the temperature in Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.print("\xC2\xB0"); // shows degree symbol
Serial.println("F");
delay(1000); // wait a second between readings
}
You should see the following output in the serial monitor.
You can see that the accuracy can be improved a bit, but for most projects, this will not be enough.
An alternative to the TMP36 is to use a digital temperature sensor like the DS18B20 which comes in the same package. Digital temperature sensors have better noise immunity which is useful when the sensor is placed at a distance or in an electrically noisy environment.
Arduino Project – Standalone Thermometer with TMP36 and an I2C LCD
Sometimes you come up with an idea where you want to display the temperature readings in real time and show an alert when the temperature is outside the specified range. Then you’ll probably need a 16×2 character LCD instead of a serial monitor.
In this example, we’ll hook the I2C LCD up to the Arduino along with the TMP36.
Connecting the I2C LCD is quite easy as you can see in the wiring diagram below. If you’re not familiar with 16×2 character I2C LCDs, consider reading (at least skimming) below tutorial.
The following diagram shows you how to wire everything.
The following sketch will print the temperature values on the 16×2 character I2C LCD. The code is similar to the first example, except that the values are printed on the I2C LCD.
// Include the LiquidCrystal_I2C library
#include <LiquidCrystal_I2C.h>
// Create a new instance of the LiquidCrystal_I2C class
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// Define a custom degree character
byte Degree[] = {
B00111,
B00101,
B00111,
B00000,
B00000,
B00000,
B00000,
B00000
};
// Define the analog pin, the TMP36's Vout pin is connected to
#define sensorPin A0
void setup() {
// Start the LCD and turn on the backlight
lcd.init();
lcd.backlight();
// Create a custom character
lcd.createChar(0, Degree);
}
void loop() {
// Get the voltage reading from the TMP36
int reading = analogRead(sensorPin);
// Convert that reading into voltage
// Replace 5.0 with 3.3, if you are using a 3.3V Arduino
float voltage = reading * (5.0 / 1024.0);
// Convert the voltage into the temperature in Celsius
float temperatureC = (voltage - 0.5) * 100;
// Print the temperature on the LCD;
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print(temperatureC, 1);
lcd.write(0); // print the custom degree character
lcd.print("C ");
// Print the temperature in Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
lcd.print(temperatureF, 1);
lcd.write(0); // print the custom degree character
lcd.print("F ");
delay(1000); // wait a second between readings
}
You should see the following output on the LCD: