Bei­spiel­pro­gramm TFT-Display

Lese­zeit: 4 Minu­ten

Das Bei­spiel­pro­gramm zeigt die Mög­lich­kei­ten ein TFT-Dis­play mit Hil­fe einer Biblio­thek anzusteuern.

Adafruit Pin­be­le­gung

Pin TFTUNO R3/R4 NanoESP32 WROOMWemos D1 Mini
GndGNDGNDGND
VCC5V5V5V
DC92D8
CARD_CS
TFT_CS105D0
SDO1123D7
SCK1318D5
SDI
LITE5V5V5V

1,77 Zoll Pinbelegung

Pin TFTUNO R3/R4 NanoESP32 WROOMWemos D1 Mini
GndGNDGNDGND
VCC5V5V5V
SCK1318D5
SDA1123D7
RES822D1
RS92D8
CS105D0
LEDA3,3V3,3V3,3V

TFT Wavesha­re Pinbelegung

Pin TFTUNO R3/R4 NanoESP32 WROOMWemos D1 Mini
VCC5V5V5V
GNDGNDGNDGND
DIN1123D7
CLK1318D5
CS105D0
DC92D8
RST822D1
BL5V5V5V

Benö­tig­te Biblio­the­ken TFT 1,77 Zoll/1,8 Zoll:

Funk­tio­nen der Biblio­thek Adafruit ST7735/TFT

Schlüs­sel­wortPara­me­terAkti­on
begin()TFT star­ten
initR(initR(INITR_*TAB););BLACKTAB
GREENTAB
REDTAB
Farb­sche­ma bestimmen
setRotation(Richtung);Rich­tung = 0 → nicht drehen
Rich­tung = 1 → 90° drehen
Rich­tung = 2 → 180° drehen
Rich­tung = 3 → 270 ° drehen
Bild­schirm ausrichten
fillScreen(Farbe);Stan­dard­far­ben:
ST7735_BLACK
ST7735_WHITE
ST7735_GREEN
ST7735_RED
ST7735_BLUE
ST7735_YELLOW
ST7735_ORANGE
ST7735_MAGENTA
ST7735_CYAN
Bild­schirm­hin­ter­grund
drawLine(StartX, Star­tY, End­eX, EndeY, Farbe);Linie zeich­nen
drawFastHLine(StartX, Star­tY, Län­ge, Farbe);hori­zon­ta­le Linie zeichnen
drawFastVLine(StartX, Star­tY, Län­ge, Farbe);ver­ti­ka­le Linie zeichnen
drawRect(StartX, Star­tY,, Brei­te, Höhe, Farbe);Recht­eck zeichnen
drawRoundRect(StartX, Star­tY, Brei­te, Höhe, Ecken­ra­di­us, Farbe);abge­run­de­tes Recht­eck zeichnen
fillRect(StartX, Star­tY, Brei­te, Höhe, Füllfarbe);aus­ge­füll­tes Recht­eck zeichnen
drawCircle(MittelpunkX, Mit­tel­punk­tY, Radi­us, Farbe);Kreis zeich­nen
fillCircle(MittelpunktX, Mit­tel­punk­tY, Radi­us, Füllfarbe);Aus­ge­füll­ten Kreis zeichnen
setCursor(x, y);Cur­sor setzen
setTextSize(Textgröße);Text­grö­ße:
1 - 4
Text­grö­ße bestimmen
setTextColor(Farbe);Text­far­be setzen
print("Text"); println("Text");Text schrei­ben
setTextWrap(true/false);fal­se → Text fließt über den Rand des TFTs hinaus
true → Text wird am Ende umgebrochen
Zei­len­um­bruch

Beach­te die unter­schied­li­che Defi­ni­ti­on der Varia­blen und den Auf­ruf der Biblio­thek Adafruit!

Pro­gramm für den Ardui­no UNO

// Bibliotheken einbinden
# include "Adafruit_GFX.h"
# include "Adafruit_ST7735.h"

