PIC18F46K22 with SSD1306 OLED Display – SPI 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 SPI mode.
CCS C Compiler is used in this project.

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

The SSD1306 display contains a driver chip with the same name (SSD1306), it can communicate with the master device (microcontroller, microprocessor …) over I2C protocol, SPI protocol or 8-bit parallel protocol.
The I2C protocol needs only 2 lines: SDA (serial data) and SCK (serial clock), an additional line is required which is a reset line (RST). The SPI protocol is faster than the I2C protocol but it uses more pins: SCK, SDA, CS (chip select: active low), D/C (data/command) and a rest pin (RST).

The SSD1306 OLED which I’m going to use in this project is shown below (back view), the default mode is SPI which can be changed to I2C by removing the resistors R3 and placing the resistors R1 & R8 (as written on the board). Note that resistance of R1 = R3 = R8 = 0 ohm.

SSD1306 OLED 128x64

The following project shows how to use the SSD1306 OLED display in I2C mode:
PIC18F46K22 with SSD1306 OLED Display – I2C Mode Example

Hardware Required:

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

PIC18F46K22 with SSD1306 OLED display circuit (4-wire SPI 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 SPI mode circuit

All the grounded terminals are connected together.

The PIC18F46K22 microcontroller has 2 hardware SPI modules (MSSP1 and MSSP2 modules).
In this project SPI1 module is used with SCK1 on pin RC3 (#18) and SDO1 (MOSI) on pin RC5 (#24). SCK1 and SDO1 pins of the PIC18F46K22 MCU are respectively connected to SCK and SDA pins of the SSD1306 OLED display module.

The other pins of the display module which are RES, DC and CS are respectively connected to PIC18F46K22 pins RD4 (#27), RD5 (#28) and RD6 (#29).

SCK: Serial Clock.
SDO: Serial Data-Out, synonym for MOSI.
MOSI: Master-Out Slave-In.

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 (4-wire SPI 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 SSD1306 OLED library is initialized to communicate with the device via SPI protocol using the following line:

The display is connected to PIC18F46K22 microcontroller SPI1 module pins (SCK1 and SDO1) which is initialized with the following line (clock @ 4MHz):

The other pins: RST, CS and DC are defined as shown below:

The display is initialized as shown below:

Rest of code is described through comments.

Full CCS C code:

Protoboard hardware circuit:

PIC18F46K22 with SSD1306 OLED SPI mode interfacing

Proteus simulation:
This example works with Proteus simulation software since it (Proteus) contains the SSD1306 OLED library. The video below shows the simulation result:

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

Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

2 thoughts on “PIC18F46K22 with SSD1306 OLED Display – SPI Mode Example”

  1. Hello, first of all thank you for your great effort in making the SSD1306 driver easier to understand and use.
    I’m using a MCOT128064SV-WM (Midas OLED 128×64) display and I’m having a problem with it. Although I can display the Adafruit logo over the entire area, however, attempts to place pixels, lines, numbers, characters result in displaying only the first 16 lines from the top. Do you know what I’m doing wrong? Can this be linked to the fact that the display uses SSD1309 and not SSD1306? Thanks

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