We are Apogeeweb Semiconductor Electronic


Home  Product Technical Articles   DS18B20 Digital Temperature Sensor (Example Analysis)

DS18B20 Digital Temperature Sensor (Example Analysis)

Author: Mia
Date: 22 Sep 2020
ds18b20 temperature sensor

I Description

First, this blog will introduce the 1 wire digital temperature sensor DS18B20. We mainly introduce its structure, characteristics and working principle here. Second, we will introduce a temperature measurement system based on DS18B20 and AT89S52 microcontroller. Here we mainly introduce its hardware structure and assembler. Third, there will be part of the source program that is detailed analyed. Finally, the blog also explains how it performs temperature measurement in the agricultural field.

The temperature measuring device has a series of advantages. Such as: high display accuracy, low price, simple structure, convenient expansion and wide application.

DS18B20 Temperature Sensor Tutorial


I Description

II Introduction

III DS18B20 Overview

3.1 DS18B20 Advantages

3.2 DS18B20 Features

3.3 DS18B20 Internal Structure

IV  DS18B20 MCU Temperature Measurement Device

4.1 Composition of System Hardware

4.2 Design of Interface

V Software Design

VI Application in Agricultural Production

6.1 Temperature of Mildew

6.2 Temperature of Agricultural Products

6.3 Temperature Detection in Greenhouses

6.4 Temperature of Soil

VII Conclusion


Ordering & Quantity

II Introduction

What is temperature?

What are the roles of temperature?

Temperature is a physical quantity that characterizes the degree of cooling of an object, and it is also a basic environmental parameter. In agro-industrial production and daily life, the measurement and control of temperature always occupy an extremely important position.

At present, a typical temperature measurement and control system consists of the following parts:

  • Analog temperature sensor;
  • A/D conversion circuit;
  • MCU.

However, the analog signal output by the analog temperature sensor has to be converted. It can interface with microprocessors such as single-chip microcomputers only after obtaining digital signals through the A/D conversion link. Therefore, the hardware circuit structure is complicated and the cost is high.

But DS18B20 can help solve this problem. The new 1 wire digital temperature sensor represented by DS18B20 integrates temperature measurement and A/D conversion, and directly outputs digital quantities. The structure of the interface circuit with the single-chip microcomputer is simple, and it is widely used in the occasions with long distance and many nodes. Therefore, DS18B20 has strong promotion and application value.

III DS18B20 Overview

3.1 DS18B20 Advantages

DS18B20 type 1 wire intelligent temperature sensor produced by DALLAS Semiconductor Company. It belongs to a new generation of intelligent temperature sensors adapted to microprocessors. Compared with the traditional thermistor, it has the following advantages:

  1. It can directly read the measured temperature;
  2. The reading mode of 9-12 digits can be realized through simple programming according to actual requirements;
  3. It can also complete 9-bit and 12-bit digital quantities within 93.75ms and 750ms, respectively, with a maximum resolution of 0.0625°C;
  4. To read or write the information of DS18B20, only one port line (1 wire interface) is required to read and write.

3.2 DS18B20 Features

  • Unique 1-Wire® Interface Requires Only One Port Pin for Communication
  •  Reduce Component Count with Integrated Temperature Sensor and EEPROM
    •  Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
    • ±0.5°C Accuracy from -10°C to +85°C
    •  Programmable Resolution from 9 Bits to 12 Bits
    • No External Components Required
  • Parasitic Power Mode Requires Only 2 Pins for Operation (DQ and GND)
  • Simplifies Distributed Temperature-Sensing Applications with Multidrop Capability
    •  Each Device Has a Unique 64-Bit Serial Code Stored in On-Board ROM
  • Flexible User-Definable Nonvolatile (NV) Alarm Settings with Alarm Search Command Identifies Devices with Temperatures Outside Programmed Limits
  • Available in 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92 Packages

3.3 DS18B20 Internal Structure

DS18B20 adopts 3-pin PR-35 package or 8-pin SOIC package.

Its DS18B20 external shape and pin diagram are shown in Figure 1.

The DS18B20 internal structure block diagram is shown as in Figure 2.

The structure of 64-bit flash ROM is shown in Figure 3.

ds18b20 pinoutFigure 1. DS18B20 Pinout

ds18b20 internal structure

Figure 2. DS18B20 Internal Structure

Figure 3. 64b Flash ROM Structure

IV  DS18B20 MCU Temperature Measurement Device

4.1 Composition of System Hardware

