ESP32-2432s028r (Yellow Display-CYD -TFT-eSPI Bodmer library-touch problem

Hello
I have a problem with the "touch-calibrate" code from Bodmer's TFT-eSPI library.
I'll start in order:
I have a Cheap Yellow Display CYD module (ESP32-2432S028R)

(link to data)
Sunton Display url

I have the User_Setup.h file configured correctly (I think).
But after loading the code into Esp32, the display displays correctly but the touch does not respond.

I use this code:

/*
  Sketch to generate the setup() calibration values, these are reported
  to the Serial Monitor.

  The sketch has been tested on the ESP8266 and screen with XPT2046 driver.
*/


#include <SPI.h>
#include <TFT_eSPI.h>      // Hardware-specific library





TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

//------------------------------------------------------------------------------------------

void setup() {
  // Use serial port
  Serial.begin(115200);

  // Initialise the TFT screen
  tft.init();

  // Set the rotation to the orientation you wish to use in your project before calibration
  // (the touch coordinates returned then correspond to that rotation only)
  tft.setRotation(1);

  // Calibrate the touch screen and retrieve the scaling factors
  touch_calibrate();

/*
  // Replace above line with the code sent to Serial Monitor
  // once calibration is complete, e.g.:
  uint16_t calData[5] = { 286, 3534, 283, 3600, 6 };
  tft.setTouch(calData);
*/

  // Clear the screen
  tft.fillScreen(TFT_BLACK);
  tft.drawCentreString("Touch screen to test!",tft.width()/2, tft.height()/2, 2);
}

//------------------------------------------------------------------------------------------

void loop(void) {
  uint16_t x = 0, y = 0; // To store the touch coordinates

  // Pressed will be set true is there is a valid touch on the screen
  bool pressed = tft.getTouch(&x, &y);

  // Draw a white spot at the detected coordinates
  if (pressed) {
    tft.fillCircle(x, y, 2, TFT_WHITE);
    //Serial.print("x,y = ");
    //Serial.print(x);
    //Serial.print(",");
    //Serial.println(y);
  }
}

//------------------------------------------------------------------------------------------

// Code to run a screen calibration, not needed when calibration values set in setup()
void touch_calibrate()
{
  uint16_t calData[5];
  uint8_t calDataOK = 0;

  // Calibrate
  tft.fillScreen(TFT_BLACK);
  tft.setCursor(20, 0);
  tft.setTextFont(2);
  tft.setTextSize(1);
  tft.setTextColor(TFT_WHITE, TFT_BLACK);

  tft.println("Touch corners as indicated");

  tft.setTextFont(1);
  tft.println();

  tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);

  Serial.println(); Serial.println();
  Serial.println("// Use this calibration code in setup():");
  Serial.print("  uint16_t calData[5] = ");
  Serial.print("{ ");

  for (uint8_t i = 0; i < 5; i++)
  {
    Serial.print(calData[i]);
    if (i < 4) Serial.print(", ");
  }

  Serial.println(" };");
  Serial.print("  tft.setTouch(calData);");
  Serial.println(); Serial.println();

  tft.fillScreen(TFT_BLACK);
  
  tft.setTextColor(TFT_GREEN, TFT_BLACK);
  tft.println("Calibration complete!");
  tft.println("Calibration code sent to Serial port.");

  delay(4000);
}

But touch don't working.

I have a question about pin configuration.
My module has this Pin configuration for screen and touch.


In User_setup.h I configured the display pins. And where should I configure the pins for touch???

When I use this code to handle touch, everything works fine. But I need to run the code I showed earlier. Unfortunately, no attempts have yielded positive results.

/*  Rui Santos & Sara Santos - Random Nerd Tutorials
    THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:
    1) ESP32-2432S028R 2.8 inch 240×320 also known as the Cheap Yellow Display (CYD): https://makeradvisor.com/tools/cyd-cheap-yellow-display-esp32-2432s028r/
      SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/cyd/
    2) REGULAR ESP32 Dev Board + 2.8 inch 240x320 TFT Display: https://makeradvisor.com/tools/2-8-inch-ili9341-tft-240x320/ and https://makeradvisor.com/tools/esp32-dev-board-wi-fi-bluetooth/
      SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/esp32-tft/
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

#include <SPI.h>

/*  Install the "TFT_eSPI" library by Bodmer to interface with the TFT Display - https://github.com/Bodmer/TFT_eSPI
    *** IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***
    *** YOU MUST USE THE User_Setup.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***
    FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://RandomNerdTutorials.com/cyd/ or https://RandomNerdTutorials.com/esp32-tft/   */
