I would like to bring to the readers’ attention an apparent conflict between graphic and touch functions relating to a 4”, 480x320 px, tft touch screen display. This display is provided with ST7796S driver and XPT2046 touch controller and is available at:
https://it.aliexpress.com/item/4001122632293.html?spm=a2g0s.9042311.0.0.43634c4dxl9z6F
I have chosen this display because of its dimension which allows displaying all needed information in a clearly readable size, including a few buttons for setting purposes which require a working touch function. At the same link clear images of both side of the display are available and pin names are well visible.
On a breadboard I have placed an ESP32 WROOM MCU and the display and connected the two as follows:
Graphics
CS–>GPIO15; RESET –>GPIO2; DC –>GPIO4; SDI(MOSI)–> GPIO23; SCK–> GPIO18; LED–> pin 3V3; SDO(MISO)–> GPIO19.
Touchscreen
T_CLK–> GPIO18; T_CS–> GPIO1; T_DIN–> GPIO23; TDO–>GPIO19; T_IRQ not connected
I have carried out my tests using 4 sketches based on examples proposed by the various libraries I have used.
While the graphic and touch functions perform well individually, I am encountering serious problems in having them working together.
The test result are as follows:
-SKETCH 1
.libraries: SPI.h, XPT2046_Touchscreen.h
.result: touch function performs well
-SKETCH 2
.libraries: SPI.h, TFT_eSPI.h
.result: graphics performs well. No touch response, on serial monitor x=0, y=0, z=4095
-SKETCH 3
.libraries: SPI.h, TFT_eSPI.h, XPT2046_Touchscreen.h
.result: Graphics OK, no touch response, on serial monitor x=0, y=0, z=4095
-SKETCH 4
.libraries: SPI.h, Adafruit_GFX.h, Adafruit_ST7796S_kbv.h, XPT2046_Touchscreen.h
.result:
.if tft.begin() is uncommented and touch.begin commented then graphics is ok
.if tft.begin() is commented and touch.begin uncommented then touch is ok
.if both are uncommented and tft.begin() precedes touch.begin() in the code sequence
then graphics is ok, no touch response and serial monitor shows x=0, y=0, z=4095
.if viceversa there is no graphics and no touch response and on monitor x=0, y=0, z=4095
I like to point out here that the same 4 sketches provide both graphic and touch proper functionalities with a similar display of the same make and pinout but of the type ILI9341 2.8" 240x380 px, available at the same link cited above.
Here are the four sketches:
//SKETCH 1
#include <SPI.h>
#include <XPT2046_Touchscreen.h>
#define CS_PIN 21
XPT2046_Touchscreen ts(CS_PIN);
void setup() {
Serial.begin(115200);
ts.begin();
ts.setRotation(1);
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
delay(30);
Serial.println();
}
}
//SKETCH 2
#include <SPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
void setup(void) {
Serial.begin(115200);
Serial.println("\n\nStarting...");
tft.init();
tft.fillScreen(TFT_BLACK);
tft.setRotation(1);
tft.drawRoundRect(0, 0, 480 , 320, 4, TFT_WHITE);
tft.drawRoundRect(5, 5, 470 , 100, 4, TFT_RED);
tft.drawRoundRect(5,108, 470 , 53, 4, TFT_GREEN);
tft.drawRoundRect(5, 165, 122, 71, 4, TFT_YELLOW);
tft.drawRoundRect(131, 165,184 ,71,4, TFT_CYAN);
}
void loop() {
uint16_t x, y;
static uint16_t color;
if (tft.getTouch(&x, &y)) {
tft.setCursor(5, 5, 2);
tft.printf("x: %i ", x);
tft.setCursor(5, 20, 2);
tft.printf("y: %i ", y);
tft.drawPixel(x, y, color);
color += 155;
}
}
//SKETCH 3
#include <SPI.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>
#define CS_PIN 21
XPT2046_Touchscreen ts(CS_PIN);
TFT_eSPI tft = TFT_eSPI();
void setup() {
Serial.begin(115200);
tft.init();
ts.begin();
tft.fillScreen(TFT_BLACK);
tft.setRotation(1);
tft.drawRoundRect(0, 0, 480 , 320, 4, TFT_WHITE);
tft.drawRoundRect(5, 5, 470 , 100, 4, TFT_RED);
tft.drawRoundRect(5,108, 470 , 53, 4, TFT_GREEN);
tft.drawRoundRect(5, 165, 122, 71, 4, TFT_YELLOW);
tft.drawRoundRect(131, 165,184 ,71,4, TFT_CYAN);
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
delay(30);
Serial.println();
}
}
//[u][b]SKETCH 4[/b][/u]
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ST7796S_kbv.h"
#include <XPT2046_Touchscreen.h>
#define TFT_DC 4
#define TFT_CS 15
#define TFT_RST 2
#define TFT_MISO 19
#define TFT_MOSI 23
#define TFT_CLK 18
#define TOUCH_CS_PIN 21
Adafruit_ST7796S_kbv tft = Adafruit_ST7796S_kbv( TFT_CS,TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);//
XPT2046_Touchscreen touch( TOUCH_CS_PIN);//
void setup() {
Serial.begin( 115200 );
tft.begin();
touch.begin();
tft.setRotation(1);
tft.fillScreen(ST7796S_BLACK);
tft.drawRoundRect(0, 0, 480 , 320, 4, ST7796S_WHITE);
tft.drawRoundRect(5, 5, 470 , 100, 4, ST7796S_RED);
tft.drawRoundRect(5,108, 470 , 53, 4, ST7796S_GREEN);
tft.drawRoundRect(5, 165, 122, 71, 4, ST7796S_YELLOW);
tft.drawRoundRect(131, 165,184 ,71,4, ST7796S_CYAN);
}
TS_Point rawLocation;
void loop() {
while ( touch.touched() )
{
rawLocation = touch.getPoint();
Serial.print("x = ");
Serial.print(rawLocation.x);
Serial.print(", y = ");
Serial.print(rawLocation.y);
Serial.print(", z = ");
Serial.println(rawLocation.z);
}
}