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:


  1. I’m interested in this project but i’m looking at the SSD1306 OLED display on ebay and some have 4 pins and others have 7 pins (like the one shown on this page). If i buy the 4 pin version, will this still work?

    1. The 4-pin SSD1306 OLED module also should work.
      If it didn’t work with the default I2C address which is 0x7A try with 0x78.
      So the function:
      SSD1306_Begin(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);
      SSD1306_Begin(SSD1306_SWITCHCAPVCC, 0x78);

  2. Hi, I have PIC18f4520 and ssd1308. would this program work for me and what changes, hardware and software, I would need to make if you can plz elaborate? thx

  3. Hi, thank you for sharing this program. I got the same PIC MCU and the oled display and have interconnected them according to the schematic given in this post. I have been trying to compile this program in Mikroc for sometime but I get stopped at one or another error. This time I got ‘undefined identifier I2C1_Start’. Can someone please hellp with error or kindly put the programs in a mikroc project so its easier to run it and learn from it? I would like to be able to compile and run it. Thank you.

Leave a Reply

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