Lesson 24. Connecting analog temperature sensors to Arduino (LM35, TMP35, TMP36, TMP37). The working project of the thermometer.

Arduino thermometer

Lesson on connecting integral temperature sensors with analog output to the Arduino controller. The working draft of the thermometer is given, it is told about the software processing of information from thermal sensors.

Previous lesson     List of lessons     Next lesson

With this publication, I begin a series of lessons about temperature measurement in the Arduino system. A total of 4 lessons are planned on various types of temperature sensors:

In each lesson I will tell:

  • briefly on the principle of operation and the parameters of the thermal sensors;
  • on the connection diagrams of temperature sensors to microcontrollers;
  • I will tell about software processing of information from thermal sensors;
  • I will give a schematic diagram of the thermometer based on the Arduino board and software for it.

Each lesson will consider the design of a thermometer based on an Arduino controller operating:

  • in standalone mode with the display of information on the LED display;
  • in connection with a computer, which allows not only to display the current temperature, but also to record the temperature change with the data output in graphical form.

 

Integrated temperature sensors with analog voltage output.

With all the variety of these devices, they have the following common qualities:

  • output voltage is linearly proportional to temperature;
  • the sensors have a calibrated scale factor for the dependence of the output voltage on temperature; they do not require additional calibration.

Simply speaking, to measure the temperature using sensors of this type, it is necessary to measure the output voltage and, using a scale factor, recalculate it to temperature.

There are many temperature sensors that can be classified in this category. I would highlight the following types of temperature sensors:

  • LM35;
  • TMP35;
  • TMP36;
  • TMP37.

These are the most common, fairly accurate, inexpensive devices. For technical information about them, see LM35 and TMP35, TMP36, TMP37.

 

Connection of thermal sensors to the microcontroller.

The most convenient way is to use sensors in the TO-92 package.

thermal sensor TO-92

The wiring diagram for devices in the TO-92 package looks like this.

connection diagram TMP36

 

The main parameters and differences of the sensors.

The principal differences between these sensors from each other is that:

  • TMP36 is the only one of the listed temperature sensors that can measure negative temperature.
  • The sensors have different temperature ranges.

We are talking about temperature sensors connected according to the above scheme. For example, there is a circuit LM35, which allows to measure negative temperatures. But it is more difficult to implement and requires additional power supply. It is better to use TMP36 for negative temperatures.

The main parameters of the temperature sensors LM35, TMP35, TMP36, TMP37 for this scheme I summarized in the table.

Type Temperature measurement range,
°C
Output voltage offset,

mV

Scale Factor

mV / ° C

Output voltage at +25 ° C,

mV

LM35, LM35A 0 … + 150 0 10 250
LM35C, LM35CA 0 … + 110 0 10 250
LM35D 0 … + 100 0 10 250
TMP35 + 10 … + 125 0 10 250
TMP36 - 40 … + 125 500 10 750
TMP37 + 5 … + 100 0 20 500

For all the temperature sensors, the output voltage can only be positive, but due to the offset TMP36 is able to measure a negative temperature. Zero voltage at its output corresponds to a temperature of -40 ° C. And at an output voltage of 0.5 V, the temperature will be 0 ° C. I consider the TMP36 to be the most convenient of the analog integrated temperature sensors and use them widely.

 

Arduino thermometer project for temperature sensors, LM35, TMP35, TMP36, and TMP37.

We will develop a thermometer that will:

  • In stand-alone mode, display the temperature value on a four-digit seven-segment LED.
  • To transfer the current value of the temperature to a computer. You can watch it using the Arduino IDE serial port monitor.
  • Using a special top-level program (I developed it):
    • display the measured temperature on a computer monitor.
    • record the temperature change and display it graphically.

 

Thermometer scheme based on Arduino UNO R3 board.

To the Arduino board you need to connect:

  • a four-digit seven-segment LED display in multiplexed mode;
  • TMP36 temperature sensor or similar.

