HomeTutorials › Using A TMP36 Temperature Sensor With Arduino

Using A TMP36 Temperature Sensor With Arduino

The TMP36 temperature sensor is an easy way to measure temperature using an Arduino! The sensor can measure a fairly wide range of temperature (-50°C to 125°C), is fairly precise (0.1°C resolution), and is very low cost, making it a popular choice. In this tutorial we will go over the basics of hooking the TMP36 up and writing some basic code to read the analog input it is connected to.

A Few Considerations:
Before we jump into getting this TMP36 temperature sensor hooked up there are a few points to consider:

• This sensor is not weatherproof, so it will need to be shielded from direct exposure to the elements.

How It Works:
Unlike a thermistor, the TMP36 does not have a temperature sensitive resistor. Instead this sensor uses the property of diodes; as a diode changes temperature the voltage changes with it at a known rate. The sensor measures the small change and outputs an analog voltage between 0 and 1.75VDC based on it. To get the temperature we just need to measure the output voltage and a little bit of math!

The Parts Needed:

tmp36-2

This tutorial will be requiring a few common parts:

We have also used one of our full sized Arduino Mounting Plates in this tutorial. The mounting plate keeps your Arduino fixed in place, allowing for much more organized prototyping!

The Schematic

This handy little diagram shows how we will be connecting everything. Don’t worry if it looks a little overwhelming, we will be going through this step by step!

tutorial4_bb

Step 1 – The TMP36 Temperature Sensor

tmp36-3

Since this sensor is very simple and does not require any supporting components we will be directly connecting the sensor to the Arduino. Start by bending the legs on the TMP36 sensor so they will fit into the breadboard.

Step 2 – Powering The Sensor

tmp36-4

This sensor has a fairly wide input voltage, meaning it isn’t terribly picky about the voltage required (anywhere between 2.7VDC and 5.5VDC). We will use the Arduino’s 5VDC power for this tutorial. Start by connecting a jumper wire from the Arduino’s 5VDC pin and running it to the sensor’s “pin 1”. Next, run another jumper wire from one of the Arduino’s ground pins to the sensor’s “pin 3”

Step 3 – Connecting The Output

tmp36-5

We are going to be using one of the Arduino’s analog input pins for this tutorial. Run a jumper wire from the sensor’s “pin 2” (the middle pin) to an analog input. We will be using analog input 0 in this tutorial.

Step 4 – Double Check And Plug It In!

tmp36-6

Before we give the Arduino power it is always a good idea to go over all of the connections to make sure there are no wires in the wrong spot – sometimes that can make for a very expensive mistake!

Step 5 – Starting The Code


void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}

Now that we have finished with the hookup we need to start writing some code. We will be using the Arduino IDE, this is available from https://www.arduino.cc/en/Main/Software

We will start with the “BareMinimum” sketch found by clicking “File” and selecting Examples / Basic / BareMinimum. This sketch is a great starting point as it includes the Setup and Loop functions – we will write the rest!

Step 6 – Understanding How To Read The Sensor

This sensor is very easy to read – the sensor does all of the hard work so all we have to do is read the output. Since the output voltage is an analog voltage proportional to the temperature we can do some very basic math to take the voltage and turn it into a number that makes more sense.

Step 7 – Writing The Code

We are starting with the BareMinimum Sketch found in the IDE, it should look something like this:

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:  
}

So first we will need some variables to use:

int sensePin = A0;  //This is the Arduino Pin that will read the sensor output
int sensorInput;    //The variable we will use to store the sensor input
double temp;        //The variable we will use to store temperature in degrees. 

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:  
}

We plugged the sensor into analog input 0 so we will declare this as a variable – this will make it much easier to change in the future if we ever decide to change which pin the sensor is plugged into. We also need two variables for the data; 1 will store the initial input from the analog input, and the other will store the temperature once we have converted it to decimal degrees.

