OLED Displays with Arduino: Technology and Connection
FunduinoPublished on
Drei Größen, zwei Bussysteme und die Bibliothek u8g2
How an OLED works
The three sizes and how to control them
1 / 3
Connect a 0.96-inch OLED (I²C)
Connect a 2.42-inch OLED (SPI)
Install the u8g2 library
1 / 2
The program
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