Comprehensive Guide to Using Arduino Temperature Sensors

Comprehensive Guide to Using Arduino Temperature Sensors

Temperature is a physical quantity that we often encounter and can intuitively feel, such as needing to add clothing when the weather gets cooler or blowing on food that is too hot. It is also necessary to measure temperature accurately, for example, the normal body temperature of humans is 37.5℃, and the boiling point of pure water at one atmosphere pressure is 100℃. We need to conduct experiments to discover the science behind these measurements. Below, we will detail several commonly used temperature sensors and how to use Arduino to measure temperature, including thermistors, LM35, DS18B20, DHT11, and thermocouples.

1. Thermistor

1.1 Introduction to Thermistors

A thermistor is a semiconductor sensor whose resistance value changes with temperature. Its typical characteristics are that the resistance value is very sensitive to temperature and exhibits different resistance values at different temperatures, allowing us to infer the environmental temperature based on the resistance value. Thermistors have high sensitivity, small size, low thermal capacity, fast response speed, and low cost. According to different temperature coefficients, thermistors can be divided into positive temperature coefficient (PTC) thermistors, negative temperature coefficient (NTC) thermistors, and critical negative temperature coefficient (CTR) thermistors. PTC thermistors exhibit increasing resistance with rising temperature; NTC thermistors exhibit decreasing resistance with rising temperature; CTR thermistors exhibit a negative resistance mutation characteristic, where the resistance value sharply decreases with increasing temperature at a certain temperature, having a large negative temperature coefficient. Due to their different characteristics, the applications of thermistors also vary. PTC is generally used as heating elements and overheat protection; NTC is generally used for temperature measurement and temperature compensation; CTR is generally used for temperature control alarms. The temperature measurement range of NTC is -60 to +300℃, with nominal resistance typically between 1Ω and 100MΩ. Using a combination of precision resistors and thermistors can expand the linear measurement range of temperature. Figure 1 shows a physical diagram of NTC, where NTC 10D-9 and NTC 5D-7 are shown. NTC represents a negative temperature coefficient thermistor, with 10D-9 and 5D-7 representing its model, where 10D-9 indicates a resistance of 10 ohms at room temperature (25 degrees Celsius) and a diameter of 9 millimeters, and 5D-7 indicates a resistance of 5 ohms at room temperature (25 degrees Celsius) and a diameter of 7 millimeters. In addition to the shape shown in Figure 1, thermistor probes can be made in bead, rod, sheet, and film shapes, with packaging shells made of glass, nickel, and stainless steel pipe structures, as shown in Figure 2.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 1 Physical Diagram of NTC

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 2 Various Forms of NTC

1.2 Using NTC

The measured temperature of NTC and its resistance value have a known nonlinear relationship, so measuring the resistance value of NTC can also calculate the measured temperature value. The relationship between the resistance value and temperature of NTC is as follows:

Rt = R x e^[B x (1/T1-1/T2)]

Where Rt is the resistance value of the thermistor at temperature T1; R is the nominal resistance of the thermistor at temperature T2; B is an important parameter of the thermistor; T1 and T2 refer to Kelvin degrees, where Kelvin = 273.15 (absolute temperature) Celsius.

Calculating the relationship between the temperature value and resistance value of the thermistor yields:

T1=1/(ln(Rt/R) /B 1/T2 )

Resistance value measurement is generally done by using a known resistance in series and applying a known voltage, measuring the voltage drop across the known resistance to calculate the resistance value of the measured resistance, as shown in Figure 3. The applied excitation voltage is Eb, the resistance value of the thermistor is Rt, and the series resistance value is Rs, then the voltage drop across the series resistance is:

Eout = Eb x Rs/(Rt Rs)

In addition to the series measurement method, there is also the Wheatstone bridge measurement method, as shown in Figure 4. Let the excitation voltage of the bridge be Eb, the resistance value of the thermistor be Rt, and the bridge resistance values be R1, R2, and R3, then the output voltage of the bridge is:

out = Eb x R3/(Rt R3) – Eb x R2/(R1 R2) = Eb x [R3/(Rt R3) – R2/(R1 R2)]

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 3 Series Measurement Method

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 4 Bridge Measurement Method

1.3 Example of Use

(1) Hardware Connection

Here, we use the series measurement method to measure the thermistor in the experiment, with the hardware connection diagram shown in Figure 5. The thermistor is NTC 10D-9, and the series resistance is 100Ω.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 5 NTC Measurement Hardware Connection Diagram

(2) Program Design

The main idea of the program design: The Arduino Uno controller measures the voltage value across the series resistance through the analog input port, then calculates the resistance value of the thermistor using the principle of equal current, and finally uses the formula to calculate the temperature value.