I chose the LED display type GNQ-3641BUE-21. It is bright, optimal for this task size. We connected it to the Arduino board in lesson 20. In this lesson you can see the documentation on the display, the wiring diagrams. There is also a description of the seven-segment LED displays control library.

The schematic diagram of the thermometer based on the Arduino UNO R3 board looks like this.

schematic diagram of the thermometer

The LED display is connected to the controller in multiplexed mode (Lesson 19, Lesson 20).

The temperature sensor is connected to analog input A0. Capacitor C1 - blocking power supply of the sensor , R1 and C2 - the simplest analog filter. If the temperature sensor is installed near the microcontroller, then the filter can be excluded from the circuit.

TMP35, TMP36, TMP37 allow work on the load capacity of up to 10 nF, and LM35 - no more than 50 pF. Therefore, if the sensor is connected to the controller with a long line having significant capacitance, then the resistor R1 must be installed on the sensor side, and the capacitor C2 - on the controller side. The blocking capacitor C1 is always installed near the thermal sensor.

In any case, the digital signal filtering from the sensor will be implemented in the controller program.

To test, I assembled the device on a breadboard.

Arduino thermometer

 

The calculation of the temperature.

The principle is simple. To calculate the temperature of the LM35, TMP35, TMP37 sensors, you need:

  • Read the ADC code.
  • Calculate the output voltage of the sensor as Uout = N * Uref / 1024, where
    • Uout - voltage at the output of the temperature sensor;
    • N - the ADC code;
    • Uref - voltage of the reference voltage source (for our circuit 5 V);
  • 1024 - maximum number of ADC gradations (10 bits).
  • Divide the voltage at the sensor output by the scale factor.
  • For the TMP36 sensor, the offset voltage (0.5 V) must be subtracted before dividing by the scale factor.

Formulas for calculating the temperature for different sensors with a reference voltage of 5 V are as follows.

Sensor type Formula for calculating temperature T (° C),
with a reference voltage of 5 V,
from ADC code - N.
LM35, TMP35 T = ( N * 5 / 1024 ) / 0.01
TMP36 T = ( N * 5 / 1024 – 0.5 ) / 0.01
TMP37 T = ( N * 5 / 1024 ) / 0.02

If digital filtering is used, it is also necessary to consider the coefficient for it. You also need to understand that the formulas are presented in the form of easy to understand. In a real program, it is better to calculate the constant part of the formula in advance and use it as a coefficient. This is described in detail in lesson 13. There is also information on reading and digital filtering of the analog signal.

 

Arduino thermometer program.

The program should perform the following functions:

  • read ADC code values;
  • averaging them (digital filtering) to increase noise immunity;
  • calculate temperature from the ADC code;
  • display the temperature value to a four-digit LED display in the format:
    • sign;
    • tens;
    • units;
    • tenths of ° C.
  • transmit the temperature to the computer in text format once per second.

Development of the program is based on the usual principle:

  • implemented a timer interrupt with a period of 2 ms;
  • in it by a parallel process takes place:
    • LED display regeneration;
    • reading ADC codes and averaging their values;
    • software timers.
  • In the main asynchronous process occurs:
    • synchronization from software timer 1 sec;
    • temperature calculation;
    • transfer the temperature value to the computer.

If you have read the previous lessons, everything will be clear.

MsTimer2.h and Led4Digits.h libraries must be included. Libraries can be downloaded from lesson 10 and lesson 20. There is also a detailed description and examples there. Regarding the measurement of the voltage of the analog inputs, you can see lesson 13.

I'll just give you a sketch of the program.

// thermometer, sensors LM35, TMP35, TMP36, TMP37
#include <MsTimer2.h>
#include <Led4Digits.h>

#define MEASURE_PERIOD 500 // measurement time, * 2 ms
#define ADC_RESOLUTION 4.8828125 // resolution of ADC, mV (5000 mV / 1024)
#define OFFSET 500. // output voltage offset, mV (for TMP36)
#define SCALE_FACTOR 10. // scale factor, mV (for TMP36)

int timeCount; // measurement time counter
long sumA0; // variable to add ADC codes
long avarageTemp; // average temperature value (sum of ADC codes, average value * 500)
boolean flagTempReady; // temperature measurement ready
float temperature; // calculated temperature, ° C