#include <TFT_eSPI.h>

// Install the "XPT2046_Touchscreen" library by Paul Stoffregen to use the Touchscreen - https://github.com/PaulStoffregen/XPT2046_Touchscreen
// Note: this library doesn't require further configuration
#include <XPT2046_Touchscreen.h>

TFT_eSPI tft = TFT_eSPI();

// Touchscreen pins
#define XPT2046_IRQ 36   // T_IRQ
#define XPT2046_MOSI 32  // T_DIN
#define XPT2046_MISO 39  // T_OUT
#define XPT2046_CLK 25   // T_CLK
#define XPT2046_CS 33    // T_CS

SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define FONT_SIZE 2

// Touchscreen coordinates: (x, y) and pressure (z)
int x, y, z;

// Print Touchscreen info about X, Y and Pressure (Z) on the Serial Monitor
void printTouchToSerial(int touchX, int touchY, int touchZ) {
  Serial.print("X = ");
  Serial.print(touchX);
  Serial.print(" | Y = ");
  Serial.print(touchY);
  Serial.print(" | Pressure = ");
  Serial.print(touchZ);
  Serial.println();
}

// Print Touchscreen info about X, Y and Pressure (Z) on the TFT Display
void printTouchToDisplay(int touchX, int touchY, int touchZ) {
  // Clear TFT screen
  tft.fillScreen(TFT_WHITE);
  tft.setTextColor(TFT_BLACK, TFT_WHITE);

  int centerX = SCREEN_WIDTH / 2;
  int textY = 80;
 
  String tempText = "X = " + String(touchX);
  tft.drawCentreString(tempText, centerX, textY, FONT_SIZE);

  textY += 20;
  tempText = "Y = " + String(touchY);
  tft.drawCentreString(tempText, centerX, textY, FONT_SIZE);

  textY += 20;
  tempText = "Pressure = " + String(touchZ);
  tft.drawCentreString(tempText, centerX, textY, FONT_SIZE);
}

void setup() {
  Serial.begin(115200);

  // Start the SPI for the touchscreen and init the touchscreen
  touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
  touchscreen.begin(touchscreenSPI);
  // Set the Touchscreen rotation in landscape mode
  // Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 3: touchscreen.setRotation(3);
  touchscreen.setRotation(1);

  // Start the tft display
  tft.init();
  // Set the TFT display rotation in landscape mode
  tft.setRotation(1);

  // Clear the screen before writing to it
  tft.fillScreen(TFT_WHITE);
  tft.setTextColor(TFT_BLACK, TFT_WHITE);
  
  // Set X and Y coordinates for center of display
  int centerX = SCREEN_WIDTH / 2;
  int centerY = SCREEN_HEIGHT / 2;

  tft.drawCentreString("Hello, world!", centerX, 30, FONT_SIZE);
  tft.drawCentreString("Touch screen to test", centerX, centerY, FONT_SIZE);
}

void loop() {
  // Checks if Touchscreen was touched, and prints X, Y and Pressure (Z) info on the TFT display and Serial Monitor
  if (touchscreen.tirqTouched() && touchscreen.touched()) {
    // Get Touchscreen points
    TS_Point p = touchscreen.getPoint();
    // Calibrate Touchscreen points with map function to the correct width and height
    x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
    y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
    z = p.z;

    printTouchToSerial(x, y, z);
    printTouchToDisplay(x, y, z);

    delay(100);
  }
}

I also add the User_Setup.h file to show how my pins are configured.

User_Setup.h (18.6 KB)

Please give me some advice. I suspect that the problem is the pin configuration.

If the nerds sample works, just look for the differences related to the screen with your non-working code and change accordingly.

When you first run touch_calibrate(), you need to copy the value displayed on the serial monitor to uint16_t calData[5] = {...}, enable the commented out code, and then pass it to tft.setTouch().