#include//Include Math Library
void setup(){
  Serial.begin(9600);      //Set baud rate to 9600
}
void loop(){
  double Digital_Value=analogRead(0);   //Read the voltage value across the series resistance (digital)
  double Voltage_Value=(Digital_Value/1023)*5.00;//Convert to analog voltage value
  double Rt_Value=(3.3-Voltage_Value)/Voltage_Value*100;  //Calculate the resistance value of the thermistor
  //Calculate the perceived temperature and send it
Serial.println( 1/(log(Rt_Value/10)/3000   1/( 25   273.15)) - 273.15,2);
  delay(1000);   //Refresh once every second
}

2. LM35

LM35 is an analog temperature sensor produced by National Semiconductor (NS) in the United States, whose output voltage is linearly proportional to the Celsius temperature, outputting 0V at 0℃, and increasing the output voltage by 10mV for every 1℃ increase in temperature. The temperature measurement range is -55 to 150℃, with an accuracy of 0.75℃, and the accuracy at room temperature can reach 0.25℃. The typical pin arrangement for the commonly used TO-92 package is shown in Figure 6, and the typical application circuit for the temperature range of 2℃ to 150℃ is shown in Figure 7.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 6 Pin Arrangement of TO-92 Package

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 7 Typical Circuit Diagram for 2℃ to 150℃

2.3 Example of Use

(1) Hardware Connection

Connect the Vs and GND of the LM35 analog temperature sensor to the 5V and GND of the Arduino Uno controller, respectively, to provide power to the LM35. The Vout pin of LM35 is connected to the analog input port A0 of the Arduino Uno controller, as shown in Figure 8.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 8 LM35 Measurement Hardware Connection Diagram

(2) Program Design

The main idea of the program design: The Arduino Uno controller measures the output voltage of LM35 through the analog input port and then calculates the temperature value using the proportional coefficient of 10mV/℃. At 100℃, the output voltage of LM35 is 1000mV, which is within the internal reference voltage range of the Arduino Uno controller, so the internal reference voltage of 1.1V is used.

int Digital_Value=0;
float temp_Value=0;
void setup(){
  Serial.begin(9600);      //Set baud rate to 9600
  //Since the temperature range is 0~100℃, the output voltage is 0~1V, using internal 1.1V reference voltage
analogReference(INTERNAL);
}
void loop(){
   Digital_Value=analogRead(A0);   //Read voltage value (digital)
   temp_Value=(float)Digital_Value/1023*110.00;//Convert to Celsius temperature
   Serial.print("Temperature for LM35 is: ");
   Serial.println(temp_Value,2);  //Send temperature data
   delay(1000);   //Refresh once every second
}

(3) Experimental Demonstration

The actual experimental hardware connection diagram is shown in Figure 9, and the temperature data received by the serial port is shown in Figure 10.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 9 Experimental Hardware Connection Diagram

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 10 Temperature Data Received by Serial Port

3. DS18B20

3.1 Introduction to DS18B20

DS18B20 is a digital single-bus smart temperature sensor from Dallas Semiconductor in the United States. Compared to traditional thermistors, it can directly read the measured temperature and can achieve 9 to 12 bit digital readings through simple programming as required. Reading or writing information from DS18B20 only requires one wire (single bus) for read/write, and the bus itself can also power the connected devices without needing an additional power source.

The performance characteristics of DS18B20 are as follows:

(1) Single-wire interface for bidirectional communication;

(2) Supply voltage range: 3.0V to 5.5V, can be powered by the data line;

(3) Temperature measurement range: -55 to 125℃, inherent measurement resolution of 0.5℃;

(4) Programmable to achieve 9 to 12 bit digital readings;

(5) Supports multi-point networking, allowing multiple DS18B20s to be connected in parallel on a single bus for multi-point temperature measurement.

The appearance and pin arrangement of DS18B20 are shown in Figure 11. The pin definitions of DS18B20 are: (1) DQ is the digital signal input/output pin; (2) GND is the power ground; (3) VDD is the external power supply input (connected to ground in parasitic power wiring).

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 11 DS18B20 Package Diagram

3.2 Programming and Library Usage for DS18B20

To operate DS18B20 with Arduino, you need the OneWire and Dallas Temperature Control libraries. Download links are: OneWire and Dallas Temperature Control. The Dallas Temperature Control function library is developed based on the OneWire function library, making it easier to use. Below, we will explain the main functions and their usage.

(1) void begin(void): Initializes with no input parameters and no return parameters.

