PIC MCU with SSD1306 OLED and DHT22 sensor | mikroC Projects

This topic shows how to interface PIC16F887 microcontroller with SSD1306 OLED display (128×64 pixel) and DHT22 (AM2302, RHT03) digital humidity and temperature sensor. The measured values of the relative humidity and temperature are displayed on the SSD1306 OLED screen.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.
A small video shows Proteus simulation of this project at the end of the post.

The SSD1306 OLED used in this project is configured to work in I2C mode, some SSD1306 OLED boards may require a small hardware modifications (to select between SPI mode and I2C mode) such as soldering, placing jumpers …

PIC16F887 MCU with SSD1306 OLED and DHT22 sensor

Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC microcontroller with SSD1306 OLED | mikroC Projects
DHT22 Sensor interface with PIC microcontroller – mikroC Projects

Hardware Required:

  • PIC16F887 microcontroller
  • SSD1306 OLED display (128×64 Pixel)
  • DHT22 (AM2302) digital temperature and humidity sensor
  • 4.7k ohm resistor
  • 5V power source
  • Breadboard
  • Jumper wires

Interfacing PIC16F887 MCU with SSD1306 OLED and DHT22 sensor circuit:
Project circuit diagram is shown below.

PIC16F887 with SSD1306 OLED and DHT22 sensor - SSD1306 DHT22

(All grounded terminals are connected together)

The PIC16F887 microcontroller has one hardware I2C module (MSSP module) with SDA on pin RC4 (#23) and SCL on pin RC3 (#18). The SDA pin of the MCU is connected to the SDA pin of the display and the SCL pin of the MCU is connected to the SCL pin of the display.
The reset pin of the display is connected to pin RD4 (#27) of the microcontroller.

The SSD1306 OLED display DC pin is connected to VDD which means the I2C slave address of the display is 0x7A.

The DHT22 sensor has 4 pins (from left to right): VCC (+5V), data pin, NC (not connected pin) and GND. The data pin is connected to pin RD5 (#28). A pull-up resistor of 4.7k ohm is required for the data pin.

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

Interfacing PIC16F887 MCU with SSD1306 OLED and DHT22 sensor C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.

To be able to compile the C code below with no error, a driver (& library) for the SSD1306 OLED display is required, it’s full name is SSD1306.C, download link is below:
SSD1306 OLED display driver for mikroC compiler

after the download, add the driver file (SSD1306.C) to project folder.

Functions used in the code:
Start_Signal(): this function sends the start signal to the sensor, it sends a logic low for 25 ms and a logic high for 30 us.

Check_Response(): this function checks if there is a response from the sensor (after sending the start signal using the previous function), returns 1 (true) if ok and 0 (false) if error (for example a connection problem).

Read_Data(*dht_data): this function reads temperature and humidity data from the sensor (4 bytes), it also reads an other byte (5th byte) which is used as a checksum. This function returns 0 (false) if data read was ok and 1 (true) if there was a time out problem.

As shown in the circuit diagram above, the DHT22 data pin is connected to pin RD5, this connection is defined as follows:

In this project I used Timer1 module to measure signal widths (logic 1 and logic 0 widths), it is configured to increment every 1 us (1 tick per microsecond).

The reset pin of the SSD1306 OLED display module is connected to pin RD4 of the microcontroller, this connection is defined as (there is no need for this connection if the display board has no rest pin):

Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700

The simulation of this project using Proteus should be as shown in the following video where PIC16F877A microcontroller is used:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

1 thought on “PIC MCU with SSD1306 OLED and DHT22 sensor | mikroC Projects”

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