The DS18B20 single-chip microcomputer intelligent temperature measurement device is mainly composed of DS18B20 temperature sensor, AT89S52, display module and power module, as shown in Figure 4.

The main technical indicators of the product are:

  • Measuring Range (℃): -55.0~+125.0
  • Measurement Accuracy (℃): 0.1
  • Response Time (s): ≤1.5


Figure 4. System Structure Diagram

The system uses DS18B20 as a temperature sensor. The one-chip computer AT89S52 of ATMEL Company serves as the processor. Temperature display and light-emitting diode as temperature control output unit. The whole system strives to have a simple structure and perfect functions.

The working principle of the system is as follows: After DS18B20 carries on the field temperature measurement, the measured data is sent to the P3.5 port of AT89S52. The temperature value is displayed after being processed by the microcontroller. Then, this temperature value is compared with the upper limit of the set alarm temperature. If it is higher than the set upper limit, the yellow LED lights up.

The main circuit diagram of the system is shown as in Fig. 5.ds18b20 temperature sensor

Figure 5. DS18B20 Temperatuer Measurement Device

4.2 Design of Interface

There are 2 ways to connect DS18B20 to the hardware of the microcontroller:

  1. Vcc is connected to external power supply, GND is grounded, and I/O is connected to the I/O line of the microcontroller;
  2. Use parasitic power supply, UDD and GND are grounded at this time, and I/O is connected to MCU I/O.

Regardless of the 1st or 2nd power supply mode, the I/O line must be connected to a pull-up resistor of about 4.7kΩ. Figure 6 shows a typical connection between DS18B20 and a microprocessor.

  • In Figure 6(a), DS18B20 adopts parasitic power supply, and its VDD and GNG terminals are both grounded;
  • In Figure 6(b), the DS18B20 uses an external power supply, and its VDD terminal uses a 3~5.5V power supply.

This system adopts the wiring mode shown in Figure 6(b), that is, the working mode of external power supply. The actual connection picture of the system is shown in Figure 6.

Figure 6. Physical Diagram of System Connection

V Software Design

It is worth noting that DS18B20 has very high requirements on two aspects: timing and electrical parameters. Therefore, the work flow of the main CPU accessing the DS18B20 through the single-bus interface must follow a strict operating sequence: first, initialize the DS18B20; second, send ROM commands; and then, send function commands.

We can take a look at the following part of the source program is as follows:

ORG 0000H
AJMP MAIN; Statement of MCU memory allocation!
TEMPER_L EQU 29H; used to save the lower 8 bits of the read temperature
TEMPER_H EQU 28H; used to save the upper 8 bits of the read temperature
FLAG1 EQU 38H; Whether the DS18B20 flag is detected
PNFLAG EQU 68H; Data positive and negative flag
A_BIT EQU 20H; the single digit of the digital tube stores the memory location
B_BIT EQU 21H; The ten digits of the digital tube store the memory location
C_BIT EQU 22H; The decimal places of the digital tube store the memory location
T_INTEGER EQU 26H; The integer part after FORMAT, which integrates two bytes of temperature into one byte
T_DF EQU 27H; The decimal fraction after FORMAT, the decimal fraction of nibble temperature (there are low four digits)
LCALL GET_TEMPER; Call the temperature reading subroutine
LCALL T_FORMAT; Format the read 2 byte temperature
LCALL ALARM; call the alarm subroutine
LCALL DISPLAY; call the digital tube display subroutine
LCALL D1S; test after a delay of 0.5 seconds
AJMP MAIN; this is the DS18B20 reset initialization subroutine
INIT_1820: SETB P3.5
CLR P3.5; the host sends out a reset low pulse with a delay of 537 microseconds
MOV R1,#2
TSR1: MOV R0,#250
SETB P3.5; then pull up the data line
MOV R0,#25H
TSR2: JNB P3.5, TSR3; waiting for DS18B20 response
DJNZ R0, TSR2; delay
TSR3: SETB FLAG1; Set the flag bit to indicate that DS1820 exists
TSR4: CLR FLAG1; clear the flag bit, indicating that DS1820 does not exist
TSR5: MOV R0,#120
TSR6: DJNZ R0, TSR6; timing requires a period of delay
RET; read the temperature value after conversion
LCALL INIT_1820; first reset DS18B20
RET; Determine whether DS1820 exists? If DS18B20 does not exist
Then return
TSS2: MOV A,#0CCH; skip ROM matching
MOV A, #44H; Issue temperature conversion command
LCALL INIT_1820; reset before reading temperature
MOV A, #0CCH; Skip ROM matching
MOV A, #0BEH; Issue read temperature command
LCALL READ_18200; save the read temperature data to 28H/29H
RET; Write DS18B20 subroutine (with specific timing requirements)
WRITE_1820: MOV R2,#8; a total of 8 bits of data
WR1: CLR P3.5
MOV R3,#6
MOV P3.5,C
MOV R3,#23
RET; read the program of DS18B20, read two bytes of temperature data from DS18B20
READ_18200: MOV 36H, #2; Set the high and low temperature
Read from DS18B20
MOV R1, #29H; the low bit is stored in 29H (TEMPER_L), the high bit
Deposit 28H (TEMPER_H)
RE00: MOV R2,#8; There are 8 bits of data
RE01: ;CLR C
CLR P3.5
MOV R3,#9
RE10: DJNZ R3, RE10
MOV C,P3.5
MOV R3,#23
RE20: DJNZ R3, RE20
;-----Integrate the two-byte temperature read out (please refer to the information about the 2-byte temperature format read out by DS18B20) ----------
;Alarm subroutine
; Display subroutine
DISPLAY:; 1MS delay (calculated by 12MHZ)
D1MS: MOV R7,#250
DJNZ R7,llmm
RET; 1MS delay (calculated by 12MHZ)
D1S: Mov R6,#4
LOOP2: mov R5,#125; ------------ 250
RET; 7-segment digital tube 0-9 digit common anode display code
07H, 08H, 08H, 09H, 09H