(2) getDeviceCount(void): Gets the total number of devices connected to the single bus, with no input parameters and returns the number of devices.

(3) validAddress(uint8_t*): Validates whether a specified address device exists, with the input parameter being the device address and returning a boolean value.

(4) getAddress(uint8_t*, const uint8_t): Validates whether the device address matches the index value, with input parameters being the device address and index value, returning a boolean value.

(5) getResolution(uint8_t*): Gets the precision of the specified device, with input parameters being the device address and returning the precision bits.

(6) setResolution(uint8_t*, uint8_t): Sets the precision of the device, with input parameters being the device address and precision bits, with no return parameters. Precision bits can be 9, 10, 11, or 12.

(7) requestTemperatures(void): Sends a temperature conversion request to all devices on the single bus, with no input parameters and no return parameters.

(8) requestTemperaturesByAddress(uint8_t*): Sends a temperature conversion request to the device at the specified address on the single bus, with input parameters being the device address and no return parameters.

(9) requestTemperaturesByIndex(uint8_t): Sends a temperature conversion request to the device at the specified index on the single bus, with input parameters being the device index and no return parameters.

(10) getTempC(uint8_t*): Gets the Celsius temperature through the device address, with input parameters being the device address and returning the Celsius temperature.

(11) getTempF(uint8_t*): Gets the Fahrenheit temperature through the device address, with input parameters being the device address and returning the Fahrenheit temperature.

(12) getTempCByIndex(uint8_t): Gets the Celsius temperature by index value, with input parameters being the device index and returning the Celsius temperature.

(13) getTempFByIndex(uint8_t): Gets the Fahrenheit temperature by device index, with input parameters being the device index and returning the Fahrenheit temperature.

3.3 Example of Use

Since multiple DS18B20s can be connected on a single bus without occupying additional IO ports of the Arduino controller, it is easy to achieve multi-point temperature measurement. Below are two examples demonstrating the use of DS18B20 with Arduino.

3.3.1 Single Temperature Measurement

(1) Hardware Connection

Connect the VCC and GND of the DS18B20 temperature sensor to the 5V and GND of the Arduino Uno controller, respectively, to provide power to the DS18B20. The DQ pin of DS18B20 is connected to the digital pin D2 of the Arduino Uno controller, and a 4.7kΩ pull-up resistor is connected in parallel, as shown in Figure 12.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 12 Single Temperature Measurement Hardware Connection Diagram

(2) Program Design

The main idea of the program design: The Arduino Uno controller uses the DallasTemperature function library to start the single bus, send a request to measure temperature, read the temperature of sensor 0, and finally send it through the serial port.

#include#include#define ONE_WIRE_BUS 2    //Define the port for the single bus
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup(void)
{
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();    //Start the single bus
}

void loop(void)
{
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures();    //Send temperature measurement request command
  Serial.println("DONE");

  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.print(sensors.getTempCByIndex(0));    //Get temperature data from sensor 0 and send
  Serial.println("℃");
  delay(1000);   //Refresh once every second
}

3.3.2 Multi-channel Temperature Measurement

(1) Hardware Connection

Connect the VCC and GND of the two DS18B20 temperature sensors to the 5V and GND of the Arduino Uno controller, respectively, to provide power to both DS18B20s. The DQ pins of both DS18B20s are connected to the digital pin D2 of the Arduino Uno controller, and a 4.7kΩ pull-up resistor is connected in parallel, as shown in Figure 13.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 13 Multi-channel Temperature Measurement Hardware Connection Diagram

(2) Program Design

The main idea of the program design: The Arduino Uno controller uses the DallasTemperature function library to start the single bus, send a temperature measurement request, read the temperature of sensor 0 and send it through the serial port, then read the temperature of sensor 1 and send it through the serial port.

#include#include#define ONE_WIRE_BUS 2    //Define the port for the single bus
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup(void)
{
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();   //Start the single bus
}

void loop(void)
{
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures();   //Send temperature measurement request command
  Serial.println("DONE");
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0));  //Get temperature data from sensor 0 and send
Serial.print("Temperature for the device 2 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(1));  //Get temperature data from sensor 1 and send
}

3.3.2 Experimental Demonstration

The actual single-channel experimental hardware connection diagram is shown in Figure 14, and the temperature data received by the serial port in the single-channel and dual-channel experiments is shown in Figures 15 and 16 respectively.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 14 Single-channel Experimental Hardware Connection Diagram

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 15 Single-channel Temperature Data Received by Serial Port

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 16 Dual-channel Temperature Data Received by Serial Port

4. DHT11

4.1 Introduction to DHT11

