PIC18F4550 MCU with SSD1306 OLED and DS18B20 sensor

This project shows how to implement a simple temperature measurement station using PIC18F4550 microcontroller and DS18B20 digital temperature sensor.
The PIC18F4550 MCU reads temperature from the DS18B20 sensor and print its value (in °C) on SSD1306 OLED display (128×64 pixel).
CCS C compiler is used in this project.

In this project the SSD1306 OLED is configured to work in I2C mode, make sure that your display is configured to work in I2C mode, some displays need jumper placing or some soldering.

To see how to interface PIC18F4550 MCU with SSD1306 OLED display (I2C mode), take a look at the following project:
Interfacing PIC18F4550 with SSD1306 OLED

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 ….). 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).

Hardware Required:

  • PIC18F4550 microcontroller     —->  datasheet
  • SSD1306 OLED display with 128×64 pixel resolution
  • DS18B20 temperature sensor    —->   datasheet
  • 4.7k ohm resistor
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F4550 MCU with DS18B20 sensor and SSD1306 OLED

PIC18F4550 with SSD1306 OLED and DS18B20 sensor circuit:
The image below shows project circuit diagram.

The DS18B20 sensor has 3 pins (from left to right): GND, data pin and VCC (or VDD) where:
GND: connected to circuit ground (0V),
data pin: connected to PIC18F4550 RB3 (#36) and
VCC: sensor power supply pin, connected to circuit +5V.

A pull-up resistor of 4.7k ohm is required because the DS18B20 has an open drain output.

PIC18F4550 DS18B20 sensor SSD1306 OLED

All the grounded terminals are connected together.

The SSD1306 OLED display is connected to the circuit as follows:
SSD1306 OLED GND goes to circuit ground,
SSD1306 OLED VDD is connected to circuit 5V,
SSD1306 OLED SDA pin (serial data) to pin RB0 (PIC18F4550 hardware I2C SDA pin),
SSD1306 OLED SCK pin (serial clock) to pin RB1 (PIC18F4550 hardware I2C SCL pin),
SSD1306 OLED RES pin (reset) to pin RB2.

The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x7A. If the DC pin is connected to ground (GND) then the I2C slave address becomes 0x78.

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

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

To be able to compile the C code below with no error, a driver for the SSD1306 OLED display is required, it’s full name (with extension) is SSD1306OLED.C, for more information about this driver, visit the following post:
SSD1306 OLED Library for CCS C compiler

It can be downloaded also from the link below:
SSD1306 OLED Library download

after the download, add the driver file to project folder or CCS C compiler drivers folder.

Programming hints:
The SSD1306 OLED reset pin and DS18B20 sensor data pin are defined in the code as shown below:

Functions used in the code:
int1 ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns 1 if OK and 0 if error.
ds18b20_write_bit(int1 value): writes (sends) 1 bit to the DS18B20 sensor, the bit is value which may be 1 or 0.
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 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 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 1 if OK and 0 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.
Note that 1/16 = 0.0625.

Temperature value is printed on the SSD1306 OLED. If there is a problem with the sensor, the screen will display ERROR instead of the temperature value.

Full CCS C code:

The following small video shows the result of my hardware circuit:

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