In questo tutorial imparerai come interfacciare e controllare il display TFT GMT020-02 con un ESP32. Il display GMT020-02 utilizza il driver IC ST7789, quindi questo tutorial dovrebbe essere utile anche per altri display TFT con lo stesso driver IC.
Il GMT020-02 è un bel piccolo display TFT da 2,0″ con una risoluzione di 240×320 pixel e 262K colori che puoi acquistare a basso costo su AliExpress. Tuttavia, la documentazione è scarsa e a volte anche fuorviante, il che rende l’uso del display inutilmente difficile. Spero che questo piccolo tutorial ti sia d’aiuto.
Caratteristiche del display GMT020-02
Il GMT020-02 Vers 1.1 è un modulo display con interfaccia SPI integrata. L’immagine qui sotto mostra il fronte e il retro del modulo.

Lo puoi trovare su AliExpress, dove è etichettato come dotato di driver IC ST7735 e adatto per Arduino.

Driver IC per il display GMT020-02
Questo è fuorviante poiché il datasheet indica che il driver IC è un ST7789 e che il modulo funziona a 3,3V. Secondo lo schema elettrico non è presente alcun convertitore di livello sul modulo, il che rende rischioso collegarlo a un Arduino UNO con logica a 5V. I datasheet per il modulo GMT020-02 e per l’ST7789 sono linkati qui sotto.
Retroilluminazione del display GMT020-02
Un accenno anche alla retroilluminazione. Il modulo GMT020-02 ha una retroilluminazione ma purtroppo sembra essere cablata direttamente (vedi LED+ e LED- nello schema sotto) e quindi non può essere controllata via software. Noterai che non c’è nemmeno un pin BLK o LED che permetta di controllare la retroilluminazione esternamente.

L’unico modo sarebbe spegnere completamente l’alimentazione del modulo. Ma dato che il LED della retroilluminazione assorbe già 60mA, non puoi fornire VCC direttamente da un’uscita GPIO! Dovrai usare un transistor/MOSFET per commutare l’alimentazione (VCC)!
Interfaccia di comunicazione del display GMT020-02
A proposito, i pin IM0, IM1 e IM2 nello schema sopra servono per selezionare l’interfaccia di comunicazione (SPI o parallelo 8 bit). Dalla tabella qui sotto e dallo schema si vede che il modulo è cablato per usare SPI:

Collegare il display GMT020-02 con ESP32
Collegare il GMT020-02 a un ESP32 è semplice ma la denominazione dei pin sul GMT020-02 è un po’ confusa. Mostra i pin SDA e SCL ma dato che usa un’interfaccia SPI, questi non sono i pin SDA e SCL per I2C!
Lo schema di collegamento seguente mostra come collegare il GMT020-02 a un WEMOS Lolin32 lite:

E qui, per comodità, le connessioni richieste in tabella:
| Display | ESP32 |
|---|---|
| CS / SS | 5 |
| RST | 17 |
| DC | 16 |
| SDA / MOSI | 23 |
| SCL / SCLK | 18 |
| GND | GND |
| VCC | 3.3V |
A seconda dell’ESP32 e della scheda di sviluppo che usi, questi pin possono variare. Dovresti usare i pin per SPI hardware. Puoi trovarli selezionando la tua scheda nell’IDE Arduino e poi eseguendo il codice qui sotto.
void setup() {
Serial.begin(115200);
Serial.print("MOSI: ");
Serial.println(SDA / MOSI);
Serial.print("MISO: ");
Serial.println(MISO);
Serial.print("SCL / SCK: ");
Serial.println(SCK);
Serial.print("CS / SS: ");
Serial.println(SS);
}
void loop() { }
Stampa i pin necessari per SPI hardware. Gli altri pin puoi sceglierli liberamente.
Codice di test per il display GMT020-02
Useremo la Adafruit-ST7735 Library per scrivere e disegnare sul display GMT020-02. Per installarla apri il Library Manager, cerca “Adafruit-ST7735” e clicca sul pulsante verde INSTALL:

L’installer probabilmente ti chiederà di installare anche le dipendenze. Clicca su INSTALL ALL:

Con la libreria installata, possiamo ora testare il display. Compila e carica il codice seguente. Stampa il testo “Makerguides” sul display:
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
// WEMOS LOLIN32 lite
#define TFT_CS 5
#define TFT_RST 17
#define TFT_DC 16
#define TFT_MOSI 23 // SDA, HW MOSI
#define TFT_SCLK 18 // SCL, HW SCLK
// #define TFT_MISO 19 // not used
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); // Hardware SPI
void setup() {
tft.init(240, 320);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(3);
tft.setCursor(50, 100);
tft.println("Makerguides");
}
void loop() {
}
Se hai collegato il display ai pin SPI hardware corretti del tuo ESP32, in realtà non devi definire TFT_MOSI e TFT_SCLK e puoi usare il costruttore seguente (come nel codice sopra):
Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
Lo SPI hardware è più veloce ma se hai problemi puoi provare lo SPI software definendo i pin TFT_MOSI e TFT_SCLK e usandoli nel costruttore.
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
In ogni caso, l’output sul display dovrebbe apparire così:

