Interfacing PIC microcontroller with SSD1306 OLED | mikroC Projects

This is an example shows how to write texts on SSD1306 OLED display (128×64 pixel) using PIC16F887 microcontroller. In this project the SSD1306 OLED is configured to work in I2C mode, make sure that your SSD1306 OLED display is configured to work in I2C mode, some displays need jumper placing or some soldering.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.
A small video shows Proteus simulation of this project at the end of the post.

This example is for interfacing PIC microcontroller devices (with limited RAM) with SSD1306 OLED display.

The SSD1306 OLED display communicates with the master device over I2C mode, SPI mode or 8-bit parallel mode.
Generally the SSD1306 OLED requires a RAM buffer with all screen data, for example if we’ve a 128×64 Pixel display then we’ve to use a buffer of 1024 bytes (128×64/8). The problem is when we want to write a single pixel we’ve to overwrite another 7 pixels, therefore we need to know the status of the other 7 pixels.
Another problem with this type of displays (the SSD1306 controller chip) is it doesn’t allow any reading from its RAM when it works in serial mode (I2C or SPI).

In this example the SSD1306 works in I2C mode and the PIC16F887 has a limited RAM of 368 bytes, that means using a buffer of 1024 bytes is not possible.

I wrote a small driver for the SSD1306 OLED in order to be able to write texts, this driver works with SSD1306 displays with resolution of 128×64, 128×32 and 96×16 pixel, it splits the 128×64 pixel display into 21 columns and 8 rows, the 128×32 pixel into 21 columns and 4 rows, the 96×16 pixel into 16 columns and 2 rows. So in a display of 128×64 pixel we can write up to 168 character (21×8). The driver has a built-in font of 5×7 pixel.

SSD1306 OLED display driver functions:
This is a list of all user functions of the driver.

SSD1306_Init(vccstate, i2caddr); initializes the display, i2caddr is the display address, the default address is SSD1306_I2C_ADDRESS which is equal to 0x7A.

SSD1306_StartScrollRight(start, stop); scroll right
SSD1306_StartScrollLeft(start, stop); scroll left
SSD1306_StartScrollDiagRight(start, stop); scroll diagonal right
SSD1306_StartScrollDiagLeft(start, stop); scroll diagonal left
SSD1306_StopScroll(); stop scrolling

SSD1306_ClearDisplay(); clears the display.
SSD1306_FillScreen(); fills the whole screen.
SSD1306_Dim(dim); dim the display, dim can be 1  or 0 .
SSD1306_InvertDisplay(i); inverts the display, i can be 1 or 0.

SSD1306_GotoXY(x, y); move cursor to position (x, y).
SSD1306_PutC(c); draws a character c on screen position (x, y).
SSD1306_PutCustomC(char *c); draws a custom character c on the screen. c dimension should be 5×7.
SSD1306_Print(char *s). prints a text (string) on screen position (x, y). It draws the string characters using the function SSD1306_PutC until it faces string terminator (‘\0’).

The library supports 3 types of the SSD1306 OLED display depending on the screen resolution, these types are: 128×64 pixel, 128×32 pixel and 96×16 pixel. The default type is: 128×64 pixel.
If you want to use it with the 128×32 pixel type just define it in the main code as:
#define SSD1306_128_32

and if the 96×16 pixel type is used, the definition becomes:
#define SSD1306_96_16

Note that the library doesn’t support defining both previous types at once and compiling a code with the two definitions (#define SSD1306_128_32 and #define SSD1306_96_16) will give an error.

By default the library uses hardware I2C1 module of the microcontroller unless the following line is defined which makes it uses hardware I2C2 module.
#define SSD1306_I2C2

Interfacing PIC16F887 with SSD1306 OLED display:

PIC16F887 microcontroller with 0.96" SSD1306 OLED display

Hardware Required:

  • PIC16F887 microcontroller
  • SSD1306 OLED display
  • 5V source
  • Breadboard
  • Jumper wires

The Circuit:
The following image shows example circuit schematic diagram.

PIC16F887 microcontroller with SSD1306 OLED display - mikroC SSD1306 OLED

(All grounded terminals are connected together)

The PIC16F887 microcontroller has one hardware I2C module (MSSP module) with SDA on pin RC4 (#23) and SCL on pin RC3 (#18). The SDA pin of the MCU is connected to the SDA pin of the display and the SCL 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 the I2C slave address of the display is 0x7A.

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

The 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 (& library) for the SSD1306 OLED display is required, it’s full name is SSD1306.C, download link is below:
SSD1306 OLED display driver for mikroC compiler

after the download, add the driver file (SSD1306.C) to project folder.

Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700

The simulation of this project using Proteus should be as shown in the following video where PIC16F877A MCU is used:

Other examples where PIC16F887 microcontroller and SSD1306 OLED display are used:
PIC MCU with SSD1306 OLED and DHT11 sensor | mikroC Projects
PIC MCU with SSD1306 OLED and DHT22 sensor | mikroC Projects
PIC16F887 Interfacing with SSD1306 and DS1307 | mikroC Projects
PIC MCU with SSD1306 OLED and DS3231 RTC | mikroC Projects
Interface PIC MCU with DS18B20 sensor and SSD1306 | mikroC Projects

Leave a Reply

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