Difficulty interfacing ST7796S TFT display

I have this version of the display: Amazon.com: Hosyond 4.0 Inches 480x320 TFT Touch Screen LCD Display Module SPI ST7796S Driver for Arduino R3/Mega2560 : Electronics.

I have contacted Hosynod tech support which has proven useless. I finally got the display to work well with the "Adafruit_ST7796S_kbv" library. My problem is getting the touch feature to work. Using the "TFT_touch" library stand-alone, the display responds with reasonable (un-calibrated) values. However when add display object, touch is no longer consistently responsive. Upper left replies after several taps, lower right does not respond at all. Intermittent replies over the other parts of the screen.

I'm using the sketch below with the defined pin assignments. Analog or digital for touch DIN and DOUT makes no difference.

Since the display and touch work independently, I'm guessing there's a conflict. Do I need different libraries or pin assignments? Thanks for any direction you can give me.

type or paste code here#include <SPI.h>
#include "Adafruit_GFX.h"         // Core graphics library
#include "Adafruit_ST7796S_kbv.h" // Hardware-specific library for ST7796S

#define TFT_CS        10
#define TFT_DC         9
#define TFT_RST        8
#define TFT_SDA       11
#define TFT_CLK       13
#define TFT_MISO      12

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

#define DOUT A0  /* Data out pin (T_DO) of touch screen */
#define DIN  A2  /* Data in pin (T_DIN) of touch screen */
#define DCS  5  /* Chip select pin (T_CS) of touch screen */
#define DCLK 6  /* Clock pin (T_CLK) of touch screen */

//#define DOUT 3  /* Data out pin (T_DO) of touch screen */
//#define DIN  4  /* Data in pin (T_DIN) of touch screen */
//#define DCS  5  /* Chip select pin (T_CS) of touch screen */
//#define DCLK 6  /* Clock pin (T_CLK) of touch screen */

#define HMIN 0
#define HMAX 4095
#define VMIN 0
#define VMAX 4095
#define XYSWAP 1 // 0 or 1

#define HRES 480 /* Default screen resulution for X axis */
#define VRES 320 /* Default screen resulution for Y axis */

#include <TFT_Touch.h>

TFT_Touch touch = TFT_Touch(DCS, DCLK, DIN, DOUT);
#define TEXT_SIZE 3

void setup()
{
  Serial.begin(9600);
  while (!Serial);
  delay(1000);

/*
  tft.begin();
 
  Serial.println(F("TFT Initialized"));

  tft.setRotation(3);
  tft.setTextWrap(false);
  tft.setTextSize(TEXT_SIZE);
  tft.fillScreen(ST7796S_BLACK);
  delay(1000);
*/
  Serial.println("Begin setup");
  touch.setCal(HMIN, HMAX, VMIN, VMAX, HRES, VRES, XYSWAP); // Raw xmin, xmax, ymin, ymax, width, height
  touch.setRotation(1);
  Serial.println("Begin example");
}

void loop()
{
  unsigned int X_Raw, Y_Raw;
  int X_Coord, Y_Coord;

  if (touch.Pressed()) {    
    X_Raw = touch.RawX();
    Y_Raw = touch.RawY();

    X_Coord = touch.X();
    Y_Coord = touch.Y();

    Serial.print("Raw x,y = ");
    Serial.print(X_Raw);
    Serial.print(",");
    Serial.println(Y_Raw);

    Serial.print("Coords x,y = ");
    Serial.print(X_Coord);
    Serial.print(",");
    Serial.println(Y_Coord);

    delay(10);
  }
  delay(10);
}


Hello paj418;
I have been working on the same tft screen and , using your code example, have made some progress. I tried to put notes int the sketch that describes the things I did to get it to work.

I have it working on the UNO R4 WIFI and the Nano ESP32.

#include <SPI.h>
#include "Adafruit_GFX.h"         // Core graphics library
#include "Adafruit_ST7796S_kbv.h" // Hardware-specific library for ST7796S

/*
TFT-PINS        UNO R4 WIFI    NANO ESP32
#define TFT_CS        10          10
#define TFT_DC         9           9
#define TFT_RST        8           8
#define TFT_SDA       11          11
#define TFT_CLK       13          13
#define TFT_MISO      12          12

         #define DOUT A0          A0               // Data out pin (T_DO) of touch screen 
         #define DIN  A2          A2               // Data in pin (T_DIN) of touch screen 
         #define DCS  5            5               // Chip select pin (T_CS) of touch screen 
         #define DCLK 6            6              // Clock pin (T_CLK) of touch screen 
Notes: 
1. I had to modify Adafruit_ST7796S_kbv.cpp where it tries to include wiring_private.h to prevent compiler fail.
   commented out this:
    //#ifndef RASPI
    //#include "wiring_private.h"
    //#endif

   Then put this in it's place:

    #if defined(__has_include)
    #if __has_include("wiring_private.h")
    #include "wiring_private.h"
    #endif  // __has_include("wiring_private.h")
    #else  //defined(__has_include)
    #include "wiring_private.h"
    #endif  //defined(__has_include)


*/





