15 – TMP36 Temperature sensor



    • Measure temperature using a float data type.
    • Build a temperature alarm.
    • Convert temperature from Celsius to Fahrenheit degrees


Bill of materials

Arduino Uno Arduino Uno or equivallent.

We can use any other Arduino board in this chapter.


Jumper wires

A solderless Breadboard plus some jumper wires.

330 Ohm Resistor

A LED diode and a 330Ω resistor
TMP36 A TMP36 or an LM35DZ temperature sensor,.

  • Check that this reference number is silverscreened, because transistors are quite similar.



A temperature sensor is simply an integrated circuit that returns a voltage value proportional to the temperature to which it is subjected. In this chapter we will use any of these widespread, cheap and easy to find business models: TMP36 or LM35DZ.

Let’s see how to use them. First, you have to look for the manufacturer’s datasheet (search TMP36 or LM35DZ datasheet in internet). Although at the beginning it is not easy to get used to read such documents, datasheets are the place to go when you want to know exactly how an integrated circuit operates .

The summary could be more or less like this:

    • Both measure temperature in Celsius degrees.
    • TMP36 operates between -50º C and 125ºC.
    • LM35DZ operates between 0º C and 100ºC.
    • It is not particularly accurate, as it has ±1 °C of accuracy, but it is good enough for simple projects and is very cheap.
    • Their encapsulation is similar to transistors and they have also three leads, so be very careful not to get confused. Try to read the silkscreened letters on the flat side (if you can, because they are usually so small that sometimes can’t be read even using a magnifying glass). 

The central pin provides the output signal, but to distinguish between the GND and 5V pin, the encapsulation has a flat face and a curved side. Putting the flat side facing you (so that you can read the model) with the leads down, the pin on the left is 5V and, of course, the other is GND.

  • If you connect voltage the other way round, you will have time to react and change it, but be very careful because it will heat up more than enough to make you a painful burn.
  • If you see that it is hot, do not try to remove it with your fingers, just power off Arduino and give it some time to cool down.



We will assemble a small prototype that reads the temperature of a sensor, print the value in the Serial monitor console and turn on a diode when the temperature exceeds a certain threshold. Here we have the circuit schematic diagram:

Chapter 15, Fritzing schematic

 And the circuit wiring diagram is very simple also:

Chapter 15, wiring diagram



The manufacturer of the TMP36 and LM35DZ tells us that the output voltage increases 10 mV (millivolts) for each degree of temperature and we also know that the maximum value that Arduino can measure via the analog pins is 1,024 when the input voltage is 5V (and 0 when the input voltage is 0V). Therefore for a given reading, the value measured in volts will be:

Chapter 15, formula 1


Since each volt is equivalent to 100 ºC (1 V / 10mV = 100), the temperature in Celsius degrees is obtained by multiplying the result of the previous formula by 100.

But to make things a little bit complicated, the manufacturer of the TMP36 tells us that a voltage value of 0V is not equal to 0ºC but -50 °C (so you can read values below zero), so we have to subtract 50 from the total. The LM35DZ starts measuring from 0 ºC instead, so we don’t have to subtract anything and it is easier to operate.

Summarizing, to calculate the temperature in °C from the Arduino reading using a TMP36 we must use the following formula:

Chapter 15, formula 2


Remember that the LM35DZ does not begin at -50 °C but at 0 °C, so it is not necessary to subtract the 50 degrees of adjustment. You can find many sensors similar to these with different measuring ranges, both above and below, but you can use this same method to calibrate the measurement.

So we know now how to calculate the temperature, but as we see in the formula that we use a division, we should use our first float variable because the result will have decimals.

  • I know that the natural tendency is to use int for everything, but usually as soon as a division appears you should use a float until you have very clear because you do use any other data type.

Every time we take a sample from our analog input we will calculate temperature and if it exceeds the defined threshold, we will fire the alarm. Otherwise we will switch it off.

As temperature varies slowly, we will use a delay to obtain only one measure per second.



First we are going to define a couple of variables:

     int Sensor = 0 ;
     int Threshold = 25 ;

The Sensor variable will be used to store the value read from the analog pin A0, which is connected to the temperature sensor, and the Threshold variable value will be used to fire the temperature alarm.

  • We will have to adjust the threshold value according to the environment temperature where we assemble the prototype.
  • In principle we can use a threshold value of 25 ºC, for if you want to raise temperature you may do it simply by blowing or by holding the sensor between the fingers. Another system to raise temperature fast is to put the sensor next to the cooling output of a laptop.

Applying the previous calculations we can write the following sketch:

void loop ()
    int reading = analogRead(Sensor);
    float voltage = 5.0 /1024 * reading ; // Pay attention here
    // If you used a LM35DZ the formula would be
    //float temp = voltage * 100 ;
    float temp = voltage * 100 -50 ; 
    Serial.println(temp) ; 

Important: In the line where we calculate voltage we have written 5.0 and not 5. This is to prevent C++ (which is a bit lazy) from making a division between integers and despising decimals.

  • Test the sketch writing 5 instead of 5.0 and you will see that temperature will always be -50 ºC. Why?
  • Simply because the whole part of 5/1024 is 0 and that’s it. So, although we later multiply it by the reading variable, it will keep on being 0 and for our sketch, a temperature of -50 °C corresponds to a voltage value of 0V.
  • Until you don’t fully understand how does C++ handle operations walk on eggshells and check them if you don’t want surprises. A part of this chapter is to provoke such situations with float variables.

And it only remains us to check if temperature reaches the threshold and switches the LED on or off

Sketch 15.1
int Sensor = 0; 
nt threshold = 25;
void setup()

void loop()
    int reading= analogRead(Sensor);
    float voltage = 5.0 /1024 * reading; // Pay attention here
    // If you use the LM35DZ you will have to uncomment the following line and comment the previous one
    // float temp = voltage * 100  
    float temp = voltage * 100 -50; 
    if (temp >= threshold ) 
        digitalWrite(11, HIGH); 
    else digitalWrite(11,LOW); 

If you had a small 5V fan, which usually can be found in computer stores (or if you disassemble an old computer), it would be easy to change the LED for the fan and direct it towards the sensor.

Setting an upper and a lower threshold we can write a program to turn on or off the fan when the temperature is out of the set margins. You would have achieved, in a very simple way, a temperature control system with a sensor and a fan.



In Europe we are quite used to the Celsius scale since we are children, but in America and UK it is common to measure temperature in Fahrenheit degrees. So let’s modify our program to measure in °F.

To convert temperature from Celsius to Fahrenheit degrees we must use the following formula:


I suggest you to convert the previous program so that it measures temperature in Fahrenheit degrees instead of Celsius (or better yet, both on the same line).




  • We have seen what temperature sensors are and how a basic temperature sensor, as the TMP35, works. There are many out there, but they are usually like this.
  • We have also introduced the float data type and seen how to force operations with it and, what is more important, to prevent possible tricks .
  • We have built a circuit to measure temperature that includes an alarm.






No Comments

Give a Reply