Interfacing OLED I2C Display with Arduino

Interfacing OLED I2C Display with Arduino
In this tutorial we want to learn how to interface OLED I2C display with Arduino using the Adafruit Libraries.

What is OLED display?

OLED is short for Organic Light-Emitting Diode. This display doesn’t require backlight, which results in a very nice contrast in dark environments. Additionally, its pixels consume energy only when they are on, so the OLED display consumes less power when compared with other displays. The display that we’ll use in this tutorial is the SSD1306 model which is  a monocolor, 0.96-inch display with 128×32 pixels.This type of module is also  available in following sizes 128×64, 96×16, 64×48 and 64×32.
These models use  I2C communication protocol and as such need only four wires when connecting to the Arduino.

Connecting the OLED to Arduino.

An important note about I2C enabled devices is that the way you should connect them to Arduino are all the same. This is because Arduino runs its I2C communication only on specific pins. In this tutorial I’m using Arduino Uno. The Arduino Uno uses pin A5 as SCL and A4 as SDA
oled display 128x32
The pins are wired to the Arduino board as follows:
  • VCC to 5V
  • GND to Ground
  • SCL to pin A5
  • SDA to pin A4
If you’re using a different Arduino board, make sure you check the correct I2C pins:
  • Nano: SDA (A4); SCL(A5);
  • MEGA: SDA (20); SCL(21);
  • Leonardo: SDA (20); SCL(21);
  • oled_i2c_schematic

    Getting the Address of the OLED module

    Before using any I2C enabled device, you need to have the address of the module. In order to do so, after wiring up the module to your Arduino, you should just upload the I2C Scanner Code. This code uses the Wire library which is a library included with Arduino IDE that handles I2C communication. It scans connected I2C devices and sends their address via serial port to your computer. So you can be able to see the address via Serial Monitor tool in Arduino IDE. For example in this case the address of the OLED module we are using is 0X3C.
    oled module adddress

    Installing necessary Libraries.

    Before writing any code for interfacing the OLED I2C display with Arduino, you need to install the “adafruit_GFX.h” and the “adafruit_SSD1306.h” libraries.
    The adafruit GFX library can be downloaded from here . Unzip the .zip folder you have downloaded and rename the  Adafruit-GFX-Library-master folder to Adafruit_GFX_Library (take note of the change from “-” to “_”)

    • Move the Adafruit_GFX_Library folder to your Arduino IDE installation libraries folder
    The adafruit_SSD1306 library can be downloaded from here . Unzip the .zip folder you have downloaded and rename the Adafruit-GFX-Library-master folder to Adafruit_SSD1306  
  • Move the Adafruit_SSD1306 folder to your Arduino IDE installation libraries folder
  • Finally, re-open your Arduino IDE

    Code for running the OLED display.

    Some of the important functions included in the above libraries that are worth mastering if you are to know the basic use of oleds with Arduino include:
  • display.clearDisplay(): Clears the display completely
  • display.display(): Displaying data or images on the screen
  • display.setCursor(): to specify the coordinates of the display where we’re going to display the text.
  • display.print(): accepts the text as an string and tries to display it
  • display.write(): for displaying a single character and it accepts a character code as an uint8_t type and displays the character corresponding to that code as a string.
  • display.setTextSize(): for controlling text size.
  • display.drawLine(startX, startY, endX, endY, color): for drawing lines.For further reference on how to use this function click here.
  • display.drawRect(upperLeftX, upperLeftY, width, height, color):
  • for drawing rectangles.
  • display.drawPixel(pixelX, pixelY, color): for drawing a point
  • display.drawBitmap(): for drawing images
  • The code below shows  the basic use of the above functions.
    #include <SPI.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 32 // OLED display height, in pixels
    #define OLED_RESET 4
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
    void setup()   {  
      // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
      display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
      display.display(); // this command will display all the data which is in buffer
    void loop() { 
      display.println("HELLO WORLD");
    // drawing diagonal lines
      display.drawLine(0,0,display.width() - 1, display.height() - 1, WHITE);
      display.drawLine(display.width() - 1,0,0, display.height() - 1, WHITE);
    //drawing rectangles
      display.drawRect(100, 10, 20, 20, WHITE);
      display.fillRect(10, 10, 45, 15, WHITE);
      display.drawRoundRect(60, 20, 35, 35, 8, WHITE);
    //drawing circles
      display.drawCircle(30, 15, 15, WHITE);
      display.fillCircle(25, 10, 2, WHITE);
      display.fillCircle(35, 10, 2, WHITE);
    //drawing points
      display.drawPixel(20, 35, WHITE);
      display.drawPixel(45, 12, WHITE);
      display.drawPixel(120, 59, WHITE);
      display.drawPixel(97, 20, WHITE);
      display.drawPixel(35, 36, WHITE);
      display.drawPixel(72, 19, WHITE);
      display.drawPixel(90, 7, WHITE);
      display.drawPixel(11, 29, WHITE);
      display.drawPixel(57, 42, WHITE);
      display.drawPixel(69, 34,  WHITE);
      display.drawPixel(108, 12, WHITE);

    Leave a Reply

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

    Back to top