OLED Displays with Arduino: Technology and Connection

FunduinoPublished on

Drei Größen, zwei Bussysteme und die Bibliothek u8g2

OLED-Displays leuchten selbst und brauchen deshalb keine Hintergrundbeleuchtung. Das macht sie klein, kontrastreich und sparsam – und zu einer beliebten Anzeige für Arduino-Projekte.

Diese Seite erklärt, wie ein OLED technisch funktioniert, worin sich die gängigen Größen unterscheiden, wie du sie über I²C oder SPI anschließt – und zeigt an einem ausführlichen Beispiel, was die Bibliothek u8g2 alles zeichnen kann.

Circuit diagram with 2.42-inch OLED via SPI on the Arduino UNO
Circuit diagram: 2.42-inch OLED with SSD1309 on Arduino

How an OLED works

OLED stands for Organic Light Emitting Diode. Unlike LCDs (Liquid Crystal Display), OLEDs don’t need a backlight - they emit light themselves.

An OLED consists of two electrodes, at least one of which must be transparent. Between them are organic semiconductor layers made from natural dyes. These layers emit light as soon as direct current flows through them. This is based on electroluminescence: a solid is stimulated to produce light by applying an electrical voltage.

The different OLEDs vary in size and in how the microcontroller communicates with them. In addition to the power supply, the I²C (Inter-Integrated Circuit) data bus needs only two connections for data transmission, while the SPI (Serial Peripheral Interface) bus needs five.

The three sizes and how to control them

Here we show three common sizes. Which bus is used depends on the installed chipset:

  • 0.96 inches – SSD1306 chipset, controlled via I²C
  • 1.3 inches – SH1106 chipset, controlled via I²C
  • 2.42 inches – SSD1309 chipset, controlled via SPI
0.96-inch OLED display
0.96-inch OLED – controlled via I²C
1.3-inch OLED display
1.3-inch OLED – controlled via I²C
2.42-inch OLED display
2.42-inch OLED – controlled via SPI
1 / 3

Connect a 0.96-inch OLED (I²C)

For I²C, you only need two wires in addition to the power supply: SDA and SCL. On the Arduino UNO, they are on A4 (SDA) and A5 (SCL).

Circuit diagram with 0.96-inch OLED on the Arduino UNO
Schematic: 0.96-inch OLED on Arduino

Connect a 2.42-inch OLED (SPI)

SPI adds five lines for power supply:

  • GND – GND
  • VCC – 5V
  • SCK – Pin 13
  • SDA – Pin 11
  • RS – Pin 8 (Reset)
  • DC – Pin 9
  • CS – Pin 10

These three pins – 10, 9, and 8 – appear again in exactly this order in the sketch when the display is initialized.

Circuit diagram with 2.42-inch OLED via SPI on the Arduino UNO
Circuit diagram: 2.42-inch OLED with SSD1309 on Arduino

Install the u8g2 library

Search under Sketch › Include Library › Manage Libraries for the library u8g2. Alternatively, click the Library Manager icon in the sidebar.

Library management in the Arduino IDE with the u8g2 library
The u8g2 library in the Library Manager
Library Manager icon in the Arduino IDE
Alternatively, via this icon in the sidebar
1 / 2

The program

Depending on the chipset, you need to configure the display differently in u8g2. Replace the first line after the include in the sketch with the appropriate option:

  • 0.96 inches, I²C (SSD1306): U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);
  • 1.3 inches, I²C (SH1106): U8G2_SH1106_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);
  • 2.42 inches, SPI (SSD1309): U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 8);

The sketch demonstrates the library’s capabilities in sequence: an image as XBM, a random pixel pattern, text in three fonts, circles, frames, lines, and filled circles. Each view stays on screen for two seconds.