//*
  // Replace above line with the code sent to Serial Monitor
  // once calibration is complete, e.g.:
  uint16_t calData[5] = { 286, 3534, 283, 3600, 6 }; // <== copy the values when you have run `touch_calibrate()`
  tft.setTouch(calData);
//*/

Yes, I know I have to use the value from the terminal.
Unfortunately the code doesn't work properly. Nothing is displayed in the terminal. The code seems to stop at function:

touch_calibrate();

and it does not come out of it to...

void setup()

display the "welcome" beginning

  // Clear the screen
  tft.fillScreen(TFT_BLACK);
  tft.drawCentreString("Touch screen to test!",tft.width()/2, tft.height()/2, 2);

Where do I need to set the pins responsible for touch??? Because there's probably a problem here.

this calibration fragment:

  uint16_t calData[5] = { 286, 3534, 283, 3600, 6 };
  tft.setTouch(calData);

I need it to properly calibrate touch in LVGL for the EEZ STUDIO program. Everything related to the display works fine but the touch is dead.

Nothing is displayed in the terminal because the condition is not met

 if(pressed){
  bool pressed = tft.getTouch(&x, &y);

  // Draw a white spot at the detected coordinates
  if (pressed) {
    tft.fillCircle(x, y, 2, TFT_WHITE);
    Serial.print("x,y = ");
    Serial.print(x);
    Serial.print(",");
    Serial.println(y);
  }

Because I probably have unconfigured pins related to touch.

Unfortunately, I don't know how the touch SPI is connected in the CYD circuit diagram, but I think it's probably the same as the display. In other words, I think the CS pin switches between the display and touch.

In my case, the display type is different from yours, but the SPI_TOUCH_FREQUENCY setting is significantly different from the example that works with the combination of ESP32 and TFT_eSPI.

In your User_Setup.h, it is set as follows.

// The XPT2046 requires a lower SPI clock rate of 2.5MHz so we define that here:
#define SPI_TOUCH_FREQUENCY 2500000

I looked at the XPT2046 datasheet, but unfortunately I couldn't find any mention of 2.5MHz being appropriate. I think this is the original,
but in my case, I remember it not working at 2.5MHz. So I set it at 250KHz which works fine.

Also, the XPT2046_Touchscreen that you said worked is set to 2MHz.

#define SPI_SETTING SPISettings(2000000, MSBFIRST, SPI_MODE0)

So, why not try lowering SPI_TOUCH_FREQUENCY a little?

1 Like

This was my misunderstanding.

I looked into some articles on CYD, and for the touchscreen, they don't use the touch function of TFT_eSPI, but use XPT2046_Touchscreen.

I think the reason is that MOSI, MISO, etc. are not common with TFT. This was also clear from the image you attached.

Unfortunately, I tried to find a way to set the touchscreen to a different SPI from the display in TFT_eSPI, but I couldn't find it.

You may need to reconsider why you need to use TFT_eSPI for touch functionality.

1 Like

I found this in TFT_eSPI discussion:

1 Like

FWIW, my Sunton 5" display uses the GT911 library
/* uncomment for GT911 */

I use LVGL with it, not TFT_eSPI and I never bothered to calibrate. Taking a quick glance at the code, I just call

    if (touch_has_signal())
    {
        if (touch_touched())
        {
            ... do stuff
        }
   }
1 Like

My module is 2.8 inches. I have documentation for this module. I am attaching the connection diagrams.
According to the examples I have for this module, all configurations of the User_setup file will recommend using ILI9341.

Do you have any sample code for this module?

I don't really need to use TFT_eSPI. I need this to use LVGL and EEZ STUDIO. But in fact, I would prefer to skip ArduinoIDE altogether and use, for example, EEZ STUDIO and Eclipse. I know this environment, but I don't know how to use files generated in EEZ Studio to load them into the module via, for example, the Eclpise environment. The ArduinoIDE environment seems simpler to me at the moment, but it causes a lot of problems with touch and TFT_eSPI...

Official "sample" code, no. But I can let you have the code I wrote to test it. I'd just have to run it to be sure I didn't break something the last time I wrote it. Code is written for VSCode/platformIO.

1 Like

If you could, it would help me a lot. I would like to base it on something that works.

Sure. Here you are

cedarlakeinstruments/ESP32_LVGL_HMI: Basic ESP32-based HMI using LVGL