PIC18F46K22 with SSD1306 OLED and DHT11 sensor | mikroC Projects

This post shows how to interface PIC18F46K22 microcontroller with DHT11 digital humidity and temperature sensor.
The PIC18F46K22 MCU reads temperature (in °C) & humidity (in rH%) values from the DHT11 sensor and print them on SSD1306 OLED display (128×64 pixel).
MikroC PRO for PIC 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 PIC18F46K22 MCU with SSD1306 OLED display (I2C mode), take a look at the following project:
Interfacing PIC18F46K22 with SSD1306 OLED display | mikroC Projects

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • DHT11 (RHT01) humidity and temperature sensor   —->   datasheet
  • 4.7k ohm resistor
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 with SSD1306 OLED and DHT11 sensor circuit:
The image below shows project circuit diagram.

PIC18F46K22 DHT11 SSD1306 OLED circuit

All the grounded terminals are connected together.

The DHT11 sensor has 4 pins (from left to right):
Pin 1 is power supply pin, connected to circuit +5V
Pin 2: data output pin, connected to PIC18F46K22 RD5 pin (#28)
Pin 3: not connected pin
Pin 4: GND (ground), connected to circuit ground.

The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).
In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). The SDA1 pin of the MCU is connected to the SDA pin of the display and the SCL1 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 I2C slave address of the device is 0x7A.

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

PIC18F46K22 with SSD1306 OLED and DHT11 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 for the SSD1306 OLED display is required, its full name (with extension) is SSD1306OLED.C, download link is the one below:
SSD1306 OLED mikroC library

for more information about this driver, visit the following post:
SSD1306 OLED display library for mikroC compiler | mikroC Projects

after the download, add the driver file to mikroC project folder.

The connection of the DHT11 sensor data pin and the SSD1306 OED reset pin are defined in the C code as shown below:

Functions used in the code:
void Start_Signal(void): sends start signal to the DHT11 sensor.

char Check_Response(): checks response signal of the DHT11 sensor (after sending the start signal using the previous function), returns 1 if OK and 0 if error.

void Read_Data(unsigned short* dht_data): reads 1 byte from the DHT11 sensor which is saved to the variable dht_data.

If there is a problem with the DHT11 sensor (for example bad connection) the display will display Sensor (instead of the temperature value) Error (instead of the humidity value), and if there is a check sum error (for example due to wrong data reading) the display will display Checksum (instead of the temperature value) Error (instead of the humidity value).

Timer0 module is used to measure pulse widths (high and low), it’s configured to work as 8-bit timer/counter and prescaler = 4 (increment by 1 every 1 microsecond ==> clock input = 1MHz).
Timer0 clock input = Fosc/(4 x prescaler) = 16MHz/(4 x 4) = 1MHz.

Rest of code is described through comments.

Full mikroC code:

The following picture shows a protoboard circuit of the project:

PIC18F46K22 with DHT11 sensor and SSD1306 OLED

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