/* 
  1,77 Zoll TFT
  # define TFT_PIN_CS  10   
  # define TFT_PIN_DC   9 
  # define TFT_PIN_RST  8  
  Adafruit_ST7735 tft = Adafruit_ST7735(TFT_PIN_CS, TFT_PIN_DC, TFT_PIN_RST); 
*/

// Adafruit TFT, WaveShare TFT 1,8 Zoll
# define TFT_CS       10
# define TFT_RST       9
# define TFT_DC        8

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

void setup()
{
  Serial.begin(9600);
  delay(500);
  Serial.println("Bildschirm: " + String(tft.height()) + " x " + String(tft.width()));

  tft.initR(INITR_BLACKTAB);

  // Rotation anpassen
  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);
  for (int i = 1; i < tft.height(); i+=10)
  {
    tft.drawLine(1, i, tft.width(), i, ST7735_ORANGE);
  }
  delay(2000);

  // Kreise zeichnen
  tft.fillScreen(ST7735_BLACK);
  tft.fillCircle(tft.width() / 2, tft.height() / 2, 50, ST7735_MAGENTA);
  tft.fillCircle(tft.width() / 2, tft.height() / 2, 30, ST7735_GREEN);
  tft.fillCircle(tft.width() / 2, tft.height() / 2, 10, 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
}

Benö­tig­te Biblio­the­ken Wavesha­re 2,4 Zoll

Funk­tio­nen der Biblio­thek Adafruit ILI9341

Schlüs­sel­wortPara­me­terAkti­on
begin()TFT star­ten
setRotation(Richtung);Rich­tung = 0 → nicht drehen
Rich­tung = 1 → 90° drehen
Rich­tung = 2 → 180° drehen
Rich­tung = 3 → 270 ° drehen
Bild­schirm ausrichten
fillScreen(Farbe);Stan­dard­far­ben:
ILI9341_BLACK
ILI9341_WHITE
ILI9341_GREEN
ILI9341_DARKGREEN
ILI9341_RED
ILI9341_BLUE
ILI9341_NAVY
ILI9341_CASET
ILI9341_YELLOW
ILI9341_ORANGE
ILI9341_MAGENTA
ILI9341_CYAN
ILI9341_DARKCYAN
ILI9341_LIGHTGREY
ILI9341_DARKGREY
ILI9341_GREENYELLOW
ILI9341_MAROON
Bild­schirm­hin­ter­grund
drawLine(StartX, Star­tY, End­eX, EndeY, Farbe);Linie zeich­nen
drawFastHLine(StartX, Star­tY, Län­ge, Farbe);hori­zon­ta­le Linie zeichnen
drawFastVLine(StartX, Star­tY, Län­ge, Farbe);ver­ti­ka­le Linie zeichnen
drawRect(StartX, Star­tY,, Brei­te, Höhe, Farbe);Recht­eck zeichnen
drawRoundRect(StartX, Star­tY, Brei­te, Höhe, Ecken­ra­di­us, Farbe);abge­run­de­tes Recht­eck zeichnen
fill.Rect(StartX, Star­tY, Brei­te, Höhe, Füllfarbe);aus­ge­füll­tes Recht­eck zeichnen
drawCircle(MittelpunkX, Mit­tel­punk­tY, Radi­us, Farbe);Kreis zeich­nen
fillCircle(MittelpunktX, Mit­tel­punk­tY, Radi­us, Füllfarbe);Aus­ge­füll­ten Kreis zeichnen
setCursor(x, y);Cur­sor setzen
setTextSize(Textgröße);Text­grö­ße:Text­grö­ße setzen
setTextColor(Farbe);Text­far­be bestimmen
color565(rot, grün, blau);Far­be als RGB setzen
print("Text"); println("Text");Text schrei­ben
setTextWrap(true/false);fal­se → Text fließt über den Rand des TFTs hinaus
true → Text wird am Ende umgebrochen
Zei­len­um­bruch

