Pantalla TFT de 1,44 pulgadas en Arduino

FunduinoPublicado el

Text, Linien und Formen auf dem ST7735 zeichnen

Ein TFT-Display zeigt nicht nur Zeichen wie ein LCD, sondern echte Grafik in Farbe. Das hier verwendete Modell misst 1,44 Zoll, wird über SPI angesteuert und nutzt den verbreiteten Controller ST7735.

Die Bibliothek Adafruit ST7735 nimmt dir die Ansteuerung ab. Der Beispielsketch führt der Reihe nach vor, was möglich ist: Schrift in vier Größen und mehreren Farben, Linien, gefüllte Kreise sowie umrandete und ausgefüllte Rechtecke.

Esquema de conexión de la pantalla TFT al Arduino
Así se conecta la pantalla

Para qué sirven las pantallas TFT

Las pantallas TFT están por todas partes: en el navegador y el tablero del coche, en televisores y monitores, en cámaras digitales y smartphones, y ahora también en hornos y robots de cocina.

El modelo utilizado aquí tiene una diagonal de 1,44 pulgadas, se controla mediante SPI y funciona con el controlador ST7735.

Conectar la pantalla

En el sketch se han definido tres líneas de control: CS en el pin 10, DC en el pin 9 y RST en el pin 8. También se conectan las líneas SPI, la alimentación y la masa.

El esquema de conexiones muestra la asignación completa.

Esquema de conexión de la pantalla TFT al Arduino
Así se conecta la pantalla

Instalar la biblioteca

Busca en el Administrador de bibliotecas, en Sketch → Incluir biblioteca → Gestionar bibliotecas, por Adafruit ST7735. En el cuadro de diálogo siguiente, debes instalar también las bibliotecas adicionales necesarias, especialmente Adafruit GFX, que incluye los comandos de dibujo propiamente dichos.

La tabla de resumen muestra qué comandos están disponibles.

Biblioteca Adafruit ST7735 en el administrador de bibliotecas
La biblioteca Adafruit ST7735
Tabla con los comandos de dibujo de la biblioteca
Resumen de los comandos más importantes
1 / 2

El programa

El sketch se ejecuta por completo una vez en setup y muestra en secuencia lo que puede hacer la pantalla: cuatro tamaños de fuente en distintos colores, líneas horizontales, círculos concéntricos y rectángulos contorneados y rellenos.

El loop permanece vacío: no hay nada que repetir.

Si la pantalla muestra bordes desplazados o colores intercambiados, el tipo de inicialización no es el adecuado. El sketch usa INITR_REDTAB; según el lote, pueden ser correctos INITR_GREENTAB o INITR_BLACKTAB.

Dibujar texto, líneas, círculos y rectángulos
// Pins zuordnen
# define TFT_PIN_CS 10
# define TFT_PIN_DC 9
# define TFT_PIN_RST 8
// Bibliotheken einbinden
# include "SPI.h"
# include "Adafruit_GFX.h"
# include "Adafruit_ST7735.h"
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_PIN_CS, TFT_PIN_DC, TFT_PIN_RST);
void setup()
{
  Serial.begin(9600);
  delay(500);
  Serial.println("Bildschirm: " + String(tft.height()) + " x " + String(tft.width()));
  tft.initR(INITR_REDTAB);
  // Bildschirm um 180 Grad drehen
  tft.setRotation(2);
  // schwarzer Hintergrund
  tft.fillScreen(ST7735_BLACK);
  // verschiedene Schriftgrößen
  tft.setTextSize(1);
  tft.setCursor(1, 5);
  tft.setTextColor(ST7735_BLUE);
  tft.print("Text");
  delay(500);
  tft.setTextSize(2);
  tft.setCursor(1, 20);
  tft.setTextColor(ST7735_GREEN);
  tft.print("Text");
  delay(500);
  tft.setTextSize(3);
  tft.setCursor(1, 40);
  tft.setTextColor(ST7735_RED);
  tft.print("Text");
  delay(500);
  tft.setTextSize(4);
  tft.setCursor(1, 70);
  tft.setTextColor(ST7735_YELLOW);
  tft.print("Text");
  delay(2000);
  // Linien ziehen
  tft.fillScreen(ST7735_BLACK);
  tft.drawLine(1, 1, tft.width(), 1, ST7735_ORANGE);
  tft.drawLine(1, 30, tft.width(), 30, ST7735_ORANGE);
  tft.drawLine(1, 60, tft.width(), 60, ST7735_ORANGE);
  delay(2000);
  // Kreise zeichnen
  tft.fillScreen(ST7735_BLACK);
  tft.fillCircle(60, 60, 50, ST7735_MAGENTA);
  tft.fillCircle(60, 60, 30, ST7735_GREEN);
  tft.fillCircle(60, 60, 30, ST7735_YELLOW);
  delay(2000);
  // Rechtecke zeichnen
  tft.fillScreen(ST7735_BLACK);
  tft.drawRect(1, 1, 50, 50, ST7735_ORANGE);
  tft.drawRect(5, 5, 50, 50, ST7735_ORANGE);
  tft.drawRect(10, 10, 50, 50, ST7735_ORANGE);
  delay(2000);
  // ausgefüllte Rechtecke zeichnen
  tft.fillScreen(ST7735_BLACK);
  tft.fillRect(5, 5, 50, 50, ST7735_GREEN);
  tft.fillRect(10, 10, 70, 70, ST7735_BLUE);
  tft.fillRect(15, 15, 90, 90, ST7735_RED);
}
void loop()
{
  // nichts zu tun, das Programm
  // läuft nur einmal
}
Calificar la guía

Comentarios

Haz una pregunta o comparte tu experiencia

Schreib uns, wenn etwas nicht funktioniert. Nenne dabei dein Board und die Fehlermeldung.

Revisamos cada comentario antes de publicarlo. Tu dirección de correo electrónico permanece con nosotros.