Interfacing DS18B20 sensor with PIC microcontroller | MPLAB Projects

The DS18B20 temperature sensor is a 3-pin electronic component (like a simple transistor) from Maxim (formerly Dallas) 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).
This topic shows how to interface PIC16F887 microcontroller with DS18B20 temperature sensor where temperature value is displayed on 16×2 LCD screen.

The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).

PIC16F887 microcontroller with 16x2 LCD and DS18B20 temperature sensor

Components Required:

  • PIC16F887 microcontroller       —->  datasheet
  • DS18B20 temperature sensor   —->  datasheet
  • 16×2 LCD screen
  • 4.7k ohm resistor
  • 330 ohm resistor
  • 10k ohm variable resistor or potentiometer
  • Power source with 5 VDC
  • Breadboard
  • Jumper wires

Interfacing DS18B20 sensor with PIC microcontroller circuit:
The following image shows project circuit schematic diagram.

PIC16F887 MCU with DS18B20 sensor and LCD - MPLAB XC8 DS18B20

(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 (#34).

The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing DS18B20 sensor with PIC microcontroller C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

To be able to compile the C code, a small LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:

after the download, add the library file (LCD_Lib.c) to project folder.

Functions used in the code:
The default resolution of DS18B20 is 12-bit which means the step of the temperature is 0.0625°C. More details are in the datasheet of the device.

__bit ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns 1 if OK and 0 if error.

void ds18b20_write_bit(uint8_t value): writes (sends) 1 bit to the DS18B20 sensor, this bit is the LSB of the 8-bit variable ‘value’ which may be 1 or 0.

void ds18b20_write_byte(uint8_t value): writes 1 byte (8 bits) to the DS18B20 sensor, this function is based on the previous function. This function writes LSB (Least Significant Bit) first.

__bit ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0).

uint8_t ds18b20_read_byte(void): reads 1 byte (8 bits) from the DS18B20 sensor, this function is based on the previous function. This function reads LSB first.

__bit ds18b20_read(uint16_t *raw_temp_value): reads the temperature raw data which is 16-bit long (two 8-bit registers), the data is stored in the variable raw_temp_value, returns 1 if OK and 0 if error.

The microcontroller used in this example is PIC16F887, configuration words are:


  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Full MPLAB XC8 code:

Proteus simulation of the project should give a result similar to what’s shown in the following video where PIC18F4550 microcontroller is used (connections between the PIC16F887 and the 1602 LCD are not the same):

Related Projects:
Interfacing LCD with PIC microcontroller | MPLAB Projects
Interfacing DHT11 sensor with PIC microcontroller | MPLAB Projects
Interfacing DHT22 sensor with PIC microcontroller | MPLAB Projects

Leave a Reply

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