Menu Close

OLED Tutorial using Arduino

This tutorial explains how you can use OLED display  (Organic LED) using arduino.

In this tutorial you will learn

1. Display a string on OLED display. Ex Your name or any message

2. Display a variable on OLED display. Ex Value of any sensor like temperature.

3. Display a single pixel so you can make your own graphic

1. Introduction:

This tutorial explains how to operate OLED display (Organic LED) using Arduino. You will learn to display a string like your name or a pre-defined message also after completing this tutorial you will be able to display any variable or pixel as per your requirement.

2. Hardware Required:

  1. Arduino Uno Board (Compatible or Original, both works same) – 1 Nos  BUY NOW
  2. 128X64 4 pin OLED Display SSD1306 IC based – 1Nos – BUY NOW
  3. Bread Board – 1 Nos – Buy NOW
  4. Base Plate – 1 Nos – BUY NOW
  5. Jumper Wire Male to male – 4 Nos – BUY NOW
  6. USB cable A to B Type – 1 Nos – BUY NOW

3. Connections and Setup:

4. Software Setup:

Software setup is divided into four steps to make you understand the whole process more easily

Step 1: Install Latest Arduino IDE. Click here to download

Step 2: Install Adafruit OLED SSD1306 and GFX Library.

After successfully installing Arduino IDE we have to install Adafruit Library for OLED SSD1360 and GFX Library. The library is inbuilt in Arduino IDE, we just have to install them. Follow stepwise instructions to install the library

1. First Open Arduino IDE and Click on Sketch button > Include Library > Manage Libraries (Steps shown in Picture)

2. Library Manager window will be open, you will find search tab in Library Manager, in that window search for OLED and in results you will find library “Adafruit SSD1306 by Adafruit“. Click to select library and then hit install

3. Similarly, search for GFX and in the result, you will find “Adafruit GFXLibrary by Adafruit“. Install this library also. After installation both Library will be shown as installed. Till now we have completed two easy steps.

Step 3: Select OLED in library

After installing all library we have to select OLED in the library because this library is for 128X64 as well as 128X32 and by default 128X32 OLED is selected so

1. We have to locate a file “Adafruit_SSD1306.h” and make changes in this file.

Location of file is : Documents > Arduino > Libraries > Adafruit_SSD1306

Open file name “Adafruit_SSD1306.h” in wordpad

2. On opening, you have to find three line in the program 

// #define SSD1306_128_64 //Make 128X64 OLED enable by un-commenting it 
   #define SSD1306_128_32 //Make 128X32 OLED disable by commenting it 
// #define SSD1306_96_16

Result

    #define SSD1306_128_64 
// #define SSD1306_128_32 
// #define SSD1306_96_16

Step 4: Detect the I2C address of the OLED device

1. To detect the I2C address of display, download the program Provided below and upload to Arduino.

Click Here to download full code of I2C detector

// --------------------------------------
// Robo India Tutorial
// i2c_scanner
// www.roboindia.com
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
// --------------------------------------
 
#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             //  wait for serial monitor
  Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}


2. After Uploading code to Arduino Uno, run serial monitor (Ctrl + Shift + M) to see your device address. In our case address is 0x3C

Step 5: Update I2C address and Run final program in Arduino Uno

1. Download Robo_India_OLED_SSD1306_128X64 full program here

/*********************************************************************

This is an example for our Monochrome OLEDs based on SSD1306 drivers

Buy OLED Here :
  ------> http://www.roboindia.com

We are using Adafruit Library 

*********************************************************************/

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

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!"); // If this is the error that means
#endif
int a=1; // global variable

void setup()   {  
  Serial.begin(9600);
 
  // 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 128x64)
  // Check your I2C address and enter it here, in Our case address is 0x3C
  
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
 

  // Clear the buffer.
  display.clearDisplay();

  
  display.setTextSize(2); // You can select text size ex- 1, 2, 3 and so on 
  display.setTextColor(WHITE); // This will display Bright character on Black background
  display.setCursor(5,0); // You are setting the cursor as per your requirement. In short its the co-ordinates in display from where data will display
  display.println("Robo India"); // String which you are printing

  display.setTextSize(1);
  display.setTextColor(BLACK, WHITE); // Here you print 'Black character' on 'Bright Background'
  display.setCursor(12,25);
  display.println("www.roboindia.com");
 
  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(13,44);
  display.println("ROINCO");
  
  display.display(); // this command will display all the data which is in buffer
  delay(2000);
  
  display.clearDisplay(); // clear the buffer
  
}

void loop() { 
  display.clearDisplay();
  if(a>10){a=0;} // This loop will make counter 0 again after reaching maximum value 10
  display.drawPixel(127, 63, WHITE); // This command will help you to print a pixel on display , its the last pixel, coordinate 127,63
  display.drawPixel(0, 0, WHITE); // pixel with 0,0 coordinate
  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(13,44);
  display.println("ROINCO");
  display.setCursor(10,0);
  display.setTextSize(2);
  display.println(a); // printing a variable
  a++; // incriment in variable a
  display.display();
  delay(1000);
}

2. Open program in Arduino IDE and update the I2C address of OLED in program

Locate code  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); in program. Here 0x3C is I2C address of OLED used in the tutorial.

Update your OLED I2C address in place of 0x3C and upload the program

All done, upload your code in Arduino and enjoy the new interaction with your OLED

Leave a Reply