PIC MCU with SSD1306 OLED – I2C Mode Example | mikroC Projects

This post shows how to interface PIC18F46K22 microcontroller with SSD1306 OLED display (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.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.

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

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 …
In the I2C mode there are two main 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 module has a reset pin.

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

To see how to use the SSD1306 OLED display module in SPI mode, visit this post:
PIC MCU with SSD1306 OLED – SPI Mode Example | mikroC Projects

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 @ 16 MHz, MCLR pin is configured as an input pin.

PIC18F46K22 with SSD1306 OLED display C code (I2C mode):
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.6.0.

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 mikroC compiler

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

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

In this example, the SSD1306 OLED display is connected to hardware I2C1 (MSSP1 module) of the PIC18F46K22 microcontroller. This is the default configuration, but hardware I2C2 module (MSSP2) or even mikroC software I2C library (built-in) can be used.

To use mikroC software I2C library just define the following line (before #include “SSD1306.c”):
#define SSD1306_SOFT_I2C
In software I2C we’ve to configure the pins used (SCL and SDA) by mikroC software I2C library as shown in the help file.
Also the configuration should be initialized in the main code (always before initializing the display) with this line:
Soft_I2C_Init();
The clock frequency of the software I2C library is fixed to 20 kHz (slow!).

If the display module is connected to hardware I2C2 (MSSP2 module) then we’ve to define this line:
#define SSD1306_HARD_I2C2
and hardware I2C2 module must be initialized before the initialization of the SSD1306 OLED display (for example I2C2_Init(400000);).

The SSD1306 OLED display library supports three types depending on screen size (number of pixels): 128×64, 128×32 and 96×16. The default one is 128×64.
If the display used is 128×32 then the line below has to be added before #include “SSD1306.c”:
#define SSD1306_128_32
and if the display used is 96×16 then use this definition:
#define SSD1306_96_16

Hints:
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 (MSSP1) of the PIC18F46K22 which is initialized 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 mikroC 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


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

7 thoughts on “PIC MCU with SSD1306 OLED – I2C Mode Example | mikroC Projects”

  1. Not work, 1st of all is uncapable to include ssd1306oled.c

    and after this, a lot of errors, wat a loss time omg

    27 304 error: Can’t open include file “ssd1306oled.c” #include
    61 304 error: Can’t open include file “ssd1306oled.c” #include 2 errors in preprocessor.

  2. MikroC Pro v7.2.0

    22 undeclared identifiers

    Copied all files required, set all PIC18F46K22 parameters, does not compile.

  3. Hi, I was trying to use your code to run a SSD1306 OLED with a PIC microcontroller, but after reviewing your code and trying to build it I can’t seem to find the functions for SSD1306_I2C_Start(), SSD1306_SSD_Write and SSD1306_I2C_Stop() used in SSD1306.c and also referenced in SS1306.h. Are we suppose to build these functions ourselves or could you please share a link to your code for the I2C codes? Thank you!

  4. The File GFX_Library.c shows alot of errors. It does not work with MikroC., One day trying but not compiling at all.

      1. I am getting similar errors in GFX_Library.c, with ‘undeclared identifier abs’ and others. I am using MikroC 6.6.2. Can you plz help what I am missing?

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