// type of display 1; digit pins 5,4,3,2; segment pins 6,7,8,9,10,11,12,13
Led4Digits disp(1, 5,4,3,2, 6,7,8,9,10,11,12,13);

void setup() {
  MsTimer2::set(2, timerInterrupt); // set the interrupt period by timer 2 ms
  MsTimer2::start(); // enable timer interrupt
  Serial.begin(9600); // initialize the port, rate 9600
}

void loop() {

  if ( flagTempReady == true ) {
    flagTempReady= false;
    // data is ready

    // calculate temperature
    temperature = ( avarageTemp * ADC_RESOLUTION / 500. - OFFSET )       / SCALE_FACTOR;

    // temperature display
    if (temperature >= 0) {
      // temperature is positive
      disp.print((int)(temperature * 10.), 4, 1);
    }
    else {
      // temperature is negative
      disp.digit[3]= 0x40; // shows minus
      disp.print((int)(temperature * -1 * 10.), 3, 1);
    }
    disp.digit[1] |= 0x80; // lit the point of digit 2

    // transfer temperature to computer
    Serial.println(temperature);
  }
}

//-------------------------------------- interrupt handler 2 ms
void timerInterrupt() {
  disp.regen(); // LED regeneration

  // measure the average temperature
  timeCount++; // +1 averaging sample counter
  sumA0+= analogRead(A0); // summation of A0 channel ADC codes

  // check the number of averaging samples
  if ( timeCount >= MEASURE_PERIOD ) {
    timeCount= 0;
    avarageTemp= sumA0; // average overload
    sumA0= 0;
    flagTempReady= true; // sign of readiness result
  }
}

You can download the sketch from this link sketch_24_1.

Download, test. Start the serial port monitor and check data on the computer.

the data to the computer

The program is designed for TMP36 sensors, but it is easy to adapt to other types of sensors. To do this, it is enough to change the values of the scale factor and the offset specified at the beginning of the program by #define operators.

Sensor type Coefficient and Offset
LM35, TMP35 #define OFFSET  0.
#define SCALE_FACTOR 10.
TMP36 #define OFFSET  500.
#define SCALE_FACTOR 10.
TMP37 #define OFFSET  0.
#define SCALE_FACTOR 20.

 

Resolution and accuracy of the thermometer.

The resolution of the ADC in our circuit is 5 V / 1024 = 4.88 mV.

The thermometer resolution:

  • with a scale factor of 10 mV / ° C (sensors LM35, TMP35, TMP36) is less than 0.5 ° C;
  • with a scale factor of 20 mV / ° C (sensor TMP37) is less than 0.25 ° C.

Quite decent parameters.

As for the measurement error, it is somewhat worse. The measurement error of the sensors themselves is:

  • not more than 0.5 ° C for LM35;
  • not more than 1 ° C for TMP35, TMP36, TMP37.

 

The measurement error of Arduino ADC.

In our device, we used a reference voltage of 5 V, i.e. power supply voltage. In Arduino UNO R3 boards, a voltage of 5 V is formed using the NCP1117ST50 linear regulator. The stability of the output voltage of this chip is quite high - 1%.

Those. The total measurement error of the thermometer is no more than 2%.

It can be slightly increased by measuring the voltage of 5 V on the board and setting the ADC resolution parameter to not a 5 V, but a more accurate value. On my Board the voltage is equal of 5.01 V. It is necessary to correct:

#define ADC_RESOLUTION 4.892578 // resolution of the ADC, mV (5010 mV / 1024)

 

The use of an external reference voltage source for the Arduino.

But there is a radical way to improve both the accuracy of ADC measurement and resolution. This is an external reference voltage source application.

The most common source of stable voltage are chips LM431, TL431, etc (LM431.pdf).

Here is the wiring diagram for the LM431 as a 2.5 V reference source for the Arduino board.

wiring diagram for the LM431

In the program, you need to change the line that determines the resolution of the ADC:

