Interfacing DS18B20 Sensor with PIC MCU – mikroC Projects

In this blog there are many examples shows how to interface some sensors with different types of microcontrollers and in this topic I’m going to show how to connect the popular and low cose temperature sensor DS18B20 with PIC16F887 microcontroller. The compiler used in this example is mikroC PRO for PIC.

Related Projects:
Interfacing PIC MCU with DHT11 sensor – mikroC Projects
DHT22 Sensor interface with PIC microcontroller – mikroC Projects
LM335 Sensor interface with PIC16F887 MCU – mikroC Projects
Interfacing PIC microcontroller with LM35 sensor – mikroC Projects
Interfacing PIC16F877A with DS18B20 temperature sensor

The DS18B20 sensor is a 3-pin electronic component (like a simple transistor) from Analog Devices (formerly Maxim Integrated) which uses 1-wire protocol to communicate with master device (microprocessor, microcontroller….).
The DS18B20 sensor provides 9-bit to 12-bit Celsius temperature measurement resolution (programmable resolution).

Components Required:

  • PIC16F887 microcontroller   —> datasheet
  • DS18B20 temperature sensor — datasheet
  • 16×2 LCD screen
  • 4.7k ohm resistor
  • 10k ohm variable resistor or potentiometer
  • Power source with 5 VDC
  • Breadboard & jumper wires
  • PIC Microcontroller programmer (PICkit 2, PICkit 3…)

PIC16F887 microcontroller with 16x2 LCD and DS18B20 temperature sensor

Interfacing DS18B20 sensor with PIC microcontroller circuit:
Example circuit schematic diagram is shown below.

mikroC DS18B20 temperature sensor with PIC16F887

(All grounded terminals are connected together)

The DS18B20 sensor has 3 pins (from left to right): GND, data and VCC (+5V). The data pin is connected to PIC16F887 pin RB1.

The LCD screen is used to display the temperature value read by the DS18B20 sensor. It’s connected to PORTD pins with:
RS —> RD0
E  —> RD1
D4 —> RD2
D5 —> RD3
D6 —> RD4
D7 —> RD5

In this example the PIC16F887 microcontroller runs with its internal oscillator (@ 8MHz) and MCLR pin is configured as a digital input pin (configured in the software).

Interfacing DS18B20 sensor with PIC microcontroller mikroC code:
The C code below is for mikroC PRO for PIC compiler, the configuration words are:
CONFIG1 : 0x2CD4
CONFIG2 : 0x0700

The mikroC compiler has a built-in one-wire protocol library which makes the interfacing more easier.

The default resolution of DS18B20 is 12-bit which means the step of the temperature is 0.0625°C. The datasheet of the device contains more details about that. Rest of code is described through comments.


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top