Pantallas OLED con Arduino: tecnología y conexión
FunduinoPublicado el
Drei Größen, zwei Bussysteme und die Bibliothek u8g2
Cómo funciona una pantalla OLED
Los tres tamaños y su control
1 / 3
Conectar OLED de 0,96 pulgadas (I²C)
Conectar una pantalla OLED de 2,42 pulgadas (SPI)
Instalar la biblioteca u8g2
1 / 2
El programa
Mostrar todas las funciones gráficas de la biblioteca u8g2
#include "U8g2lib.h"
// Inicializar OLED de 2,42 pulgadas
U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 8);
// Determinar el tamaño de la pantalla
int BildschirmBreite = oled.getDisplayWidth();
int BildschirmHoehe = oled.getDisplayHeight();
// Smiley como XBM, creado con 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()
{
// Iniciar el generador aleatorio
randomSeed(A0);
// Iniciar la pantalla
oled.begin();
// Contraste, máximo 255
oled.setContrast(200);
}
void loop()
{
// Color blanco
oled.setDrawColor(1);
// Smiley
oled.firstPage();
do
{
oled.drawXBM(40, 10, SmileyBreite, SmileyHoehe, Smiley);
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Patrón de píxeles
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();
// Mostrar texto
oled.firstPage();
do
{
oled.setFontDirection(0);
// Cambiar la fuente
oled.setFont(u8g2_font_unifont_t_symbols);
oled.setCursor(2, 10);
oled.print("Text");
// Cambiar la fuente
oled.setFont(u8g2_font_helvB14_tf);
oled.setCursor(2, 30);
oled.print("Text");
// Cambiar la fuente
oled.setFont(u8g2_font_courB24_tf);
oled.setCursor(2, 60);
oled.print("Text");
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Círculos
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();
// Marcos
oled.firstPage();
do
{
for (int i = 2; i < BildschirmHoehe; i += 10)
{
oled.drawFrame(0, 0, i, i);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Líneas verticales
oled.firstPage();
do
{
for (int i = 0; i < BildschirmBreite; i += 4)
{
oled.drawVLine(i, 0, BildschirmBreite - 1);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Líneas horizontales
oled.firstPage();
do
{
for (int i = 0; i < BildschirmHoehe; i += 4)
{
oled.drawHLine(0, i, BildschirmBreite - 1);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Círculos rellenos
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();
// Líneas
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();
}Calificar la guía