How to interface an SSD1306 OLED display with Arduino

How to interface an SSD1306 OLED display with Arduino

Hello, welcome back. In this tutorial, we will learn how to interface an SSD1306 OLED display with Arduino. Also in the previous tutorials, we did projects using the LCD display and the OLED display is smaller compared to the LCD display. Also, these are very thin and bright display types. We can buy these displays in different colors and sizes. Also in this OLED display, we can easily display any picture or character we want. The main advantage of these displays, they can be controlled by two wires in addition to the power wires if the display has I2C communication. When controlling the LCD display, a separate i2c module must be connected for I2c communication, but not for OLED displays. Keep reading.

How to interface an SSD1306 OLED display with Arduino

The structure of this OLED display

The OLED stands for Organic Light-Emitting Diode. This OLED display includes a driver controller chip called SSD1306 and it controls everything on the display. Also, this driver chip includes a 1KB Graphic display data RAM. This 1kb memory capacity is multiplied by the number of pages, segments, and bit size in the OLED display. That’s 128×64 resolution display contains 8 pages, 128 sections, and 8 bits. Then, multiplying these values gives 8192 bits or 1KB.

Also, by the SSD1306 driver control chip mentioned above, the display communicates with the microcontroller. For that, it includes both SPI and I2C communication methods, and the display used in this tutorial contains only the I2C method. Therefore, we can send the required data to the display using only two wires. It is a good advantage for us.

We can see these OLED displays in different sizes and colors. That is 128×64,128×32, single color, and dual color can be bought in the market. This tutorial uses a dual-color (Blue and Yellow) and 128×64 resolution OLED display. Also, this OLED display can be activated by giving us a 5v potential.

The PIN structure of this Display

OK, let’s learn how this OLED display works with Arduino. The required components are given below.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. For that, use the circuit diagram below.

How to interface an SSD1306 OLED display with Arduino

Step 3

Thirdly, let’s creates the program for this project. It is as follows.

/*OLED display tutorial with Arduino.
   https://srituhobby.com
*/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 srituhobby = Adafruit_SSD1306(128, 64, &Wire);

const unsigned char subscribe [] PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x78, 0x70, 0x00, 0x00, 0x70, 0x03, 0xFF, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x38, 0xF0, 0x00, 0x00, 0x70, 0x03, 0xFF, 0x1F, 0xFF, 0xFC, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x38, 0xE0, 0x00, 0x00, 0x70, 0x03, 0xFF, 0x1F, 0xFF, 0xFC, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x3C, 0xE0, 0x00, 0x00, 0x7E, 0x1F, 0xFF, 0x1F, 0xFF, 0xFC, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x1C, 0xE0, 0x00, 0x00, 0x7E, 0x1F, 0xFF, 0x1F, 0xFF, 0xFC, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x1D, 0xC7, 0xC3, 0x9C, 0x7E, 0x18, 0x43, 0x11, 0xF0, 0x7C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x1D, 0xCF, 0xE3, 0x9C, 0x7E, 0x18, 0x43, 0x00, 0xE0, 0x3C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0F, 0xCF, 0xE3, 0x9C, 0x7E, 0x18, 0x43, 0x00, 0xC0, 0x3C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0F, 0x9C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC6, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x0F, 0x9C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC6, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x9C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC6, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x1C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC0, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x1C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC0, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x1C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC0, 0x3C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x1C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC7, 0xFC, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x1C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC6, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x1C, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC6, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x0E, 0x73, 0x9C, 0x7E, 0x18, 0x43, 0x18, 0xC2, 0x1C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x0F, 0xE3, 0xFC, 0x7E, 0x18, 0x03, 0x00, 0xC0, 0x3C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x07, 0x07, 0xE3, 0xDC, 0x7E, 0x18, 0x03, 0x00, 0xE0, 0x3C, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x03, 0x03, 0x81, 0x88, 0x7E, 0x1C, 0x63, 0x31, 0xF0, 0xF8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0xCF, 0xFF, 0xFF, 0x1F, 0x9F, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0x06, 0x72, 0x06, 0x0E, 0x06, 0x0C, 0xC1, 0x81, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0x22, 0x72, 0x02, 0x4C, 0x26, 0x04, 0xC0, 0x81, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0x3E, 0x72, 0x72, 0x7C, 0xFE, 0x64, 0xCC, 0x9F, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0x0E, 0x72, 0x06, 0x18, 0xFE, 0x44, 0xC0, 0x81, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0x86, 0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, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

void setup() {
  srituhobby.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
  srituhobby.display();
  delay(5000);
  srituhobby.clearDisplay();
}

void loop() {
  byte a = 0;
  while (a <= 2) {
    srituhobby.setTextSize(2);
    srituhobby.setTextColor(WHITE);
    srituhobby.setCursor(40, 0);
    srituhobby.println("OLED");
    srituhobby.setCursor(20, 20);
    srituhobby.println("Display");
    srituhobby.setCursor(15, 40);
    srituhobby.println("Tutorial");
    srituhobby.display();
    srituhobby.startscrollright(0x00, 0x07);
    delay(1000);
    srituhobby.startscrollleft(0x00, 0x07);
    delay(1000);
    a++;

  }

  srituhobby.stopscroll();
  srituhobby.clearDisplay();
  srituhobby.drawBitmap(0, 0,  subscribe, 128, 64, WHITE);
  srituhobby.display();
  delay(5000);
  srituhobby.clearDisplay();

}

Code explanation

Firstly, library files are included.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Secondly, an object is created for this SSD1306 library. It includes the size of the display.

Adafruit_SSD1306 srituhobby = Adafruit_SSD1306(128, 64, &Wire);

Thirdly, the char array is created for the image. For that, follow the steps below.

  • First, choose your own picture and resize it using any other software(This size is depend on your OLED display). I’m used to photoshop software. After, save that picture to any location on your computer.
  • Then, open the Paint software and drag the drop the picture into the interface. To do this, use the picture we prepared earlier. After, save that picture as the bitmap file type.
  • Now, download and install the bitmap array generator software. For that, use this download link. Afterwards, run that software and import your picture to it.
  • Then, change the byte orientation as Horizontal. After, save the output and get the char array.
  • Now copy this array and paste it to the Arduino program array.

In the setup function,

void setup() {
//The SSD1306 library is begin
  srituhobby.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
//This code starts the display
  srituhobby.display();
  delay(5000);
//The display clear function
  srituhobby.clearDisplay();
}

In the loop function,

void loop() {
  byte a = 0;
  while (a <= 2) {
//Sets the text size
    srituhobby.setTextSize(2);
//Sets the text color
    srituhobby.setTextColor(WHITE);
//Sets the text position point
    srituhobby.setCursor(40, 0);
//Prints the text
    srituhobby.println("OLED");
    srituhobby.setCursor(20, 20);
    srituhobby.println("Display");
    srituhobby.setCursor(15, 40);
    srituhobby.println("Tutorial");
    srituhobby.display();
//This code sets the scrolls to the right
    srituhobby.startscrollright(0x00, 0x07);
    delay(1000);
//This code sets the scrolls to the left
    srituhobby.startscrollleft(0x00, 0x07);
    delay(1000);
    a++;

  }
//This code stops scrolling
  srituhobby.stopscroll();
//The display clear function
  srituhobby.clearDisplay();
//The image is shown using the array above
  srituhobby.drawBitmap(0, 0,  subscribe, 128, 64, WHITE);
//This code starts the display
  srituhobby.display();
  delay(5000);
  srituhobby.clearDisplay();

}

Step 4

Finally, select board and port. After, upload this code to the Arduino board.

OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to interface an SSD1306 OLED display with Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *