How to test Digital temperature sensor DS18B20 with arduino uno

lu bing

There are many types of temperature sensors, which are divided into contact type and non-contact type according to the measurement method, thermal resistance and thermocouple according to the sensor material, and analog and digital according to the working principle. The previous article introduced the analog temperature sensor LM35, and this article introduced the use of the digital temperature sensor DS18B20.

  1. Introduction to DS18B20
    DS18B20 is a commonly used digital temperature sensor, which uses an integrated chip and uses a single bus technology to effectively reduce external interference and improve measurement accuracy. The output is a digital signal, and the wiring is very convenient. It can be applied to different occasions in different ways, such as pipe type, screw type, magnet adsorption type, and stainless steel package type.

Main feature:

Use a single bus interface. Only one data line is needed for bidirectional communication.


Wide measuring range and high accuracy. The measurement range is -55 ℃ — + 125 ℃, and the accuracy is ± 0.5 ℃ in the range of -10— + 85 ℃.
Multi-point networking function. Multiple DS18B20 can be connected in parallel on the only three lines to achieve multi-point temperature measurement.


Flexible power supply. Power can be obtained from the data line through internal parasitic circuits.


The measurement parameters are configurable. The measurement resolution of DS18B20 can be set to 9-12 bits through the program.
Power-off protection function. It contains EEPROM, after the system is powered off, it can still save the resolution and alarm temperature settings.

  1. Experimental materials
    Uno R3 development board
    Supporting USB data cable
    Breadboard and supporting cable
    DS18B20
    10K in-line resistor
  2. Install the library
    Two libraries are used in this experiment, namely “OneWire” and “DallasTemperature”, the former is a single bus library, and the latter is a library packaged for Dallas temperature sensors based on the former.

Click “Project”-“Load Library”-“Manage Library” in the IDE, find “OneWire”, select the latest version to install. The latest version is currently 2.3.4.

  1. Experimental steps
  2. Build the circuit diagram according to the schematic diagram.
    The line connection is very simple. The VCC, DQ, and GND of the DS18B20 are connected to the 5V, 2, and GND of the development board, respectively. A 10K resistor is connected to VCC at one end, and DQ is connected at the other end, and the pull-up resistor is used to improve the drive capability of the I / O port.

The experimental schematic diagram is shown below:

Code:

#include <OneWire.h>
#include <DallasTemperature.h>

// 数据输出脚接开发板数字引脚2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup(void)
{
  Serial.begin(9600);
  sensors.begin();
}

void loop(void)
{ 
  sensors.requestTemperatures(); // 发送命令获取温度
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempCByIndex(0)); 
  delay(500); 
}

3.Connect the development board, set the corresponding port number and development board type, and download the program

Experimental phenomena
Open the serial monitor, the baud rate is set to 9600, the serial port will print the temperature value read cyclically

Leave a Reply

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