Arduino analog inputs do not need to be configured in the Setup loop, so we will just add some code for starting the serial connection we will use to output the data.

int sensePin = A0;  //This is the Arduino Pin that will read the sensor output
int sensorInput;    //The variable we will use to store the sensor input
double temp;        //The variable we will use to store temperature in degrees. 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Start the Serial Port at 9600 baud (default)

}
void loop() {
  // put your main code here, to run repeatedly:  
}

Ok now we can read the sensor, this will be done in the loop.

int sensePin = A0;  //This is the Arduino Pin that will read the sensor output
int sensorInput;    //The variable we will use to store the sensor input
double temp;        //The variable we will use to store temperature in degrees. 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Start the Serial Port at 9600 baud (default)

}
void loop() {
  // put your main code here, to run repeatedly: 
  sensorInput = analogRead(A0); //read the analog sensor and store it
}

Reading analog inputs is very easy with the Arduino – just that one line of code!

Now we will use a little math to convert that data to something a little more familiar:

int sensePin = A0;  //This is the Arduino Pin that will read the sensor output
int sensorInput;    //The variable we will use to store the sensor input
double temp;        //The variable we will use to store temperature in degrees. 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Start the Serial Port at 9600 baud (default)

}
void loop() {
  // put your main code here, to run repeatedly: 
  sensorInput = analogRead(A0);        //read the analog sensor and store it
  temp = (double)sensorInput / 1024;   //find percentage of input reading
  temp = temp * 5;                     //multiply by 5V to get voltage
  temp = temp - 0.5;                   //Subtract the offset 
  temp = temp * 100;                   //Convert to degrees 

}

The analog input gives us a value between 0 and 1023, 0 being no voltage and 1023 being 5V. To figure out what this voltage converts to in degrees Celsius we will need to first find the percentage of 5V on the input. This can be done by dividing the sensorInput by 1024.

As we know from the datasheet, the sensor will output 0 – 1.75V over a range of 175 degrees (-50° to 125°) so that means every 0.01V equals 1 degree. We will need to convert the percentage of input to voltage by multiplying by 5V.

Since we are not measuring 0° – 175° we will need to shift the output value so that the minimum reading of -50° equals a voltage reading of 0. We do this by subtracting 0.5 from the output voltage. Our new value now ranges from -0.5 to 1.25 (which looks remarkably similar to our temperature range!)

To convert from millivolts to degrees Celsius we will need to multiply by 100.

Now that we have degrees, let’s output it via serial so it can be seen from the Arduino Serial Monitor.

int sensePin = A0;  //This is the Arduino Pin that will read the sensor output
int sensorInput;    //The variable we will use to store the sensor input
double temp;        //The variable we will use to store temperature in degrees. 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Start the Serial Port at 9600 baud (default)

}
void loop() {
  // put your main code here, to run repeatedly: 
  sensorInput = analogRead(A0);    //read the analog sensor and store it
  temp = (double)sensorInput / 1024;       //find percentage of input reading
  temp = temp * 5;                 //multiply by 5V to get voltage
  temp = temp - 0.5;               //Subtract the offset 
  temp = temp * 100;               //Convert to degrees 

  Serial.print("Current Temperature: ");
  Serial.println(temp);
}

That’s it – we are all done.

Step 8 – Upload The Code And Test

code

Now that all of the code has been written it can be uploaded to your Arduino! Click “Upload” button in the top left corner of the Arduino IDE and it should upload without any issues. Next, click the “Serial Monitor” button in the top right corner (it looks like a magnifying glass). After a few seconds you should start to see a stream of data appear in the window – that is your temperature in degrees Celsius.

Have A Question?
If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!

Parts Used In This Tutorial:

12 comments

  1. karthik
  2. Tas
  3. James Hudson
  4. MikeH
  5. Sp.
  6. Lucas
  7. Anonymous
  8. Deepak Rajendra Waghmare
  9. Skelder

Leave a Reply to Skelder Cancel reply