The types of temperature sensors are numerous, classified by measurement methods into contact and non-contact types, by sensor materials into thermistors and thermocouples, and by working principles into analog and digital types. The previous article introduced the analog temperature sensor LM35, while this article introduces the use of the digital temperature sensor DS18B20.
1. Introduction to DS18B20
DS18B20 is a commonly used digital temperature sensor that uses an integrated chip and employs single-bus technology, effectively reducing external interference and improving measurement accuracy. It outputs a digital signal, making wiring very convenient, and it can be packaged in different forms for various applications, such as pipe type, threaded type, magnetic adsorption type, and stainless steel encapsulated type.

Main features:
-
Uses a single-bus interface. Only one data line is needed for two-way communication.
-
Wide measurement range and high accuracy. Its measurement range is -55℃ ~ +125℃, with an accuracy of ±0.5℃ in the range of -10~+85℃.
-
Multi-point networking capability. Multiple DS18B20s can be connected in parallel on a single wire for multi-point temperature measurement.
-
Flexible power supply method. Can obtain power from the data line through an internal parasitic circuit.
-
Configurable measurement parameters. The measurement resolution of DS18B20 can be set programmatically from 9 to 12 bits.
-
Power-off protection function. It contains EEPROM, which can retain the set resolution and alarm temperature values even after power-off.

2. Experiment Materials
-
Uno R3 Development Board
-
USB Data Cable
-
Breadboard and Connecting Wires
-
DS18B20
-
10K Through-hole Resistor
3. Install Libraries
This experiment uses two libraries: “OneWire” and “DallasTemperature”. The former is the single-bus library, while the latter is a library encapsulated for Dallas temperature sensors based on the former.
-
In the IDE, click “Project” — “Load Library” — “Manage Libraries”, search for “OneWire”, and select the latest version for installation. The latest version is currently 2.3.4.

-
Search for “DallasTemperature”, and select the latest version for installation. The latest version is currently 3.8.0.

4. Experiment Steps
1. Build the circuit according to the schematic diagram.
The wiring is very simple: connect DS18B20’s VCC, DQ, and GND to the development board’s 5V, 2, and GND respectively. One end of the 10K resistor connects to VCC, and the other end connects to DQ; the pull-up resistor is used to enhance the driving capability of the I/O port.
The schematic diagram is shown below:

The physical connection diagram is shown below:

2. Create a sketch, copy the following code to replace the automatically generated code, and save it.
#include <OneWire.h>#include <DallasTemperature.h>
// Data output pin connected to digital pin 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(); // Send command to get temperature 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 board type, and download the program.

5. Experiment Phenomena
Open the serial monitor, set the baud rate to 9600, and the serial port will continuously print the read temperature values.