#define TFT_CS        10
#define TFT_DC         9
#define TFT_RST        8
#define TFT_SDA       11
#define TFT_CLK       13
#define TFT_MISO      12

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

#define DOUT A0  /* Data out pin (T_DO) of touch screen */
#define DIN  A2  /* Data in pin (T_DIN) of touch screen */
#define DCS  5  /* Chip select pin (T_CS) of touch screen */
#define DCLK 6  /* Clock pin (T_CLK) of touch screen */

//#define DOUT 3  /* Data out pin (T_DO) of touch screen */
//#define DIN  4  /* Data in pin (T_DIN) of touch screen */
//#define DCS  5  /* Chip select pin (T_CS) of touch screen */
//#define DCLK 6  /* Clock pin (T_CLK) of touch screen */

#define HMIN 0
#define HMAX 3840
#define VMIN 0
#define VMAX 3840
#define XYSWAP 1 // 0 or 1

#define HRES 480 /* Default screen resulution for X axis */
#define VRES 320 /* Default screen resulution for Y axis */

#include <TFT_Touch.h>

TFT_Touch touch = TFT_Touch(DCS, DCLK, DIN, DOUT);
#define TEXT_SIZE 2
 unsigned int X_Raw, Y_Raw;
  int X_Coord, Y_Coord;
  //int pressure;
  


void setup()
{
  Serial.begin(115200);
  //while (!Serial);
  delay(1000);


  tft.begin();
 
  Serial.println(F("TFT Initialized"));

  
  tft.setRotation(3);
  tft.setTextWrap(true);
  tft.setTextSize(TEXT_SIZE);
  tft.setTextColor(ST7796S_YELLOW);
  //tft.fillScreen(ST7796S_YELLOW);




  //tft.fillScreen(ST7796S_BLACK);
  
   //tft.fillScreen(ST7796S_BLUE);


   

  //delay(10000);

  Serial.println("Begin setup");
  touch.setCal(HMIN, HMAX, VMIN, VMAX, HRES, VRES, XYSWAP); // Raw xmin, xmax, ymin, ymax, width, height
  touch.setRotation(1);
  Serial.println("Begin example");
  tft.fillScreen(ST7796S_BLACK);// clear the screen
  tft.setCursor(0, 0);
  tft.println("TFT Test");
  //tft.begin();
}

void loop() {
 

  //delay(15);
/* if user touches screen        */

  if (touch.Pressed()) {    
    
    
    X_Raw = touch.RawX();
    Y_Raw = touch.RawY();

    X_Coord = touch.X();
    Y_Coord = touch.Y();


    Serial.print("Coords x,y = ");
    Serial.print(X_Coord);
    Serial.print(",");
    Serial.println(Y_Coord);

    tft.setCursor(40, 40);
    tft.print("Coordinates x,y = ");
    tft.print(X_Coord);
    //tft.print(X_Raw);
    tft.print(",");
    tft.println(Y_Coord);
    tft.println(" ");
      
     //tft.setCursor(150, 5);
     hold();
     delay(100);
  }

  
}

void hold() {
 
 //tft.setCursor(0, 0);
 tft.println("TFT Test  hold");
 tft.println(" ");
 tft.println("ABCDEFGHIJKLMNOPQRTSUVWXYZ12345678901234");
 tft.println(" ");
 tft.println("40 chars in 480 pixels with textsize=2  means characters are 12 pixels wide.");

 tft.println(" ");
 tft.println("  TAP AGAIN TO RECYCLE");
 
while(!touch.Pressed()) {   } //loop here until user taps screen

Serial.println("Tapped Recycle");


tft.fillScreen(ST7796S_BLACK);
tft.setCursor(0, 0);
 tft.println("TFT Test");
}




UPDATE!: I replaced the NANO ESP32 with a NANO EVERY and it worked no different than the NANO ESP32. Just a little slower.

I received your very encouraging email a couple of days ago. I’ll be working with you example tomorrow with an UNO R4 WiFi. I’ll let you know how it goes.

The touch feature is working great now! Thank you so much.

Good to hear. I’m connected to a bme688 also now and showing the information on the screen. I’m going to try it with a gps module after that.