Bukit Mertajam, Penang

DS18B20 Temperature Sensor


The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire® bus that by definition requires only one data line (and ground) for communication with a microcontroller. The DS18B20 has an operating temperature range of -55°C to +125°C and the accuracy is ±0.5°C over the range of -10°C to +85°C. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microcontroller to control multiple DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, process monitoring and control systems.

Key Features

  • Unique 1-Wire Interface Requires Only One Pin for Communication
  • Each Device has a Unique 64-Bit Serial Code Stored in an On-Board ROM
  • Multidrop Capability Simplifies Distributed Temperature-Sensing Applications
  • Requires No External Components
  • Can Be Powered from Data Line; Power Supply Range is 3.0V to 5.5V, for long distance, power is necessary.
  • Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
  • ±0.5°C Accuracy from -10°C to +85°C
  • Thermometer Resolution is User Selectable from 9 to 12 Bits
  • Converts Temperature to 12-Bit Digital Word in 750ms (Max)
  • User-Definable Nonvolatile (NV) Alarm Settings
  • Alarm Search Command Identifies and Addresses Devices Whose Temperature is Outside Programmed • • • Limits (Temperature Alarm Condition)
  • Available in 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92 Packages
  • Software Compatible with the DS1822
  • Applications Include Thermostatic Controls, Industrial Systems, Consumer Products, Thermometers, or Any
  • Thermally Sensitive System

Hardware required:

DS18B20 Temperature Sensor x1


SK40C + PIC16F877A with LCD x1


Software required:



Related References:

Getting Started with MPLAB IDE

How to Create a project

Getting Started with SK40C

Temperature Sensor Datasheet


Using DS18B20 temperature sensor


In this project, we are going to measuring the environment temperature using the water proof DS18B20 temperature sensor. This  type of sensor are quite special since it only use the One-Wire-protocol to read he temperature and to write any command into the sensor.

Since it only use 1 wire for signal and give command, so I will use RB0 as the only port to control the temperature sensor and to read it.


The connection for my project is as below




The colour of the sensor are following the colour of the diagram


Programming and Result

Let’s take a look at the sample code before we proceed to explanation.

This is the main body of the sample coding. In this part we are setting up the sensor and at the second part, we are accepting the reading from the sensor and convert it further publish the value to the LCD screen,


We need to 1st reset the sensor. This is to check the availability of the sensor present in the communication line. If the sensor is present we will proceed to the read temperature step.


The reading function.


The writing function. The writing function is to send the ROM command to the sensor, for it to know what it need to do.



Maybe you are curious to know why one wire can do so many thing including read (receive) and write(transmit). Actually this sensor utilize the one wire protocol. Referring to the figure below, try to understand how the read and write works.

It is hard for me to explain here since it will consume few pages of words 🙂 I would strongly suggest you to read it at Wikipedia pages HERE





Initial condition for the sensor to sense room temperature


HOT WATER AROUND  ~68 degree Celsius….. Yes, you can submerge it in to water or liquid 🙂 Quite fun!

This sensor is perfect due to water proof feature and simple interface. Besides, you can also do multiple sensor in a network as it has unique address.


• Please do take care of the voltage polarity, supplying wrong polarity of power will damage the sensor permanently.

Have fun and if you have any inquiry, welcome to post it in our technical forum as we seldom check the comment section in the tutorial 🙂

Sample Coding


Related Posts

Comments (14)

can i use this component to measure hot oil?i want to measure cooking oil temperature

Not really sure. What is the expected temperature for the oil when it is cooked? Because the maximum operating temperature for this sensor is +125 degree Celsius. Besides, we do not know will the shrinking material that wrap the sensor react with the cooking oil.

When I try to “rebuild” the code in MPLAB, the build failed with message :
“Error [712] H:\Arduino\Projects-PIC\DS18B20\Example Code\lcd.c; 295. can’t generate code for this expression”

The same lcd.c file that was used in another tutorial : “GSM modem TC35 with PIC16F877A” is able to build without any problems.

What could be the problem ?

Thank you.

What are the functions:


do you have any codes for multiple DS8S20 in a 1-wire
or any help you can give me?

Just bought this months ago and I tried to use the file to run on Arduino but failed. Can this be run on it? Thanks!

Just search Arduino DS18B20 in google, I am sure you will get plenty of example 🙂

I would like to ask about the lane “i = (float)i * 6.25 ; //calcuation from the table provided ” Where is that cable and why is 6.25 ?
Thank you for your time.

Can i ask you ,how to get the reading with using a programmable logic controlled (PLC),and how to convert the output voltage from the sensor to the degree of calcius

Hello. I need to rename my sensors rather than having just a serial number. Can you point me in the right direction?

hi can i use this sensor with pic18f452 microcontroller?

Hi, of course you can. But we don’t have sample code for that.

Nnajiofor Emmanuel

Hi friends i’m stuck trying to program PIC16F877A to read temperature from a temp sensor and display the result o an Lcd. The design with a C-code succesfully simulated on proteus but i’ve not been able to get it work practically. I’m using TL866 Universal Programmer. HELP

Roy Bettesworth

Hi I am trying to work out how to Identify the different probes. I am planning 5 sensors on one line but can’t identify which probe is which – is there any way of doing it ?

Leave a comment