So sieht es aus:

Pro­gramm für den Ardui­no UNO

# include "Adafruit_GFX.h"
# include "Adafruit_ILI9341.h"

// SPI-Pins
# define TFT_CS        10
# define TFT_RST        9
# define TFT_DC         8

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

// Farben RGB
int ROT = 255, GRUEN = 255, BLAU = 255;

void setup()
{
  // Zufallsgenerator starten
  randomSeed(analogRead(0));
  Serial.begin(9600);
  delay(500);
  Serial.println("Bildschirm: " + String(tft.height()) + " x " + String(tft.width()));

  // TFT starten
  tft.begin();

  // Rotation anpassen
  tft.setRotation(2);

  // schwarzer Hintergrund
  tft.fillScreen(ILI9341_BLACK);

  tft.setTextSize(1);
  tft.setCursor(1, 5);
  tft.setTextColor(ILI9341_BLUE);
  tft.print("Text");
  delay(500);

  tft.setTextSize(3);
  tft.setCursor(1, 40);
  tft.setTextColor(ILI9341_GREEN);
  tft.print("Text");
  delay(500);

  tft.setTextSize(5);
  tft.setCursor(1, 70);
  tft.setTextColor(ILI9341_RED);
  tft.print("Text");
  delay(500);

  tft.setTextSize(7);
  tft.setCursor(1, 120);
  tft.setTextColor(ILI9341_YELLOW);
  tft.print("Text");

  tft.setTextSize(9);
  tft.setCursor(1, 200);
  tft.setTextColor(ILI9341_LIGHTGREY);
  tft.print("Text");
  delay(2000);

  // zufällige Pixel
  tft.fillScreen(ILI9341_BLACK);
  for  (int i = 0; i < 700; i ++)
  {
    int PixelX = random(1, tft.width());
    int PixelY = random(1, tft.height());
    tft.drawPixel(PixelX, PixelY, tft.color565(random(ROT),random(GRUEN),random(BLAU)));
    delay(5);
  }
  delay(2000);

  // Linien ziehen
  tft.fillScreen(ILI9341_BLACK);
  for (int i = 1; i < tft.height(); i+=10)
  {
    tft.drawLine(1, i, tft.width(), i, ILI9341_ORANGE);
  }
  delay(2000);

  // Kreise vom Mittelpunkt zeichnen
  tft.fillScreen(ILI9341_BLACK);
  for (int i = 1; i < tft.width() / 2; i+=10)
  {
    tft.fillCircle(tft.width() / 2, tft.height() / 2, tft.width() / 2 - i, tft.color565(random(ROT),random(GRUEN),random(BLAU)));
    delay(50);
  }
  delay(2000);

  // Rechtecke zeichnen
  tft.fillScreen(ILI9341_BLACK);
  for (int i = 1; i < tft.width(); i+=10)
  {
    tft.drawRect(tft.width() / 2 - i / 2, tft.height() / 2 - i / 2 , i, i, tft.color565(random(ROT),random(GRUEN),random(BLAU)));
  }
  delay(2000);

   // ausgefüllte Rechtecke zeichnen
  tft.fillScreen(ILI9341_BLACK);
  for (int i = 1; i < tft.width() / 2; i+=10)
  {
    tft.fillRect(i, i, i, i, tft.color565(random(ROT),random(GRUEN),random(BLAU)));
    delay(50);
  }
  delay(2000);

  // Dreiecke
  tft.fillScreen(ILI9341_BLACK);
  for (int i = 1; i <tft.width(); i+=10)
  {
    tft.fillTriangle(i, i, 100, 100, 1, tft.width(), tft.color565(random(ROT),random(GRUEN),random(BLAU)));
    delay(50);
  }
  delay(2000);
}
void loop()
{
  // nichts zu tun, das Programm
  // läuft nur einmal
}

Erwei­ter­tes Beispiel

So sieht es aus:


Letzte Aktualisierung: 30. Nov 2023 @ 10:55