|     Inicio    |   |         |  |   FOROS      |  |      |      
   Elastix - VoIP B4A (Basic4Android) App inventor 2 PHP - MySQL
  Estación meteorológica B4J (Basic4Java) ADB Shell - Android Arduino
  AutoIt (Programación) Visual Basic Script (VBS) FireBase (BD autoactualizable) NodeMCU como Arduino
  Teleco - Emisora de A.M. Visual Basic Cosas de Windows Webs interesantes
T Búsqueda en este sitio:


.

Arduino en español
Circuitos con Arduino - Juan Antonio Villalpando

-- Tutorial de iniciación a Arduino --

Volver al índice del tutorial

____________________________

31B.- Pantalla OLED.

- En los últimos años hemos tenido varios tipos de pantallas planas de televisión en color.

- Dos tipos: LCD (Display de Cristal Líquido) y LED (Diodo Emisor de Luz).

 

 

- En las LCD, las moléculas de un cristal líquido se mueven cierto ángulo al suministrarle distintas tensiones eléctricas. Dejando pasar más o menos luz según el ángulo de giro. Este tipo de pantalla necesitan que por su parte posterior sean iluminadas por una luz blanca contínua (backlight).


- Esta luz se obtenía en los primeros televisores de pantalla plana LCD mediante finos tubitos fluorescentes, que necesitaban unos 120 V para encender.


- Más adelante en vez de estos tubitos fluorescente, pusieron LED blancos de alta luminosidad, que necesitaban menos voltios, 24 V y menos potencia, a estos televisores le llamaban Televisores TDT LED, pero su pantalla no era LED, seguía siendo LCD, el LED era solo la luz blanca que iluminaba por atrás.

 

 

 

 

- En años posteriores empezaron a salir televisores con pantalla LED reales, es decir la pantalla solo necesitaba LED de colores Rojos, Verdes y Azules, no necesitaba la luz anterior backlight. Las pantallas grandes necesitaban muchos LED RVA para completar la imagen, esto requería mucha potencia.

 

 

 

 

- Más adelante salieron las pantallas OLED, estas pantallas también son LED (Diodo Emisor de Luz), pero están contruidas con otro tipo de material, material Orgánico, de ahí sus siglas. Este material Orgánico hace que los diodos necesiten menos potencia, la imagen tiene más contraste (diferencia entre el blanco y el negro) y pueden ser muy finas.

________________________________________________
- Vamos a ver pantallas OLED para el Arduino.

- Encontramos varios modelos, se suele indicar según su tamaño en pulgadas, por ejemplo 0,91 pulgadas o 0,96 pulgadas (inch).

- En esta imagen comparo las dimensiones de estos dos OLED y una pantalla LCD.

- En este tutorial veremos la OLED 0,91 pulgadas 128x32 I2C letras de color azul.

- Este es un tutorial en inglés con el OLED 0,96 pulgadas:

https://startingelectronics.org/tutorials/arduino/modules/OLED-128x64-I2C-display/

- Fíjate que algunas llevan el bus I2C y otras no, es mejor con el I2C porque utiliza menos terminales.

- Esquema: OLED_12832.pdf

- DataSheet1306.pdf

- Utilizaremos la librería U8g2.zip

- Otra librería: Tinysine OLED 12832 Library

______________________________________________
- Conexionado.

______________________________________________
- Obtener la dirección I2C.

- Como vimos en el tutorial anterior de la pantalla LCD, es conveniente conocer la dirección I2C de la tarjetas.

- Para ello ponemos el Monitor Serie en 115200, conectamos la tarjeta y cargamos este programita.

Indicará las direcciones de los dispositivos I2C encontrados, por ejemplo...

I2C scanner. Scanning ...
Found address: 60 (0x3C)
Done.
Found 1 device(s).

- Así que mi pantalla OLED de 0,91 tiene una dirección de 0x3C.

Código para ver direcciones I2C
#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial)
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
 
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

________________________________________________
1.- Presenta informaciones cortas.

- Los números 5 y 32 u8g2.drawStr(5,32,"Antonio"); indican la posición horizontal y vertical del primer carácter.

- Podemos poner otros caracteres consultando el archivo:

https://github.com/olikraus/u8g2/wiki/fntlistall

- Utilizamos la librería U8g2.zip

jInformaciones.ino

