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:
MPLAB XC8 LCD Library

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:

Where:

  • 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

4 thoughts on “Interfacing DS18B20 sensor with PIC microcontroller | MPLAB Projects”

  1. what’s up friend

    I’m trying to replicate your code with a PIC18F4550, but no matter how hard I try, I can’t get the sensor to connect to the pic, it always shows ERROR, as far as I can trace the problem is in the START FUNCTION, it always returns 0, according to me configuring the ADCON1 should work, but no, it always enters the ERROR message 🙁

  2. Hello,

    I’m getting a very very strange reading with this code.
    I’m using my own lcd library ( but it clearly works perfectly )
    And as i remove the pull up resistor the code suddenly stops running and the lcd doesn’t refresh anymore.
    When i reconnect the resistor we start get readings once again.
    So i conclude the sensor is working like it shoud.
    But like right now i’m measuring 6,4375°c and i’m writing this code in a warm room in my t-shirt 🙂
    When i touch the sensor i clearly see the temp reading rising.
    But then when we would measure 16°C he suddenly drops to 0°C to sart counting up again.
    Same for descending temp readings.

    So there must be a problem in the conversion and i have a slight idea where to look.
    uint16_t doesn’t work for me in MplabX v2.0.5
    so i used unsigned_int to replace it. ( so a 2 byte integer )

    What should i do to get a correct reading?

    Kind regards,

    Tom

  3. Thanks for posting the project, it helped a great deal. I had a little trouble at first with the LCD, but increasing the delay
    times in the “LCD_Lib.c” file resolved the problem.

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