Interfacing PIC16F877A with DS18B20 temperature sensor

Interfacing the popular microcontroller PIC16F877A with the temperature sensor DS18B20 is so easy, in this topic I’m going to show how to build a digital thermometer using PIC16F877A and DS18B20 sensor where the temperature is displayed on 16×2 LCD screen.

Firstable 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 ….). Each DS18B20 device has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus and controlled with one master device.
The DS18B20 sensor provides 9-bit to 12-bit Celsius temperature measurement resolution (programmable resolution).
The DS18B20 sensor is available in 8-Pin SO (150 mils), 8-Pin μSOP, and 3-Pin TO-92 Packages. DS18B20 pin configurations is shown below:

ds18b20 pin configurations

In this example the master device is the PIC16F877A microcontroller.

Components Required:

  • PIC16F877A microcontroller   —> datasheet
  • DS18B20 temperature sensor   —> datasheet
  • 16×2 LCD screen
  • 8 MHz crystal oscillator
  • 2 x 22pF ceramic capacitors
  • 10K ohm resistor
  • 4.7k ohm resistor
  • Power source with 5 VDC
  • Breadboard
  • Jumper wires

Interfacing PIC16F877A with DS18B20 temperature sensor circuit:
The image below shows the circuit schematic diagram of the project.

PIC16F877A DS18B20 sensor 16x2 LCD circuit

(All grounded terminals are connected together)

The DS18B20 sensor has 3 pins: VCC (+5V), data and GND. The data pin is connected to PIC16F877A pin RB1. The 16×2 LCD module is connected to PORTD pins.

The LCD screen is used to display the temperature value read by the DS18B20 sensor.

Interfacing PIC16F877A with DS18B20 temperature sensor CCS C code:

Functions used in the code:
int1 ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns TRUE (1) if OK and FALSE (0) if error.
void ds18b20_write_bit(int1 value): writes (sends) 1 bit to the DS18B20 sensor, the bit is ‘value’ which may be 1 or 0.
void ds18b20_write_byte(int8 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.
int1 ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0).
int8 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.
int1 ds18b20_read(int16 *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 TRUE if OK and FALSE if error.

The value of the temperature in degree Celsius is equal to the raw value divided by 16 (in case of 12-bit resolution). The default resolution of the DS18B20 is 12 bits.

Rest of code is described through comments!

Arduino + DS18B20 sensor video:
The video below shows Proteus simulation of this project.

Arduino + DS18B20 sensor + LCD Proteus simulation file download:

10 thoughts on “Interfacing PIC16F877A with DS18B20 temperature sensor”

    1. Make sure that you’re using the same clock frequency setting for the compiler and Proteus (8MHz).
      Also check the 4.7K resistor properties (Model Type: DIGITAL).

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