Se funziona, puoi eseguire il codice graphicstest_st7789 fornito con la Adafruit-ST7735 Library. Assicurati solo di selezionare il display corretto nel codice e di modificare i pin in base al tuo cablaggio:
// OPTION 1 (recommended) is to use the HARDWARE SPI pins, which are unique // to each board and not reassignable. Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST); // OPTION 2 lets you interface the display using ANY TWO or THREE PINS, // tradeoff being that performance is not as fast as hardware SPI above. //Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup(void) {
Serial.begin(9600);
Serial.print(F("Hello! ST77xx TFT Test"));
// Use this initializer (uncomment) if using a 1.3" or 1.54" 240x240 TFT:
// tft.init(240, 240); // Init ST7789 240x240
// OR use this initializer (uncomment) if using a 1.69" 280x240 TFT:
//tft.init(240, 280); // Init ST7789 280x240
// OR use this initializer (uncomment) if using a 2.0" 320x240 TFT:
tft.init(240, 320); // Init ST7789 320x240
// OR use this initializer (uncomment) if using a 1.14" 240x135 TFT:
//tft.init(135, 240); // Init ST7789 240x135
// OR use this initializer (uncomment) if using a 1.47" 172x320 TFT:
//tft.init(172, 320); // Init ST7789 172x320
// OR use this initializer (uncomment) if using a 1.9" 170x320 TFT:
//tft.init(170, 320); // Init ST7789 170x320
Display GMT020-02 con la libreria TFT_eSPI
Se vuoi usare la TFT_eSPI library invece della Adafruit-ST7735 Library è possibile. Ecco il file tft_setup.h con la configurazione richiesta (di nuovo per un WEMOS Lolin32 lite):
#define ST7789_DRIVER #define TFT_WIDTH 240 #define TFT_HEIGHT 320 #define TFT_RGB_ORDER TFT_BGR // WEMOS Lolin32 lite #define TFT_CS 5 #define TFT_RST 17 #define TFT_DC 16 #define TFT_MOSI 23 // SDA // HW MOSI #define TFT_SCLK 18 // SCL // HW SCLK // #define TFT_MISO 19 // not used #define LOAD_GLCD // Font 1. Original Adafruit 8 pixel font needs ~1820 bytes in FLASH #define LOAD_FONT2 // Font 2. Small 16 pixel high font, needs ~3534 bytes in FLASH, 96 characters #define LOAD_FONT4 // Font 4. Medium 26 pixel high font, needs ~5848 bytes in FLASH, 96 characters #define LOAD_FONT6 // Font 6. Large 48 pixel font, needs ~2666 bytes in FLASH, only characters 1234567890:-.apm #define LOAD_FONT7 // Font 7. 7 segment 48 pixel font, needs ~2438 bytes in FLASH, only characters 1234567890:. #define LOAD_FONT8 // Font 8. Large 75 pixel font needs ~3256 bytes in FLASH, only characters 1234567890:-. #define LOAD_GFXFF // FreeFonts. Include access to the 48 Adafruit_GFX free fonts FF1 to FF48 and custom fonts #define SMOOTH_FONT #define SPI_FREQUENCY 40000000 #define SPI_READ_FREQUENCY 20000000
e qui un esempio di codice che, come prima, stampa il testo “Makerguides” sul display:
#include "tft_setup.h"
#include"TFT_eSPI.h"
TFT_eSPI tft = TFT_eSPI();
void setup(void) {
tft.init();
tft.fillScreen(TFT_BLACK);
tft.setRotation(1);
tft.setCursor(100, 100, 2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
tft.println("Makerguides");
}
void loop() { }
Ricorda che il file tft_setup.h deve far parte della cartella del progetto. Se ti servono più dettagli dai un’occhiata al tutorial How to configure TFT_eSPI Library for TFT display.
Conclusione
In questo tutorial hai imparato come interfacciare e controllare il display TFT GMT020-02 con un ESP32.
Per altri display TFT dai un’occhiata ai tutorial Interface TFT ILI9341 Touch Display with ESP32,
Interface TFT ST7735 Display with ESP32, Digital Clock on CrowPanel 1.28″ Round Display e CrowPanel 2.8″ ESP32 Display : Easy Setup Guide.
Se hai commenti, sentiti libero di lasciarli nella sezione commenti.
Buon divertimento con il tinkering ; )