// Juan A. Villalpando
// KIO4.COM

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 

 void setup(void) {
   u8g2.begin();
}

  void loop(void) {
   u8g2.clearBuffer();          // Limpia la memoria interna
   u8g2.setFont(u8g2_font_logisoso28_tr);  // Más fuentes en: https://github.com/olikraus/u8g2/wiki/fntlistall
   u8g2.drawStr(30,31,"KIO4");  // Escribe en la memoria interna.
   u8g2.sendBuffer();         // Pasa de la memoria interna a la pantalla.
   delay(1000);

   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_logisoso28_tr);
   u8g2.drawStr(31,30,"Juan"); // 31,24
   u8g2.sendBuffer();
   delay(1000);

   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_logisoso28_tr);
   u8g2.drawStr(5,32,"Antonio");
   u8g2.sendBuffer();
   delay(1000);

   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_logisoso28_tr);
   u8g2.drawStr(9,32,"Arduino");
   u8g2.sendBuffer();
   delay(1000);
}

________________________________________________
2.- Presenta en una misma pantalla distintos tipos de letras.

https://github.com/olikraus/u8g2/wiki/fntlistall

jTipos_de_letras.ino

// Juan A. Villalpando
// KIO4.COM

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 

 void setup(void) {
   u8g2.begin();
}

  void loop(void) {
   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_logisoso18_tr);
   u8g2.drawStr(0,20,"Juan");
   u8g2.setFont(u8g2_font_ncenB08_tr);
   u8g2.drawStr(27,32,"Antonio");
   u8g2.sendBuffer();
   delay(1000);
   
   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_etl16thai_t);
   u8g2.drawStr(0,20,"Juan");
   u8g2.setFont(u8g2_font_mercutio_basic_nbp_tf);
   u8g2.drawStr(27,32,"Antonio");
   u8g2.sendBuffer();
   delay(1000);

   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_helvB14_tf);
   u8g2.drawStr(0,20,"Juan");
   u8g2.setFont(u8g2_font_5x8_tr);
   u8g2.drawStr(27,32,"Antonio");
   u8g2.sendBuffer();
   delay(1000);

}

________________________________________________
3.- Presenta símbolos.

https://useiconic.com/open#icons

- Escribe estos símbolos:

☀☁☂☎☏★✔✖♞

jSimbolos.ino

// Juan A. Villalpando
// KIO4.COM

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 

 void setup(void) {
   u8g2.begin();
}

  void loop(void) {
   u8g2.clearBuffer();
   u8g2.setFont(u8g2_font_unifont_t_symbols);
   u8g2.drawUTF8(0, 30, "☀☁☂☎☏★✔✖♞");
   u8g2.sendBuffer();
}

________________________________________________
4.- Con otra librería.

- Vamos a cargar otra librería:

- Otra librería: Tinysine OLED 12832 Library

- La descomprimimos y obtenemos las carpetas:

Adafruit_SSD1306 y Adafruit_GFX

- Copiamos esas dos carpetas en la carpeta Libraries de Arduino.

- Dentro de la carpeta Adafruit_SSD1306, hay un subcarpeta de examples, si nuestro módulo tiene I2C, cargamos los ejemplos i2c.

jDibujos_moviles.ino