DHT11 is a digital signal output temperature and humidity composite sensor with calibrated coefficients. It uses dedicated digital module acquisition technology and temperature and humidity sensing technology, with extremely high reliability and excellent long-term stability. It contains a resistive humidity sensing element and an NTC temperature sensing element. DHT11 sensors are laboratory calibrated, and the calibration coefficients are stored in OTP memory in the form of programs. The sensor internally calls these calibration coefficients during signal detection processing, using a single-wire serial interface to simplify system integration. With ultra-small size and extremely low power consumption, the signal transmission distance can reach over 20 meters. The physical diagram of the DHT11 digital temperature and humidity sensor is shown in Figure 17.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 17 DHT11 Temperature and Humidity Sensor

The pin description of DHT11 is shown in Table 1, with a supply voltage of 3.3 to 5V, measurement range of humidity 20 to 90%RH, temperature 0 to 50℃, measurement accuracy of humidity ±5%RH, temperature ±2℃, and measurement resolution of humidity 1%RH and temperature 1℃.

4.2 Programming and Library Usage for DHT11

The Arduino library file for DHT11 can be downloaded from: DHT11 Library. The DHT11 library has the following functions: dht.setup(int Pin), dht.getHumidity(), dht.getTemperature().

dht.setup(int Pin): Sets the pin number for the DHT11 bus connection, with the input parameter being the connected pin number and no return parameters.

dht.getHumidity(): Gets the humidity value from DHT11, with no input parameters and returning the humidity value as a double type.

dht.getTemperature(): Gets the temperature value from DHT11, with no input parameters and returning the temperature value as a double type.

4.3 Example of Use

Below, we will implement temperature and humidity measurement using the DHT11 module and output the data through the serial port. (1) Hardware Connection: Connect the VCC and GND of the DHT11 temperature and humidity sensor to the 5V and GND of the Arduino Uno controller, respectively, to provide power to the DHT11. The DOUT pin of the DHT11 module is connected to the digital pin D2 of the Arduino Uno controller, and a 5kΩ pull-up resistor is connected in parallel. The NC pin of the DHT11 module is also connected to GND, as shown in Figure 18.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 18 DHT11 Temperature and Humidity Measurement Hardware Connection Diagram

(2) Program Design: The main idea of the program design: The Arduino Uno controller uses the DHT11 function library to obtain humidity and temperature data and sends it through the serial port.

#include "DHT.h"

DHT dht;

void setup()
{
  Serial.begin(9600);
  dht.setup(2); // data pin 2
  delay(1000);
}

void loop()
{
  float temperature = dht.getTemperature();
  float humidity = dht.getHumidity();

  Serial.print("temperature is ");
  Serial.print(temperature, 1);
  Serial.println(" C");
  Serial.print("humidity is ");
  Serial.print(humidity, 1);
  Serial.println("%");
  delay(3000);
}

(3) Experimental Demonstration: The actual experimental hardware connection diagram is shown in Figure 19, and the temperature and humidity data received by the serial port is shown in Figure 20.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 19 Experimental Hardware Connection Diagram

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 20 Temperature and Humidity Data Received by Serial Port

5. Thermocouple

5.1 Introduction to Thermocouples and MAX6675

When two different materials, conductor A and conductor B, are welded together, they form a closed loop. When there is a temperature difference between the two connection points (1 and 2), an electromotive force is generated between them, thus forming a loop current in the circuit. This phenomenon is called the thermoelectric effect, and this electromotive force is called the thermoelectric potential. The principle diagram of the thermoelectric effect is shown in Figure 21.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 21 Principle Diagram of the Thermoelectric Effect

A thermocouple uses the thermoelectric principle for temperature measurement, where the end used to measure the temperature of the medium is called the working end (also known as the measuring end), and the other end is called the cold end (also known as the compensation end). It is essentially an energy converter that converts thermal energy into electrical energy, using the generated thermoelectric potential to measure temperature. The commonly used K-type thermocouple is shown in Figure 22 and can directly measure the temperature of various liquids, steam, gases, and solid surfaces in the range of 0℃ to 1300℃. It has advantages such as good linearity, large thermoelectric potential, high sensitivity, good stability and uniformity, strong oxidation resistance, and low cost.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 22 Physical Diagram of K-type Thermocouple

According to the temperature measurement principle of thermocouples, the output thermoelectric potential of K-type thermocouples is related not only to the temperature at the measuring end but also to the temperature at the cold end, requiring a temperature compensation circuit (as shown in Figure 23 for compensation schematic). Additionally, the voltage of the thermocouple is nonlinear with respect to temperature, and the MAX6675 module can amplify the signal from the K-type thermocouple, perform cold end compensation, and nonlinear correction. The MAX6675 features a simple 3-wire serial SPI interface, converting temperature signals into 12-bit digital values with a temperature resolution of 0.25℃; it also includes a thermocouple disconnection detection circuit. The cold end compensation temperature range is -20℃ to 80℃, and it can measure temperatures from 0℃ to 1023.75℃, which basically meets the needs of industrial temperature measurement.

