PIC18F46K22 with SSD1306 OLED Display – I2C Mode Example

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.
This topic shows how to use the SSD1306 OLED in I2C mode.
CCS C Compiler is used in this project.

SPI: Serial Peripheral Interface.
I2C (or IIC): Inter-Integrated Circuit.

Last time I made a simple interfacing of the PIC18F46K22 MCU and the SSD1306 OLED display which was used in SPI mode, project link is below:
PIC18F46K22 with SSD1306 OLED Display – SPI Mode Example

The SSD1306 OLED display 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 and I2C mode) such as soldering, placing jumpers …

Hardware Required:

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

PIC18F46K22 with SSD1306 OLED I2C mode interfacing

PIC18F46K22 with SSD1306 OLED display circuit (I2C mode):
Example circuit diagram is shown below.

The SSD1306 OLED display module shown in the circuit diagram has 7 pins (from left to right):
GND, VCC, SCK (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and CS (chip select).

The SSD1306 display board is supplied with 5V where GND is connected to circuit ground and VCC is connected to +5V.

PIC18F46K22 SSD1306 OLED display I2C circuit

All the 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 (RES) is connected to pin RD4 (#27) of the microcontroller.
Chip select pin of the display (CS) is not connected (not used in I2C mode).

The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x7A. If the DC pin is connected to ground (GND) then the I2C slave address becomes 0x78.

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

PIC18F46K22 with SSD1306 OLED display C code (I2C mode):
The following C code is for CCS C compiler, it was tested with versions 5.051 and 5.083.

To be able to compile project C code with no error, 2 libraries are required:
The first library is a driver for the SSD1306 OLED display, its full name (with extension) is SSD1306.c, download link is below:
SSD1306 OLED display library for CCS C compiler

The second library is graphics library, its full name is GFX_Library.c, download link is the one below:
Graphics library for CCS C compiler

after the download of the 2 library files, add both of them to the project folder.

The 2 library files are included in the main code as shown below:

The reset pin of the display is connected to pin RD4 and it’s defined as (if the display module doesn’t have a reset pin then there is no need to define it):

The SSD1306 display is connected to hardware I2C module of the PIC18F46K22 which is initialized using the #use function (CCS C built-in function) with clock frequency of 400kHz:

The SSD1306 OLED display is initialized with I2C address 0x7A (because DC pin is connected to VDD):

Rest of code is described through comments.

Full CCS C code:

Protoboard circuit test is shown in the following video:

Proteus simulation:
The video below shows Proteus simulation of this example, it gave approximately the same result as the real hardware circuit:

Proteus simulation file download link is below, use version 8.6 or higher to open it:
PIC18F46K22 + SSD1306 OLED I2C mode

Leave a Reply

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