OLED 128×32 I2C – ElectroSchematics.com

For several microcontroller projects, the integration of a minuscule display panel can be handy to show relevant information. For example, information gathered by an external sensor module. Here we discuss how to use a popular and compact monochrome 128×32 pixel I2C graphic OLED display with a microcontroller. Get ready for some experiments!

This is a 0.91 inch diagonal 128×32 OLED display module with an embedded SSD1306 controller, communicating via I2C interface. Note that the SSD1306 is a controller for 128×64 OLED display. Since this display has only 128×32 pixels it uses only a portion of the SSD1306’s buffer.

This OLED display module has a breadboard friendly formfactor with a solderable 4-pin male-header on the back that can be inserted into a regular breadboard. Or a 4-pin female-header can be used to connect to it if the display is to be mounted on a perfboard. Be cautious, the display is very thin and fragile, so be sure to press on the header pins when applying pressure to plug into a circuit board. Never press on the glass panel! ⚠

SSD1306 is a single-chip CMOS OLED/PLED driver with a controller for the organic/polymer light emitting diode dot-matrix graphic display system. It consists of 128 segments and 64commons. This chip is designed for common-cathode type OLED panel.

This OLED display, like many other modules, also works well with most 3.3V and 5V microcontrollers. Since the communication is through the I2C interface, only two pins (serial clock and serial data) are needed for the microcontroller interface – SCL & SDA. The default (sadly, hardwired) I2C address for this OLED display is 0x3C. You can see more details about I2C in the SSD1306 datasheet (Page 20).

SSD1306 Datasheet (direct download) https://www.crystalfontz.com/controllers/SolomonSystech/SSD1306/339/

Let’s get into a quick test setup involving an Arduino! I’ll take you there in a few moments…

To get it running with Arduino, a couple of libraries need to be included. The “Wire” library for I2C communication is already available in the Arduino IDE. The SSD1306 and the “GFX” library we’ll have to install ourselves (https://www.arduino.cc/en/guide/libraries).

The below table shows the Arduino hardware configuration for this OLED display module.

ARDUINO UNO OLED MODULE
GND GND
5V VCC
A5/SCL SCL
A4/SDA SDA

Here is the sample test code (Arduino Sketch):

[code]

#include

#include

#include

#define
SCREEN_WIDTH 128

#define
SCREEN_HEIGHT 32

Adafruit_SSD1306
display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

void
setup() {

Serial.begin(9600);

if (!display.begin(SSD1306_SWITCHCAPVCC,
0x3C)) {

Serial.println(F(“SSD1306 Allocation .)
Failed”)));

for (;;);

}

display.clearDisplay();

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(0, 0);

display.println(“Hello 128×32
OLED”);

display.println(“Hello
Arduino Uno”);

display.println(“Quick
Test Example”);

display.println(“Thank You!”);

display.display();

}

void
loop() {

}

[/code]

This little code just prints some custom text in 4 different lines on the OLED display (see below shots). However, to see a demo of its full graphical potency, you can try the examples together with the Adafruit SSD1306 Arduino Library.

Once you get to this point, upload this new code without changing your hardware setup. What do you see? Is it like the image shown after the code?

[code]

#include

#include

#include

#define
SCREEN_WIDTH 128

#define
SCREEN_HEIGHT 32

Adafruit_SSD1306
display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);

void
setup(void) {

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

}

void
loop() {

display.clearDisplay();

display.setCursor (10, 5);

display.setTextSize(2);

display.setTextColor (WHITE);

display.print (“Hello”);

display.setCursor (10, 23);

display.setTextSize(1);

display.print (“Friends…”);

delay (1000);

display.drawCircle (110, 15, 15, WHITE); //
Head frame

display.drawCircle (105, 14, 4, WHITE); //
left eye

display.drawCircle (115, 14, 4, WHITE); //
Right eye

display.drawPixel (103, 22, WHITE); // Mouth

display.drawPixel (118, 22, WHITE); // Mouth

display.drawPixel (104, 23, WHITE); // Mouth

display.drawPixel (117, 23, WHITE); // Mouth

display.drawPixel (105, 24, WHITE); // Mouth

display.drawPixel (116, 24, WHITE); // Mouth

display.drawLine (106, 25, 115, 25, WHITE);
// Mouth

display.display();

delay (1000);

}

[/code]

How is this possible? An excellent question! See, it’s simple because the OLED is made up of pixels, which are tiny squares that can be either turned on or turned off. The OLED display used here is a rectangle and they are arranged like dots or squares on a coordinate plane. So, every individual pixel in the OLED display can be addressed, simple graphical elements can be designed and displayed such as open and filled circles, circle segments, ellipses, lines, rectangles, and triangles.

The basic thing you need to recall is that the coordinate plane is made up of an x-axis and y-axis. The horizontal axis (left and right) is the x-axis and the vertical axis (up and down) is the y-axis. And, when we consider points in the coordinate grid, we give the point an x-value and a y-value.

Just to exemplify, take a closer look at the below figure (reference media credit goes to https://www.becauselearning.com/). Here, the right eyeball of the smiley face has an x-value of 8 and a y-value of 2, so mathematically we would write it as “(8, 2)”.

Returning to our example code, display.drawPixel (103, 22, WHITE); Tell the Arduino to turn on the pixel located at coordinate (103, 22) ie, 103 pixels in the x-direction and 22 pixels in the y-direction. Likewise, display.drawCircle (105, 14, 4, WHITE); denotes display.drawCircle(x-coordinate of the centre, y-coordinate of the centre, radius, WHITE); Did you understand?

Your real challenge is going to be to understand the secrets of OLED pixel art, but it might need some time and patience (perhaps a daunting task).

Anyway, before you go for that (you must do it yourself), let me show you an easy way!

There are some shortcuts to handle OLED graphics?

As an example, “image2cpp” is a simple online tool (works offline as well) to change images into byte arrays (and vice versa) for use with Arduino and monochrome OLED displays. You can use this tool, originally made to work with the Adafruit OLED library, online at http://javl.github.io/image2cpp/ (below you can see a sample snip).

Be concerned, for prolonged static display however an OLED seems less appropriate as there are reports of burning in of pixels in many OLED displays that have been infinitely in live state!

For now, it works well! 😍

The 128×32 I2C OLED display is very easy to get up and running because it has built in voltage regulator (https://www.torexsemi.com/file/xc6206/XC6206.pdf). However, there is no easy option in front of you to change its default (hardwired) I2C address (although one or more of the surface mount resistors might control it). One significant note about this is that if they all have the same I2C address, then in order to get more than one OLED display working from the same microcontroller, you’ll need multiple I2C buses (https://github.com/bitbank2/BitBang_I2C).

Did you enjoy this post? Now it’s time to use your knowledge to make your own OLED display projects. Ready for the next adventure?

Leave a Comment