# Learn to code

Arduino Home

### Measuring Temperature with TMP36 Sensor

In this tutorial, you will learn how to measure temperature and how to send that information to the Serial Monitor.

The TMP36 series are precision integrated-circuit temperature devices with an output voltage linearly proportional to the Centigrade temperature. TMP36 is three terminal linear temperature sensor. It can measure temperature from -40 degree Celsius to +125 degree Celsius. The voltage output of the TMP36 increases 10mV per degree Celsius rise in temperature. TMP36 can be operated from a 5V supply and the stand by current is less than 60uA.

#### How to measure Temperature

The TMP36 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 will convert the analogRead value into voltage and substract the offset to be able to get the temperature.

You can use power supply anaywheren between 3.3V and 5V. In the example below we will use a 5V supply.

If you’re using a 5V Arduino, and connecting the sensor directly into an Analog pin, you can use these formulas to turn the 10-bit analog reading into a temperature:

Voltage at pin = (reading from sensor) * 5 / 1024

This formula converts the number 0-1024 from the Sensor into 0-5 Volts.

If you’re using a 3.3V Arduino, you’ll want to use this:

Voltage at pin = (reading from sensor) * 3.3 / 1024

This formula converts the number 0-1024 from the sensor into 0-5V.

Then, to convert volts into degrees we will need the following formule:

Temperature = [(analog voltage in V) – 0.5] * 100

#### Parts you will need ##### Arduino Uno  ##### Jumper Wires ##### TMP36 Sensor BreadBoard Layout –  The TMP36 Temperature sensor is quit easy to hook up. But beware, the middle pin is goes to the analogpin. The side pins go either to the GND or 5V / 3.3V supply. Look closely at the your sensor that you do not mix up the GND and 5V / 3.3V pin.

#### The Code

``````/*
// TMP36 sensor sketch

int sensorPin = 0;

void setup() {
Serial.begin(9600); // Start Serial Communication
}

void loop() {
// read the analog signal from the sensor

// convert the reading into voltage
float voltage = reading * 5.0;
voltage /= 1024.0;

// print the reading to the Serial Monitor
Serial.print(voltage); Serial.println(" volts");

// Convert the 10 mV per defree with an offset of 500mV to degrees
float temperatureC = (voltage - 0.5) * 100;

// Print the Temperature
Serial.print(temperatureC); Serial.println(" degrees C");

// Convert the temperature into Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

// print Fahrenheit to the Serial Monitor
Serial.print(temperatureF); Serial.println(" degrees F");

delay(1000); // delay for 1 second.
}
``````

&nbsp;

#### Code Explanation

##### Declare variables and start Serial Communication

To use the TMP36 sensor you will need to define which analog pin is used.

``````int sensorPin = 0;

void setup() {
Serial.begin(9600); // Start Serial Communication
}
``````

As explained above the sensor is sending millivolts to the Arduino. Before you transform these millivolts to degrees we need to read the sensor. This is done by analogRead and the value is printed to the Serial Monitor. Remeber, the value that is printed is the received voltage of the Sensor.

```// convert the reading into voltage
float voltage = reading * 5.0;
voltage /= 1024.0;

// print the reading to the Serial Monitor
Serial.print(voltage); Serial.println(" volts");
```
##### Convert into degrees

As explained above, we will are going to use a formula that converts the voltage into degrees.

```/ Convert the 10 mV per defree with an offset of 500mV to degrees
float temperatureC = (voltage - 0.5) * 100;

// Print the Temperature
Serial.print(temperatureC); Serial.println(" degrees C");
```
##### Convert infor Fahrenheit

We also want to show the Fahrenheit.

``` // print Fahrenheit to the Serial Monitor
Serial.print(temperatureF); Serial.println(" degrees F");

delay(1000); // delay for 1 second.
```