5.2 Programming and Library Usage for MAX6675

The Arduino library file for MAX6675 can be downloaded from: MAX6675 Library. The MAX6675 library has the following functions: getCelsius(), getFahrenheit(), getKelvin(), and setOffset(int offset).

getCelsius(): Gets the Celsius temperature, with no input parameters and returning the Celsius temperature as a float type.

getFahrenheit(): Gets the Fahrenheit temperature, with no input parameters and returning the Fahrenheit temperature as a float type.

getKelvin(): Gets the Kelvin temperature, with no input parameters and returning the Kelvin temperature as a float type.

setOffset(int offset): Sets the temperature offset, with the input parameter being the offset value as an int type, with a minimum unit of 0.25℃ and no return value.

5.3 Example of Use

Here we will implement high-temperature measurement using the K-type thermocouple with the MAX6675 module and output the data through the serial port.

(1) Hardware Connection

Connect the VCC and GND of the MAX6675 module to the 5V and GND of the Arduino Uno controller, respectively, to provide power to the MAX6675. The signal pins SO, CS, and CSK of the MAX6675 module are connected to digital pins 5, 6, and 7, and the positive and negative terminals of the K-type thermocouple are connected to the T and T- of the MAX6675 module, as shown in Figure 24.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 24 Hardware Connection Diagram for Thermocouple Measurement

(2) Program Design

The main idea of the program design: The Arduino Uno controller uses the MAX6675 function library to obtain the temperature value measured by the thermocouple, completing the amplification of the thermocouple output voltage signal, cold end compensation, and nonlinear processing, and finally outputting through the serial port.

#include "Max6675.h"
Max6675 ts(5, 6, 7);             //Define the pins for SO, CS, CSK
void setup(){
ts.setOffset(0);               //Set temperature offset
Serial.begin(9600);
}

void loop(){
  Serial.print("temperature is ");
  Serial.println(ts.getCelsius(), 2);   //Get Celsius temperature and send through serial
 delay(1000);                     //Refresh once every second
}

(3) Experimental Demonstration

The actual experimental hardware connection diagram is shown in Figure 25, and the temperature data received by the serial port during the experiment is shown in Figure 26.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 25 Experimental Hardware Connection Diagram

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 26 Temperature Data Received by Serial Port

6. Conclusion

This article introduces several commonly used sensors for temperature measurement, detailing aspects such as measurement principles, device characteristics, programming, and usage in Arduino. In summary, the following points can be made:

1. NTC thermistors are low-cost, but achieving high measurement accuracy requires a lot of optimization work, which can be challenging.

2. LM35 directly outputs analog voltage, making it convenient to use and with high accuracy, suitable for environmental temperature measurement in thermocouple cold end compensation.

3. DS18B20 is a single-bus digital temperature sensor with a high cost-performance ratio and high measurement accuracy, and it can connect multiple sensors on a single bus.

4. DHT11 is a temperature and humidity sensor that uses a single bus, does not occupy too many I/O ports, and can output humidity data simultaneously, suitable for applications requiring both temperature and humidity data.

5. Thermocouples used in conjunction with MAX6675 are suitable for high-temperature measurements, eliminating the need for cold end compensation, linearization, and analog-to-digital conversion work, making it easier to use with high accuracy. Secondary fitting calibration of its data can achieve even higher measurement accuracy.

Finally, comparing the response speed of the thermocouple MAX6675 module with DS18B20, as shown in Figure 27, based on the experimental platform of Arduino and LabVIEW, the data changes of the thermocouple when placed in hot water were collected. From the figure, it can be seen that the highest temperature is about 60℃, and the response curve of the thermocouple is relatively flat with a fast rising speed. Figure 28 shows the response curve of DS18B20 to temperature changes based on the Arduino and LabVIEW experimental platform, where the highest temperature exceeds 80℃, and the response curve of DS18B20 is relatively gentle, slowing down as the temperature difference decreases.

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 27 Response Curve of Thermocouple Temperature Change

Comprehensive Guide to Using Arduino Temperature Sensors

Figure 28 Response Curve of DS18B20 Temperature Change

Shen Jinxin/Nanjing Maker Space

Comprehensive Guide to Using Arduino Temperature Sensors

Leave a Comment

×