VI Application in Agricultural Production

This temperature measurement system can directly output digital quantities. In addition, it has the characteristics of simple structure, convenient use and low price. Therefore, it can be widely used in agricultural production.

6.1 Temperature of Mildew

Modern grain warehouses can use this system to monitor the temperature of hundreds of points. In this way, you can easily grasp the temperature changes at various points at different times, increase storage capacity, and effectively reduce the occurrence of mildew.

6.2 Temperature of Agricultural Products

At present, low-temperature refrigeration measures are widely adopted for the preservation of fruits and vegetables. The system can be installed in the temperature measurement position of the refrigerator compartment. In this way, the temperature value can be conveniently observed at any time to check whether the optimal preservation temperature is reached.

6.3 Temperature Detection in Greenhouses

The system is used in plastic greenhouses for greenhouse vegetable cultivation and flower production. In this way, automatic temperature display can be realized, and labor and time for temperature measurement can be saved.

6.4 Temperature of Soil

In the process of planting crops with strict requirements on soil temperature, the system can test the changes in soil temperature as needed to facilitate the grasp of accurate temperature values.

VII Conclusion

The single-chip temperature measurement system takes full advantage of the simplicity of the hardware structure of DS18B20 and AT89S52, using 8-segment digital tube display, low price and wide application. According to actual needs, we can also use LCD as a display device or form a distributed temperature measurement and control system.

Although the design is easy to expand, it also has its shortcomings. The simplicity of the hardware structure comes at the expense of software. Therefore, special attention should be paid to the working sequence requirements of DS18B20 during programming.

In short, the system can be widely used in temperature measurement in agricultural production.


  • What is DS18B20 temperature sensor?

The DS18B20 is a 1-wire programmable temperature sensor from maxim integrated. It is widely used to measure temperature in hard environments like in chemical solutions, mines or soil etc. The constriction of the sensor is rugged and also can be purchased with a waterproof option making the mounting process easy.

  • How does the DS18B20 work?

It works on the principle of direct conversion of temperature into a digital value. 

  • Is DS18B20 a thermistor?

A thermistor is a thermal resistor - a resistor that changes its resistance with temperature. ... Thermistors have some benefits over other kinds of temperature sensors such as analog output chips (LM35/TMP36 ) or digital temperature sensor chips (DS18B20) or thermocouples.

  • How accurate is DS18B20?

The DS18B20 reads with an accuracy of ±0.5°C from -10°C to +85°C and ±2°C accuracy from -55°C to +125°C.

  • What is ds1820?

The DS18B20 is one type of temperature sensor and it supplies 9-bit to 12-bit readings of temperature. ... The communication of this sensor can be done through a one-wire bus protocol which uses one data line to communicate with an inner microprocessor.

  • How do I connect my DS18B20 to my Raspberry Pi?

Once you've connected the DS18B20, power up your Pi and log in, then follow these steps to enable the One-Wire interface:
1.At the command prompt, enter sudo nano /boot/config.txt , then add this to the bottom of the file:
3.Exit Nano, and reboot the Pi with sudo reboot.

  • What is the working principle of DS18B20?