/*********************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/category/63_98

This example is for a 128x32 size display using I2C to communicate
3 pins are required to interface (2 I2C and one reset)

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada  for Adafruit Industries.  
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {                
  Serial.begin(9600);

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done
  
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);

  // Clear the buffer.
  display.clearDisplay();

  // draw a single pixel
  display.drawPixel(10, 10, WHITE);
  // Show the display buffer on the hardware.
  // NOTE: You _must_ call display after making any drawing commands
  // to make them visible on the display hardware!
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw many lines
  testdrawline();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw rectangles
  testdrawrect();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw multiple rectangles
  testfillrect();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw mulitple circles
  testdrawcircle();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw a white circle, 10 pixel radius
  display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);
  display.display();
  delay(2000);
  display.clearDisplay();

  testdrawroundrect();
  delay(2000);
  display.clearDisplay();

  testfillroundrect();
  delay(2000);
  display.clearDisplay();

  testdrawtriangle();
  delay(2000);
  display.clearDisplay();
   
  testfilltriangle();
  delay(2000);
  display.clearDisplay();

  // draw the first ~12 characters in the font
  testdrawchar();
  display.display();
  delay(2000);
  display.clearDisplay();

  // draw scrolling text
  testscrolltext();
  delay(2000);
  display.clearDisplay();

  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Hello, world!");
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.println(3.141592);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.print("0x"); display.println(0xDEADBEEF, HEX);
  display.display();
  delay(2000);
  display.clearDisplay();

  // miniature bitmap display
  display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
  display.display();
  delay(1);

  // invert the display
  display.invertDisplay(true);
  delay(1000); 
  display.invertDisplay(false);
  delay(1000); 
  display.clearDisplay();

  // draw a bitmap icon and 'animate' movement
  testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
}


void loop() {
  
}


void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  uint8_t icons[NUMFLAKES][3];
 
  // initialize
  for (uint8_t f=0; f< NUMFLAKES; f++) {
    icons[f][XPOS] = random(display.width());
    icons[f][YPOS] = 0;
    icons[f][DELTAY] = random(5) + 1;
    
    Serial.print("x: ");
    Serial.print(icons[f][XPOS], DEC);
    Serial.print(" y: ");
    Serial.print(icons[f][YPOS], DEC);
    Serial.print(" dy: ");
    Serial.println(icons[f][DELTAY], DEC);
  }

  while (1) {
    // draw each icon
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
    }
    display.display();
    delay(200);
    
    // then erase it + move it
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
      // move it
      icons[f][YPOS] += icons[f][DELTAY];
      // if its gone, reinit
      if (icons[f][YPOS] > display.height()) {
        icons[f][XPOS] = random(display.width());
        icons[f][YPOS] = 0;
        icons[f][DELTAY] = random(5) + 1;
      }
    }
   }
}


void testdrawchar(void) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);

  for (uint8_t i=0; i < 168; i++) {
    if (i == '\n') continue;
    display.write(i);
    if ((i > 0) && (i % 21 == 0))
      display.println();
  }    
  display.display();
  delay(1);
}

void testdrawcircle(void) {
  for (int16_t i=0; i<display.height(); i+=2) {
    display.drawCircle(display.width()/2, display.height()/2, i, WHITE);
    display.display();
    delay(1);
  }
}

void testfillrect(void) {
  uint8_t color = 1;
  for (int16_t i=0; i<display.height()/2; i+=3) {
    // alternate colors
    display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
    display.display();
    delay(1);
    color++;
  }
}

void testdrawtriangle(void) {
  for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {
    display.drawTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    display.display();
    delay(1);
  }
}

void testfilltriangle(void) {
  uint8_t color = WHITE;
  for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {
    display.fillTriangle(display.width()/2, display.height()/2-i,
                     display.width()/2-i, display.height()/2+i,
                     display.width()/2+i, display.height()/2+i, WHITE);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
    delay(1);
  }
}

void testdrawroundrect(void) {
  for (int16_t i=0; ii<display.height()/2-2; i+=2) {
    display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);
    display.display();
    delay(1);
  }
}

void testfillroundrect(void) {
  uint8_t color = WHITE;
  for (int16_t i=0; i<display.height()/2-2; i+=2) {
    display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
    if (color == WHITE) color = BLACK;
    else color = WHITE;
    display.display();
    delay(1);
  }
}
   
void testdrawrect(void) {
  for (int16_t i=0; i<display.height()/2; i+=2) {
    display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);
    display.display();
    delay(1);
  }
}

void testdrawline() {  
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, 0, i, display.height()-1, WHITE);
    display.display();
    delay(1);
  }
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(0, 0, display.width()-1, i, WHITE);
    display.display();
    delay(1);
  }
  delay(250);
  
  display.clearDisplay();
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(0, display.height()-1, i, 0, WHITE);
    display.display();
    delay(1);
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);
    display.display();
    delay(1);
  }
  delay(250);
  
  display.clearDisplay();
  for (int16_t i=display.width()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);
    display.display();
    delay(1);
  }
  for (int16_t i=display.height()-1; i>=0; i-=4) {
    display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);
    display.display();
    delay(1);
  }
  delay(250);

  display.clearDisplay();
  for (int16_t i=0; i<display.height(); i+=4) {
    display.drawLine(display.width()-1, 0, 0, i, WHITE);
    display.display();
    delay(1);
  }
  for (int16_t i=0; i<display.width(); i+=4) {
    display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE); 
    display.display();
    delay(1);
  }
  delay(250);
}

void testscrolltext(void) {
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,0);
  display.clearDisplay();
  display.println("scroll");
  display.display();
  delay(1);
 
  display.startscrollright(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(2000);
  display.stopscroll();
  delay(1000);    
  display.startscrolldiagright(0x00, 0x07);
  delay(2000);
  display.startscrolldiagleft(0x00, 0x07);
  delay(2000);
  display.stopscroll();
}			

- Con esta aplicación, podemos cargar una pequeña imagen .jpg y la convierte a texto preparado para insertarlo en el código y que salga esa imagen en la pantalla:

LCDAssistant

________________________________

 

- Mi correo:
juana1991@yahoo.com
- KIO4.COM - Política de cookies. Textos e imágenes propiedad del autor:
© Juan A. Villalpando
No se permite la copia de información ni imágenes.
Usamos cookies propias y de terceros que entre otras cosas recogen datos sobre sus hábitos de navegación y realizan análisis de uso de nuestro sitio.
Si continúa navegando consideramos que acepta su uso. Acepto    Más información