Interfacing PIC18F4550 with DHT22 and SSD1306 display

Previously I connected the PIC18F4550 microcontroller with SSD1306 OLED display and I was able to draw some shapes (lines, circles, triangles …) and write texts, next I build a simple relative humidity and temperature measurement station using the same microcontroller and display where the sensor used was DHT11. Now in this topic I am going to build the same project but instead of the DHT11 I’ll use DHT22 (AM2302, RHT03) sensor because it (DHT22) is better than the other (DHT11).

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

Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC18F4550 with SSD1306 OLED
PIC18F4550 with SSD1306 OLED and DHT11 sensor
PIC18F4550 Interface with DHT22(AM2302) sensor

Hardware Required:

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

PIC18F4550 with SSD1306 OLED display and DHT22 sensor

The Circuit:
Project circuit diagram is shown below.

PIC18F4550 SSD1306 DHT22 AM2302 sensor

(All grounded terminal are connected together)

There are 3 wires between the PIC18F4550 microcontroller and the SSD1306 OLED display: SDA (serial data), SCL (serial clock) and reset. SDA and SCL (SCK) pins of the display are connected respectively to pin RB0 (#33) and pin RB1 (#34) of the microcontroller, these pins are hardware I2C module SDA and SCL pins.
The reset pin (RES) of the display is connected to pin RB2 (#35) of the microcontroller.

The SSD1306 OLED display DC pin is connected to VDD which means the I2C slave address of the display is 0x7A. There is no need to use this pin if your display module doesn’t have a reset pin.

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 RB3 (#36). A pull-up resistor of 4.7k ohm is required for the data pin.

All the circuit is supplied with 5V including the SSD1306 OLED display.

In this project the PIC18F4550 runs with its internal oscillator and MCLR pin function is disabled.

The 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 name 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.

If there is a connection problem or checksum error the display will give “EEE.E°C” and “EEE.E %” instead of the temperature and humidity values as shown in the simulation video below.

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.

CCS C code:

I made a simulation of the project using Proteus software, simulation file can be downloaded from the link below, use Proteus version 8.6 or higher to open it:
PIC18F4550 + SSD1306 + DHT22

simulation result is shown in this video:

Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

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