Demonstrate all drawing functions of the u8g2 library
```cpp
#include "U8g2lib.h"

// Initialize the 2.42-inch OLED
U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 8);

// Determine screen size
int ScreenWidth = oled.getDisplayWidth();
int ScreenHeight = oled.getDisplayHeight();

// Smiley as XBM, created with GIMP
#define SmileyWidth 46
#define SmileyHeight 45
static unsigned char Smiley[] =
{
   0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00,
   0x00, 0xf0, 0x07, 0xf8, 0x03, 0x00, 0x00, 0xfc, 0x00, 0xc0, 0x0f, 0x00,
   0x00, 0x3e, 0x00, 0x00, 0x1f, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x7c, 0x00,
   0xc0, 0x07, 0x00, 0x00, 0xf8, 0x00, 0xe0, 0x01, 0x00, 0x00, 0xe0, 0x01,
   0xf0, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x70, 0x00, 0x00, 0x00, 0x80, 0x03,
   0x38, 0x7e, 0x00, 0x80, 0x1f, 0x07, 0x38, 0xff, 0x00, 0xc0, 0x3f, 0x07,
   0x9c, 0xff, 0x01, 0xc0, 0x3f, 0x0e, 0x9c, 0xe7, 0x01, 0xc0, 0x39, 0x0e,
   0x8e, 0xc3, 0x01, 0xc0, 0x30, 0x1c, 0x8e, 0xe3, 0x01, 0xc0, 0x31, 0x1c,
   0x86, 0xf7, 0x01, 0xc0, 0x3b, 0x18, 0x87, 0xff, 0x01, 0xc0, 0x3f, 0x38,
   0x07, 0xff, 0x00, 0x80, 0x3f, 0x38, 0x03, 0x7e, 0x00, 0x80, 0x1f, 0x30,
   0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30,
   0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30,
   0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30,
   0x07, 0x00, 0x00, 0x00, 0x00, 0x38, 0x07, 0x20, 0x00, 0x00, 0x01, 0x38,
   0x06, 0x70, 0x00, 0x80, 0x03, 0x18, 0x0e, 0xf0, 0x00, 0xc0, 0x01, 0x1c,
   0x0e, 0xe0, 0x03, 0xf0, 0x01, 0x1c, 0x1c, 0xc0, 0x3f, 0xfc, 0x00, 0x0e,
   0x1c, 0x80, 0xff, 0x7f, 0x00, 0x0e, 0x38, 0x00, 0xfc, 0x1f, 0x00, 0x07,
   0x38, 0x00, 0xc0, 0x03, 0x00, 0x07, 0x70, 0x00, 0x00, 0x00, 0x80, 0x03,
   0xf0, 0x00, 0x00, 0x00, 0xc0, 0x03, 0xe0, 0x01, 0x00, 0x00, 0xe0, 0x01,
   0xc0, 0x07, 0x00, 0x00, 0xf8, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x7c, 0x00,
   0x00, 0x3e, 0x00, 0x00, 0x1f, 0x00, 0x00, 0xfc, 0x00, 0xc0, 0x0f, 0x00,
   0x00, 0xf0, 0x07, 0xf8, 0x03, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00,
   0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00
};

void setup()
{
  // Start random number generator
  randomSeed(A0);

  // Start display
  oled.begin();

  // Contrast, maximum 255
  oled.setContrast(200);
}

void loop()
{
  // White color
  oled.setDrawColor(1);

  // Smiley
  oled.firstPage();
  do
  {
    oled.drawXBM(40, 10, SmileyWidth, SmileyHeight, Smiley);
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Pixel pattern
  oled.firstPage();
  do
  {
    for (int i = 0; i < 500; i++)
    {
      int x = random(1, ScreenWidth);
      int y = random(1, ScreenHeight);
      oled.drawPixel(x, y);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Show text
  oled.firstPage();
  do
  {
    oled.setFontDirection(0);

    // Change font
    oled.setFont(u8g2_font_unifont_t_symbols);
    oled.setCursor(2, 10);
    oled.print("Text");

    // Change font
    oled.setFont(u8g2_font_helvB14_tf);
    oled.setCursor(2, 30);
    oled.print("Text");

    // Change font
    oled.setFont(u8g2_font_courB24_tf);
    oled.setCursor(2, 60);
    oled.print("Text");
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Circles
  oled.firstPage();
  do
  {
    for (int i = 2; i < ScreenHeight / 2; i += 3)
    {
      oled.drawCircle(ScreenWidth / 2, ScreenHeight / 2, i);
      delay(10);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Frames
  oled.firstPage();
  do
  {
    for (int i = 2; i < ScreenHeight; i += 10)
    {
      oled.drawFrame(0, 0, i, i);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Vertical lines
  oled.firstPage();
  do
  {
    for (int i = 0; i < ScreenWidth; i += 4)
    {
      oled.drawVLine(i, 0, ScreenWidth - 1);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Horizontal lines
  oled.firstPage();
  do
  {
    for (int i = 0; i < ScreenHeight; i += 4)
    {
      oled.drawHLine(0, i, ScreenWidth - 1);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Filled circles
  oled.firstPage();
  do
  {
    int Radius = 5;
    int StartX = 10;
    int StartY = 10;
    while (StartX < ScreenWidth - Radius)
    {
      for (int i = StartY; i < ScreenWidth - Radius; i += 20)
      {
        oled.drawDisc(StartX, i, Radius);
      }
      StartX += 10;
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

  // Lines
  oled.firstPage();
  do
  {
    for (int i = 0; i < ScreenWidth; i += 5)
    {
      oled.drawLine(0, i, 128, 64);
    }
    for (int i = ScreenWidth; i > 0; i -= 5)
    {
      oled.drawLine(ScreenWidth, i, 0, 0);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();
}
```
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.