PIC16F877A with DHT22 (AM2302) sensor and SSD1306 OLED display

This small post shows how to connect PIC16F877A microcontroller with SSD1306 OLED display 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 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 or I2C mode) such as soldering, placing jumpers …

Related Projects:
The following topics may contain some useful information about the current project.
Interfacing PIC16F877A with SSD1306 OLED display
Interfacing PIC16F877A with DHT22 sensor
PIC16F877A with DHT11 sensor and SSD1306 OLED

Hardware Required:

  • PIC16F877A microcontroller
  • SSD1306 OLED display (128×64 Pixel)
  • DHT22 sensor (or RHT03, AM2302)
  • 8 MHz crystal oscillator
  • 2 x 22pF ceramic capacitor
  • 10k ohm resistor
  • 4.7k ohm resistor
  • 5V power source
  • Breadboard
  • Jumper wires

PIC16F877A SSD1306 OLED DHT22 (AM2302, RHT03)

The Circuit:
The following image shows project circuit schematic diagram.

PIC16F877A DHT22 SSD1306 OLED display

(All grounded terminal are connected together)

The PIC16F877A microcontroller has one hardware I2C 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.

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 SSD1306.C, its download link is below:
SSD1306 OLED display driver

after the download, add the driver file to the project folder or CCS C compiler drivers 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.

CCS C code:

PIC16F877A + SSD1306 OLED + DHT22 sensor Proteus simulation:
Proteus simulation file download link is below, use it with version 8.6 or higher:
PIC16F877A + SSD1306 OLED + DHT22

The following video shows my simulation result (simulation circuit is not the same as the hardware circuit, hardware circuit diagram is shown above!):


Leave a Reply

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