OLED-Displays am Arduino: Technik und Anschluss
FunduinoZuletzt aktualisiert am
Drei Größen, zwei Bussysteme und die Bibliothek u8g2
Wie ein OLED funktioniert
Die drei Größen und ihre Ansteuerung
1 / 3
0,96-Zoll-OLED anschließen (I²C)
2,42-Zoll-OLED anschließen (SPI)
Bibliothek u8g2 installieren
1 / 2
Das Programm
Alle Zeichenfunktionen der Bibliothek u8g2 vorführen
#include "U8g2lib.h"
// 2,42 Zoll OLED initialisieren
U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 8);
// Bildschirmgröße feststellen
int BildschirmBreite = oled.getDisplayWidth();
int BildschirmHoehe = oled.getDisplayHeight();
// Smiley als XBM, erstellt mit 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()
{
// Zufallsgenerator starten
randomSeed(A0);
// Display starten
oled.begin();
// Kontrast, maximal 255
oled.setContrast(200);
}
void loop()
{
// Farbe weiß
oled.setDrawColor(1);
// Smiley
oled.firstPage();
do
{
oled.drawXBM(40, 10, SmileyBreite, SmileyHoehe, Smiley);
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Pixelmuster
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();
// Text zeigen
oled.firstPage();
do
{
oled.setFontDirection(0);
// Schriftart wechseln
oled.setFont(u8g2_font_unifont_t_symbols);
oled.setCursor(2, 10);
oled.print("Text");
// Schriftart wechseln
oled.setFont(u8g2_font_helvB14_tf);
oled.setCursor(2, 30);
oled.print("Text");
// Schriftart wechseln
oled.setFont(u8g2_font_courB24_tf);
oled.setCursor(2, 60);
oled.print("Text");
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Kreise
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();
// Rahmen
oled.firstPage();
do
{
for (int i = 2; i < BildschirmHoehe; i += 10)
{
oled.drawFrame(0, 0, i, i);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// vertikale Linien
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 Linien
oled.firstPage();
do
{
for (int i = 0; i < BildschirmHoehe; i += 4)
{
oled.drawHLine(0, i, BildschirmBreite - 1);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// ausgefüllte Kreise
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();
// Linien
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();
}Anleitung bewerten