

DS18B20 data pin is connected to pin RB1 * This is a free software with NO WARRANTY. * Interfacing PIC16F887 microcontroller with DS18B20 temperature sensor. The microcontroller used in this example is PIC16F887, configuration words are: _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. Uint8_t ds18b20_read_byte(void): reads 1 byte (8 bits) from the DS18B20 sensor, this function is based on the previous function. _bit ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0). This function writes LSB (Least Significant Bit) first. Void ds18b20_write_byte(uint8_t value): writes 1 byte (8 bits) to the DS18B20 sensor, this function is based on the previous function.

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.

_bit ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns 1 if OK and 0 if error. More details are in the datasheet of the device. The default resolution of DS18B20 is 12-bit which means the step of the temperature is 0.0625☌. 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:Īfter the download, add the library file ( LCD_Lib.c) to project folder. The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05. Interfacing DS18B20 sensor with PIC microcontroller C code: In this project the PIC16F887 microcontroller runs with its internal oscillator 8 MHz, MCLR pin is configured as an input pin. A (anode) and K (cathode) are the back light LED pins. VEE pin is used to control the contrast of the LCD. VDD to +5V and A to +5V through 330 ohm resistor VEE to the variable resistor (or potentiometer) output pin VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground) The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows: The data pin is connected to PIC16F887 pin RB1 (#34). The DS18B20 sensor has 3 pins (from left to right): GND, data and VCC (+5V). (All grounded terminals are connected together)