The DS18B20 Digital Thermometer provides 9 to 12-bit (configurable) temperature readings which indicate the temperature of the device. It communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition it can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.
The core functionality of the DS18B20 is its direct-to-digital temperature sensor. The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively. The default resolution at power-up is 12-bit.

  • Where to use DS18B20 Sensor?

The DS18B20 is a 1-wire programmable Temperature sensor from maxim integrated. It is widely used to measure temperature in hard environments like in chemical solutions, mines or soil etc. The constriction of the sensor is rugged and also can be purchased with a waterproof option making the mounting process easy. It can measure a wide range of temperature from -55°C to +125° with a decent accuracy of ±5°C. Each sensor has a unique address and requires only one pin of the MCU to transfer data so it a very good choice for measuring temperature at multiple points without compromising much of your digital pins on the microcontroller.

  • How connect DS18B20 to Arduino?

First plug the sensor on the breadboard the connect its pins to the Arduino using the jumpers in the following order: pin 1 to GND; pin 2 to any digital pin (pin 2 in our case); pin 3 to +5V or +3.3V, at the end put the pull-up resistor.

  • On an ATMega328P, why is a DS18B20 temperature sensor returning incorrect temperature values?

Several possibilities:
1. If it is just reading a little high, it might be caused by “self heating”. Add a heat sink and/or make measurements less frequently.
2. Especially if the values are really whacky, it might be code with errors or mis-wiring. Use a published sketch to check operation.
3. The DS18B20 might be defective. Try another.
4. It’s accurate to 0.5ºC.

Are you expecting it to be more accurate (like down to the LSB of the read value)?

Ordering & Quality

Photo Mfr. Part # Company Description Package PDF Qty Pricing
DS18B20 DS18B20 Company:Maxim Integrated Remark:Temperature Sensor Digital, Local -55°C ~ 125°C 12 b TO-92-3 Package:TO-226-3, TO-92-3 (TO-226AA)
In Stock:On Order
DS18B20-GG8 DS18B20-GG8 Company:Maxim Integrated Remark:Programmable Resolution 1-Wire Digital Thermometer Package:TO-92
In Stock:On Order
DS18B20U DS18B20U Company:Maxim Integrated Remark:SENSOR DIGITAL -55C-125C 8UMAX Package:8-TSSOP, 8-MSOP (0.118", 3.00mm Width)
In Stock:2478
1+: $4.25000
10+: $3.81700
25+: $3.60800
100+: $3.12710
500+: $2.66202
1000+: $2.57839
DS18B20Z DS18B20Z Company:Maxim Integrated Remark:Temperature Sensor Digital, Local -55°C ~ 125°C 12 b 8-SOIC Package:8-SOIC (0.154"", 3.90mm Width)
In Stock:On Order

Related Articles

IR2110 MOSFET/IGBT Driver: Pinout, Features, Circuit [Video]

Mia 28 Dec 2020  7860

In many applications MOSFETs configured as high-side switches and many times, it is configured as high-side and low-side switch. In such applications high-low side MOSFET drivers are used.   I...

Continue reading »

ESP8266 NodeMCU Basics: Datasheet, Pinout [FAQ]

Mia 30 Dec 2020  5306

Most people call ESP8266 as a WIFI module, but it is actually a microcontroller. ESP8266 is the name of the microcontroller developed by Espressif Systems which is a company based out of shanghai. Thi...

Continue reading »

LM358N Op Amp: Pinout, Circuit, Datasheet [Video]

Mia 26 Feb 2021  1210

LM358 is a dual op-amp IC integrated with two op-amps that are powered by a common power supply, these two amplifiers are independent, high gain, internally frequency compensated operational amplifier...

Continue reading »

MOC3021 Triac Driver: Pinout, Datasheet, Equivalent [Video]

Mia 1 Apr 2021  2352

The MOC3021 is a Zero-Crossing TRIAC driven Optocoupler or Optoisolator.   As we know, the term Optocoupler/Optoisolator means the same thing that we use light to indirectly couple circui...

Continue reading »

LIS3MDL Magnetometer: Datasheet, Features, Pinout [Video&FAQ]

Irene 22 Sep 2021  49

Product OverviewThe LIS3MDL is an ultra low-power high performance three axis magnetic sensor. The self-test capability allows the user to check the functioning of the sensor in the final application....

Continue reading »

0 comment

Leave a Reply

Your email address will not be published.


# 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z