PIC18F4550 with DS18B20 sensor and LCD project

This post shows how to measure the ambient temperature using PIC18F4550 microcontroller, DS18B20 digital temperature sensor where the measured temperature is displayed on 16×2 LCD screen. The compiler used in this project is CCS C (PIC C). Also a small video shows Proteus simulation of the project is provided at the end of the post.

The DS18B20 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 is a digital sensor which provides 9-bit to 12-bit Celsius temperature measurement resolution (programmable resolution). The default resolution of the DS18B20 sensor is 12-bit which means the temperature step is 0.0625°C.

Components Required:

  • PIC18F4550 microcontroller
  • DS18B20 temperature sensor — datasheet
  • 16×2 LCD screen
  • 4.7k ohm resistor
  • Power source with 5 VDC
  • Breadboard
  • Jumper wires

PIC18F4550 with DS18B20 temperature sensor and LCD

PIC18F4550 microcontroller with DS18B20 sensor and LCD circuit:
Project circuit schematic diagram is shown below.

PIC18F4550 DS18B20 temperature sensor and LCD circuit

(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 PIC18F4550 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
RW –> RD1
E  —> RD2
D4 —> RD3
D5 —> RD4
D6 —> RD5
D7 —> RD6

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

PIC18F4550 microcontroller with DS18B20 sensor and LCD C code:
The C code below is for CCS C compiler, it was tested with version 5.051.

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.

Proteus simulation of the project is the one below:

PIC18F4550 + DS18B20 sensor + LCD Proteus simulation file download:
Download

Leave a Reply

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