128×64 SSD1306 OLED interface with Arduino (SPI)

Visual interface plays an important role in any embedded or electronics design. It unveils the seamless capabilities of your design by providing visual monitoring. In market, there are varieties of displays are available as per requirement. In earlier tutorial we learned how to interface 16×2 LCD with Arduino. Here in this, we will learn about OLED & it’s interface with Arduino.

OLED-organic light-emitting diode is a type of display which is used in devices such as television screens, computer monitors, portable systems such as smartphones, handheld game consoles, smart bands and PDAs. In OLED, the emissive electroluminescent layer is a film of organic compound that emits light in response to an electric current. This organic layer is situated between two electrodes; typically, at least one of these electrodes is transparent.

An OLED display works without a backlight because it emits visible light. Thus, it can display deep black levels and can be thinner and lighter than a liquid crystal display (LCD). In low ambient light conditions (such as a dark room), an OLED screen can achieve a higher contrast ratio than an LCD, regardless of whether the LCD uses cold cathode fluorescent lamps or an LED backlight. OLED displays are made in the same way as LCDs, but after TFT (for active matrix displays), addressable grid (for passive matrix displays) or ITO segment (for segment displays) formation, the display is coated with hole injection, transport and blocking layers, as well with electroluminescent material after the 2 first layers, after which ITO or metal may be applied again as a cathode and later the entire stack of materials is encapsulated. OLEDs have large fields of view, about 170 degrees. The TFT layer, addressable grid or ITO segments serve as or are connected to the anode, which may be made of ITO or metal. OLEDs can be made flexible and transparent, with transparent displays being used in smartphones with optical fingerprint scanners and flexible displays being used in foldable smartphones

Most common OLED available in market:

Here we will see 128×64 SSD1306 Monochrome OLED (SPI) interface with Arduino. It has 128×64 pixels & SSD1306 driver. It has >160° visual angle, operates on 3.3V ~ 6V & usually available in blue, white & dual color (some upper portion with different color). This OLED has SPI pins & it is mentioned below:

Sr. Pin Name Also known as Function
1 GND Ground   Ground pin of OLED
2 Vdd 5V, 3v3, Vcc   Power pin (3.3V to 5V)
3 SCK D0, CLK, SCL   Works as clock pin, It is used for both SPI & I2C
4 SDA MOSI, D1   Data pin of OLED, It is used for both SPI & I2C
5 RES RESET, RST   To reset OLED, part of SPI configuration
6 DC A0   Data Command pin, part of SPI configuration
7 CS Chip Select   Useful when we have multiple SPI module on same controller

Connection with Arduino

Arduino Pin OLED Pin
3.3V Vdd, Vcc, 3v3
Ground GND
9 SDA, D1, MOSI
10 SCK, D0, SCL,CLK
11 DC, A0
12 CS, Chip Select
13 RES, RST, RESET

Programming

To getting started with this OLED, you will need libraries for it, here are two libraries recommended:

While using this OLED for the first time, it is recommended to test OLED & connections using examples in Arduino IDE. Go to File > Examples > Adafruit SSD1306 and select  ssd1306_128x64_spi example Upload the example and you can see line, drawing, text, scroll, invert display & snowflakes animation properly; if it is working fine means your connection & OLED both are fine.

Understand functions of this library

The library has various function for different output like start display, clear display, invert display, write text at particular location, show image & custom icons etc.

  • Start display & clear display:

OLED is like a writing pad, you have to inform it to display what you prepared. display.display() function is necessary after you cook something in program which is going to be display in next sequence.

If you write something again in same sketch to display, it will overwrite if you don’t clear the previous thing you displayed. display.clearDisplay() is required for the same.

  • Display a text string:

To display text or text string, you can use above lines, set x & y axis accordingly.

  • Display a value (character variable):

To display any character variable, for example temperature reading or any value, you can use above lines.

  • Draw some symbols like triangle, rectangle, circle:

Use any of the above functions to draw the element you want.

  • To invert color of whole display:

Using display.invertDisplay(true); function you can invert the display color (Black to white & vice versa), it will keep all image/text in inverse color until your code executes  display.invertDisplay(false);. 

Display image on OLED

To display an image on OLED display, the bitmap data must be stored in the program memory in form of PROMGMEM directive. In easy words, we have to instruct the OLED display what to do with each pixel by passing it a sequence or values from an array as shown above. This array will contain the bitmap data of the image. For this, you have to convert your B & W images into monochrome bitmap format with 128×64 pixels dimension. After that, open Image2cpp tool by Jasper van Loenen. This tool converts an image into a bit map values and load them into the array. Upload your 128×64 monochrome bitmap image there, check preview, review settings & check following setting;

Select code output format as ‘Arduino Code’ as shown in image, click ‘Generate code’ button. You will get a character set in window below ‘Generate code’ button. Copy it,  and paste in this code as character array;

Image display code

Select your Arduino board & port in Arduino IDE, upload the sketch & bingooo..!!! You will see the image on OLED display.

You can merge above mentioned functions and combine image with text & shapes. You can download following examples for better understanding.

Demo examples:

Here is one demo of OLED & Arduino:

 

I hope you will learn to drive your OLED display from this tutorial & it will be helpful to add visual enhancement in your DIY stuff. You can make various projects using such OLED where you need to display text/numbers & images, in future such projects will be uploaded in #BeatYourBit website. If you like this tutorial, share it with your friends & any Arduino newbie you know. Give your feedback about this tutorial in Contact, Your feedback will be appreciated.

Like/Follow @beatyourbit on FacebookTwitterInstagram & Telegram for latest updates.

Leave a Reply

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