ACROBOTIC Industries ACROBOTIC Industries

ESP8266: Using I2C OLED Screens (SSD1306)


Time to complete: 10–15min; Level of difficulty: Beginner

In this activity we’ll use an ESP8266 ESP-12E Development Board to display graphical data on a SSD1306-powered, 0.96in OLED screen connected via I2C.

List of Materials

Obtaining the Code

For this activity, we'll be using our SSD1306 Arduino Library to upload example programs onto our ESP8266 Development Board; the library's source code can be found in our Github repositories:

We'll use a simple process to install this library and make it available for use in our example programs below.

Installing the Library

There are a few different ways to install a library in the Arduino IDE.  For detailed, step-by-step instructions on all the different ways to do so please visit our tutorial.  The simplest method is to use the built-in library manager in the Arduino IDE. It can be accessed via the menu option Sketch → Include Library → Manage Libraries…

In the search field we can enter "ACROBOTIC OLED" and install the resulting option:

Install ACROBOTIC SSD1306 Library in Arduino IDE

Before diving into the code, let's review a bit the operation of OLED displays, and how graphics are formed.

How OLED Screens Work

For a detailed explanation about how SSD1306-powered OLED screens work, please visit our neat tutorial!

Wiring: Hello OLED

For trying out the OLED display we'll need to source some power and connect the communication lines to it.  As mentioned, we'll be using I2C to send data from an ESP8266 Development Board to be displayed on the screen.  As with any other I2C device this will require a few wires, and 2 resistors for pulling up the SDA and SCL lines (we have a great tutorial on pull-up/pull-down resistors!). 

The wiring is straight forward, we bring out 3V3 and GND from the ES8266 Development Board to the vertical rails, and use them to source power (VCC) and ground (GND) to the OLED screen.  We then connect SDA to the pin labeled D2 (GPIO4) and SCL to D1 (GPIO5) as the default pins for the I2C library we'll use called "Wire" (included with the Arudino IDE) uses pins 4 and 5 as the default for this protocol.

Wiring I2C OLED Display to ESP8266 Development Board

Code Walkthrough: Hello OLED

For our first demo, we will run the "HelloOLED" example that's included with the OLED libary.  This program can be accessed by using the menu option File → Examples → ACROBOTIC SSD1306 → HelloOLED.  In this program all the functionality is included in the setup() function.  We start by initializing the I2C communication using the default SDA (GPIO4) and SCL (GPIO5) pins as defined by the Arduino IDE, this matches our wiring as seen in the previous step.


We then initialize the SSD1306 OLED driver with the most common settings (e.g., brightness level, light-on-dark pixels, scrolling options), these can be changed individually using different function calls in our library.

  oled.init();                      // Initialze SSD1306 OLED display

The next step is to ensure that our screen is clear of any graphics.

  oled.clearDisplay();              // Clear screen

We can then start placing characters on our screen! To do so, we simply set the cursor to the correct line and character column using the setTextXY() function.  Note that the function assumes we're using the default 8-by-8 font (8 pixels wide by 8 pixels tall), thus with our 64-vertical by 128-horizontal resolution, there are a total of 8 lines and 16 character spaces we can use!

  oled.setTextXY(0,0);              // Set cursor position, start of line 0
  oled.setTextXY(1,0);              // Set cursor position, start of line 1
  oled.setTextXY(2,0);              // Set cursor position, start of line 2
  oled.setTextXY(2,10);             // Set cursor position, line 2 10th character

We notice that if we set the writign position to the same line as shown above, we can continue writing without a problem!  Now let's see how to add basic graphics to our diplay!

Code Walkthrough: Draw Log

We can run the "DrawLogo" demo in a similar way to our previous example, that is using the menu option File → Examples → ACROBOTIC SSD1306 → DrawLogo.  As before, all the functionality is included in the setup() function.  After initializing the I2C communication and display, and clearing the display, we call the drawBitmap() function in order to render an image on the screen.

The data that's rendered is stored inside the variable we've named ACROBOT near the beginning of the file.  This data is an array of 128×8 unsigned characters that specify the on/off state of the pixels on the screen.  Because of the large size in memory of the data, we use the PROGMEM keyword to tell the compiler to store the data in Flash memory rather than RAM.

Each unsigned character in the ACROBOT array indicates whether a 1×8 'column' of pixels is on or off.  For instance, to turn on the bottom half of a single column we'd use 00001111, which in hex notation is 0x0F.  Doing this for the 1024 values necessary to draw an image might be a painful task to do manually.  There are plenty of programs online to generate the array such as LCD Creator (for Mac OS).

Generating BMP Image for LCD CreatorAfter creating a .bmp file using your favorite Image/Photo Editor program (e.g., Adobe Photoshop), and ensuring its correct size (128×64 pixels), we can use the LCD Creator program to turn the image into the array we need.

LCD Creator Usage

The generated array can then be copied to the variable that's passed to the drawBitmap() function, in our demo program it'd be ACROBOT.  Try it with your own images and let us know how it goes!

Comments, questions, or concerns? Drop us a line!.