#define ADC_RESOLUTION 2.44140625 // resolution of ADC, mV (2500 mV / 1024)

And in setup() connect an external reference voltage source:

analogReference (EXTERNAL); // external reference voltage

As a result, the resolution will decrease by 2 times, and the stability will improve by 10 times. But all the same, in order to increase accuracy, it is necessary to measure the real voltage LM431 with a voltmeter and correct it in the program.

Such a modification of the thermometer is absolutely necessary if the device is powered by an unstabilized power supply voltage close to 5 V, for example, from galvanic batteries or a battery. In this case, the voltage of the power source will vary widely and stabilization of the reference voltage source is indispensable.

 

High level program for the thermometer.

To watch the running lines of numbers in the monitor window of the Arduino IDE, quickly get bored.  It is more convenient to just see the temperature value. In addition, for practical use of the thermometer with a computer, the Arduino IDE software must be installed. Not all computers have it. Also, people are often interested in changing the temperature, the process of heating or cooling in dynamics. It may be necessary to record the change in temperature and display it graphically.

For this, I developed  a simple high-level program that:

  • displays the current temperature value;
  • records the temperature change with a resolution of 1 sec;
  • displays information about temperature changes in graphical form.

This program can be used both with the thermometer from this article, and for thermometers of the subsequent lessons with other types of sensors.

The program runs under the operating systems Windows 95, 98, XP, 7. I have not tried the others.

 

The application installation.

  • Download the archive file Thermometer.zip.
  • Unzip it to your working folder. You can leave the folder from the archive Thermometer.

The application consists of two files:

  • Thermometer.exe - executable file;
  • Conf.txt - configuration file.

Install the program is not necessary, just run the file Thermometer.exe.

 

Connecting the thermometer to a computer.

Data exchange between the computer and the controller is carried out through the COM port. The port can be real or virtual.

The most convenient way is to use the virtual port that the Arduino board driver creates. The port appears when the board is connected to the computer. You don't need to run the Arduino IDE. The port number can be viewed in the Windows Device Manager.

Windows Device Manager

You can connect the computer via any USB-UART bridge. I am using PL2303 USB UART Board modules.

If the computer has a regular COM port (RS232 interface), then no drivers need to be installed. To connect the controller in this case, you must use the RS232 level converter - TTL, ADM232, SP232, MAX232 chips and the like.

Connection options are many. The main thing is that the COM port, virtual or real, be formed on the computer.

 

The first launch of the program.

Before you run the program, the virtual COM port must already be created on the computer. And since the port is created when connecting to the Arduino board connector, this means that you must first connect the board to the computer.

Then run the program Thermometer.exe. Some COM port is set in the program configuration file. The program will try to open it at startup. If it fails, it will display a message with the wrong port number.

could not open the port

Click OK and the program window will open. Instead of temperature there will be dashes. No data.

wrong data

Select the port selection mode from the menu (above). A selection window will open.

Port selection

Set the port number for your board. Each port has its status.  Of course, it is necessary to choose from the ports labeled “free”.

Close the window. The selected COM port is saved in the configuration file, and will always be called when the program starts. It is not necessary to set the port every time you start the program.

If the board is on, the program is loaded, everything is working correctly, then once a second the circle-led should flash before the temperature value. It blinks when new data arrives.

Arduino thermometer

 

Recorder.

The program has a recorder that allows you to observe the dynamics of temperature changes. The recorder is turned on automatically when the program starts. It records temperature values with a time resolution of 1 second.

To view the results of the recording, you need to click the “Recorder” menu tab.

Recorder data

I heated the sensor with a soldering iron.

You can enlarge a fragment by selecting a rectangular area with the right mouse button pressed. The area must be chosen from left to right, top to bottom.

Recorder data

Selecting left-to-right or bottom-up area with the mouse,  will return the display of all graphic information. It's simple.

This program will be used in the next three lessons for other types of temperature sensor projects.

In the next lesson we will measure the temperature using silicon sensors KTY81 series.

Previous lesson     List of lessons     Next lesson

Leave a Reply

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