Interfacing PIC18F46K22 with SSD1306 OLED display | mikroC Projects

This post shows how to interface PIC18F46K22 microcontroller with SSD1306 OLED (128×64 pixel).
The SSD1306 is a monochrome display which means it has only one color (white, blue, yellow …).
This display communicates with the master device over I2C mode, SPI mode or 8-bit parallel mode. In this project I’m going to use the I2C mode.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.

In the I2C mode there are two lines between the microcontroller and the SSD1306 OLED display board: SDA (serial data) and SCL (serial clock). An additional pin for hardware reset is required if the display has a reset pin.
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.

PIC18F46K22 MCU with SSD1306 OLED display

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • 5V source
  • Breadboard
  • Jumper wires

Interfacing PIC18F46K22 with SSD1306 OLED display circuit:
The image below shows example circuit diagram.

PIC18F46K22 SSD1306 OLED display I2C circuit

(All grounded terminals are connected together)

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 display is 0x7A.

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

Interfacing PIC18F46K22 with SSD1306 OLED display 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, it’s name is SSD1306OLED.C, for more information about this driver, visit the following post:
SSD1306 OLED display library for mikroC compiler | mikroC Projects

It also can be downloaded also from the link below:
SSD1306 OLED mikroC library

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

The example code is a modified version of an Adafruit example for a 128×64 pixel display.

Full mikroC code:
Configuration words (for PIC18F46K22 MCU):
CONFIG1H = 0x0028
CONFIG2L = 0x0018
CONFIG2H = 0x003C
CONFIG3H = 0x0037
CONFIG4L = 0x0081
CONFIG5L = 0x000F
CONFIG5H = 0x00C0
CONFIG6L = 0x000F
CONFIG6H = 0x00E0
CONFIG7L = 0x000F
CONFIG7H = 0x0040

The result of this project should be as shown in the following video (except the first image) where PIC18F4550 microcontroller is used:

Leave a Reply

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