OLED-displays met Arduino: techniek en aansluiting

FunduinoGepubliceerd op

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.

Schaltplan mit 2,42-Zoll-OLED über SPI am Arduino UNO
Schaltplan: 2,42-inch-OLED mit SSD1309 am Arduino

Hoe een OLED werkt

OLED staat voor Organic Light Emitting Diode. In tegenstelling tot lcd's (Liquid Crystal Display, oftewel vloeibarekristalscherm) hebben OLED's geen achtergrondverlichting nodig - ze geven zelf licht.

Een OLED bestaat uit twee elektroden, waarvan er minstens één doorzichtig moet zijn. Daartussen bevinden zich organische halfgeleidende lagen van natuurlijke kleurstoffen. Deze lagen geven licht zodra er gelijkstroom doorheen loopt. Dit berust op elektroluminescentie: een vaste stof wordt door het aanleggen van een elektrische spanning gestimuleerd om licht te produceren.

De verschillende OLED's verschillen in grootte en in de manier waarop de microcontroller ze aanstuurt. De I²C-databus (Inter-Integrated Circuit) heeft naast de stroomvoorziening slechts twee aansluitingen nodig voor de gegevensoverdracht, de SPI-bus (Serial Peripheral Interface) vijf.

De drie formaten en hun aansturing

We tonen hier drie gangbare formaten. Welke bus wordt gebruikt, hangt af van de ingebouwde chipset:

  • 0,96 inch – chipset SSD1306, aansturing via I²C
  • 1,3 inch – chipset SH1106, aansturing via I²C
  • 2,42 inch – chipset SSD1309, aansturing via SPI
0,96-inch OLED-scherm
0,96-inch OLED – aansturing via I²C
1,3 inch groot OLED-display
1,3-inch-OLED – aansturing via I²C
2,42 inch groot OLED-display
2,42-inch OLED - aansturing via SPI
1 / 3

Een 0,96-inch OLED aansluiten (I²C)

Voor I²C heb je naast de voeding maar twee lijnen nodig: SDA en SCL. Op de Arduino UNO zitten ze op A4 (SDA) en A5 (SCL).

Schakelschema met 0,96-inch-oledscherm op de Arduino UNO
Schakelschema: 0,96-inch-OLED op Arduino

2,42-inch-OLED aansluiten (SPI)

Naast SPI komen er vijf voedingsdraden bij:

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

Deze drie pinnen – 10, 9 en 8 – komen in precies deze volgorde terug in de sketch wanneer het display wordt aangemaakt.

Schaltplan mit 2,42-Zoll-OLED über SPI am Arduino UNO
Schaltplan: 2,42-inch-OLED mit SSD1309 am Arduino

Installeer de u8g2-bibliotheek

Zoek naar Sketch › Bibliotheek toevoegen › Bibliotheken beheren de bibliotheek u8g2. Je kunt ook op het pictogram voor bibliotheekbeheer in de zijbalk klikken.

Bibliotheekbeheer van de Arduino IDE met de bibliotheek u8g2
De bibliotheek u8g2 in het bibliotheekbeheer
Symbool voor bibliotheekbeheer in de Arduino IDE
Als alternatief via dit pictogram in de zijbalk
1 / 2

Het programma

Depending on the chipset, you need to set up the display differently in u8g2. To do so, replace the first line after the include in the sketch with the appropriate option:

  • 0.96 inch, I²C (SSD1306): U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);
  • 1.3 inch, I²C (SH1106): U8G2_SH1106_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);
  • 2.42 inch, 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 remains on screen for two seconds.

Alle tekenfuncties van de bibliotheek u8g2 demonstreren
#include "U8g2lib.h"
// 2,42-inch-OLED initialiseren
U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 8);

// Schermgrootte bepalen
int BildschirmBreite = oled.getDisplayWidth();
int BildschirmHoehe = oled.getDisplayHeight();

// Smiley als XBM, gemaakt met GIMP
#define SmileyBreite 46
#define SmileyHoehe 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()
{
  // Willekeurige generator starten
  randomSeed(A0);

  // Display starten
  oled.begin();

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

void loop()
{
  // Kleur wit
  oled.setDrawColor(1);

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

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

  // Tekst weergeven
  oled.firstPage();
  do
  {
    oled.setFontDirection(0);

    // Lettertype wijzigen
    oled.setFont(u8g2_font_unifont_t_symbols);
    oled.setCursor(2, 10);
    oled.print("Text");

    // Lettertype wijzigen
    oled.setFont(u8g2_font_helvB14_tf);
    oled.setCursor(2, 30);
    oled.print("Text");

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

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

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

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

  // Horizontale lijnen
  oled.firstPage();
  do
  {
    for (int i = 0; i < BildschirmHoehe; i += 4)
    {
      oled.drawHLine(0, i, BildschirmBreite - 1);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();

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

  // Lijnen
  oled.firstPage();
  do
  {
    for (int i = 0; i < BildschirmBreite; i += 5)
    {
      oled.drawLine(0, i, 128, 64);
    }
    for (int i = BildschirmBreite; i > 0; i -= 5)
    {
      oled.drawLine(BildschirmBreite, i, 0, 0);
    }
  }
  while (oled.nextPage());
  delay(2000);
  oled.clearDisplay();
}
Handleiding beoordelen

Opmerkingen

Stel een vraag of deel je ervaring

Neem contact met ons op als iets niet werkt. Vermeld daarbij je board en de foutmelding.

We controleren elke reactie voordat deze wordt gepubliceerd. Je e-mailadres blijft bij ons.