My touchscreen does not work with ESP32 DEVKIT V2 and ili9341

Hi,
I am using the ILI9341 LCD with the sketch taken from the XPT2046_touchscreen examples but it does not feel the finger touch on the video. Only x and y coordinates equal to 0 appear on the serial monitor.
The LCD is this, taken on eBay = 1PCS NEW 2.8 inch SPI LCD module 240*320 TFT with touch ILI9341 #YT | eBay. The graphics work excellently. T
he sketch has been adapted to the DOIT ESP32 DEVKIT V2, updating the pins. The modified sketch is as follows:

#include <XPT2046_Touchscreen.h>
  #include <SPI.h>
  
  #define CS_PIN  21
  // MOSI=17, MISO=14, SCK=16
  
  //XPT2046_Touchscreen ts(CS_PIN);
  #define TIRQ_PIN  25
  //XPT2046_Touchscreen ts(CS_PIN);  // Param 2 - NULL - No interrupts
  //XPT2046_Touchscreen ts(CS_PIN, 255);  // Param 2 - 255 - No interrupts
  XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

  void setup() {
    Serial.begin(38400);
    ts.begin();
    // ts.begin(SPI1); // use alternate SPI port
    ts.setRotation(1);
    while (!Serial && (millis() <= 1000));
  }
  
  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(500);
      Serial.println();
    }
  }
1 Like

That issue has shown up a lot. You need to check your soldering VERY carefully.

Okay.
I'll recheck the connections and soldering

If you used braided wire, use a lighted magnifying glass since it only takes one strand connecting to the next pin to cause a problem.

Hi,
I checked the connections as advised and they are fine.

The next logical step is to try one of the most simple sample sketches. If it works, you have an error in your sketch, if it doesn't work you have a wiring error.

If the screen look like this on the rear, then you have to connect the touch screen pins as well.
You've mentioned CS_PIN and this is exclusively for the touch screen.
Other pins, are shared SPI pins and are shared between the TFT driver and the XPT2046 touch controller, however, these are not internally connected and you must connect them explicitly.

The T_IRQ pin can probably be ignored with the correct choice of constructor for XPT2046_Touchscreen.

Anyway, show a wiring diagram of how you have connected the screen pins to the microcontroller.

Try lifting the MISO line to the display. It's all too common for these displays not to tristate that output when the display is not selected, which prevents the touchscreen (and any other SPI devices as well) from being read.

If that turns out to be the case, and you don't need to read data from the display, you're done. If you do need to read from the display, you'll have to add something like a 74AHC125 tristate buffer controlled by the CS line.

The electrical connection is as follows:
ESP32_pin_ILI.pdf (16,1 KB)
The image is identical to that of “6v6gt”.

Most people won't click on random PDFs these days. If you have a diagram you wish to share, please upload it as an image.

Ah: I see you've edited your reply. Never mind.

I wrote this PDF to simplify the connections, in fact I also added color to the various wires used.
I will take a picture and attach it.

I think I see a problem already. The SPI bus is shared. That is you must connect the following display pins together:
T_CLK( display pin 10) and SCK (display pin 7)
T_DIN( display pin 12) and SDI(MOSI) (display pin 6)
T_DO( display pin 13) and SDO(MISO) (display pin 9)

The CS ( chip select pins ) are NOT shared. These are used to select the SPI peripheral which the SPI bus is currently talking to. That is T_CS and TFT_CS must use different pins (as you correctly have).

This is your current connection diagram:

It should look more like this as fas as I can see:

using the XPT2046_Touchscreen library with the 2.8_ SPI TFT LCD Display Touch Panel ILI9341 I used the following code
Touch_Test.ino file

// TFT_eSPI touch test 240X320 Resolution 2.8_ SPI TFT LCD Display Touch Panel ILI9341

/*  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 34                 // T_IRQ
#define XPT2046_MOSI MOSI   // GPIO23  // T_DIN
#define XPT2046_MISO MISO   // GPIO19  // T_OUT
#define XPT2046_CLK SCK     // GPIO18   // T_CLK
#define XPT2046_CS 5                    // 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);
  }
}

User_Setup.h file (place in same directory as above .ino file)

// TFT_eSPI User_Setup.h file 240X320 Resolution 2.8_ SPI TFT LCD Display Touch Panel ILI9341

#define USER_SETUP_INFO "User_Setup"

#define ILI9341_DRIVER

// For ST7735, ST7789 and ILI9341 ONLY, define the colour order IF the blue and red are swapped on your display
// Try ONE option at a time to find the correct colour order for your display
//  #define TFT_RGB_ORDER TFT_RGB  // Colour order Red-Green-Blue
//  #define TFT_RGB_ORDER TFT_BGR  // Colour order Blue-Green-Red

// For ST7789, ST7735, ILI9163 and GC9A01 ONLY, define the pixel width and height in portrait orientation
#define TFT_WIDTH  240 // ST7789 240 x 240 and 240 x 320
#define TFT_HEIGHT 320 // ST7789 240 x 320

// If colours are inverted (white shows as black) then uncomment one of the next
// 2 lines try both options, one of the options should correct the inversion.

// #define TFT_INVERSION_ON
// #define TFT_INVERSION_OFF

#define TFT_CS   15 
#define TFT_MOSI MOSI 
#define TFT_SCLK SCK 
#define TFT_MISO MISO 

#define TFT_DC   2
#define TFT_RST  4

#define LOAD_GLCD
#define LOAD_FONT2
#define LOAD_FONT4
#define LOAD_FONT6
#define LOAD_FONT7
#define LOAD_FONT8
#define LOAD_GFXFF

#define SMOOTH_FONT

#define SPI_FREQUENCY   10000000

#define SPI_TOUCH_FREQUENCY  2500000

serial monitor output

X = 301 | Y = 43 | Pressure = 403
X = 242 | Y = 25 | Pressure = 674
X = 67 | Y = 39 | Pressure = 977
X = 212 | Y = 59 | Pressure = 683
X = 76 | Y = 36 | Pressure = 1782
X = 64 | Y = 56 | Pressure = 1814
X = 44 | Y = 83 | Pressure = 1846
X = 34 | Y = 114 | Pressure = 2026
X = 45 | Y = 151 | Pressure = 2083
X = 48 | Y = 178 | Pressure = 2119
X = 74 | Y = 207 | Pressure = 2088
X = 136 | Y = 206 | Pressure = 1556
X = 191 | Y = 205 | Pressure = 1935
X = 227 | Y = 211 | Pressure = 1782
X = 279 | Y = 225 | Pressure = 1673
X = 301 | Y = 218 | Pressure = 1632
X = 302 | Y = 205 | Pressure = 1540
X = 303 | Y = 188 | Pressure = 1511
X = 307 | Y = 169 | Pressure = 1435
X = 272 | Y = 148 | Pressure = 1531
X = 203 | Y = 131 | Pressure = 1583
X = 165 | Y = 131 | Pressure = 1872
X = 136 | Y = 114 | Pressure = 2017
X = 111 | Y = 104 | Pressure = 2013
X = 106 | Y = 85 | Pressure = 2002
X = 154 | Y = 114 | Pressure = 1977
X = 199 | Y = 136 | Pressure = 1853
X = 231 | Y = 173 | Pressure = 1727
X = 268 | Y = 155 | Pressure = 1655
X = 290 | Y = 153 | Pressure = 1483

as the pen is moved around the panel the display values are updated

Thanks for the advice.
Next week I will make the changes to the connections and then try the sketch you recommended.
I will let you know the results.

HI,
Do these connections go to the GPIOs described alongside?

#define XPT2046_MOSI MOSI   // GPIO23  // T_DIN
#define XPT2046_MISO MISO   // GPIO19  // T_OUT
#define XPT2046_CLK SCK     // GPIO18   // T_CLK

Thank you

both the touch SPI and TFT SPI are connected to same pins

#define TFT_MOSI MOSI 
#define TFT_SCLK SCK 
#define TFT_MISO MISO 

the chip selects must be different pins, e.g.

#define XPT2046_CS 5                    // T_CS
#define TFT_CS   15 

with Tools>Board select ESP32 Dev Module and the following are defined

MOSI: 23
MISO: 19
SCK: 18

or you can use the GPIO pins explicitly

#define XPT2046_MOSI 23  // T_DIN
#define XPT2046_MISO 19  // T_OUT
#define XPT2046_CLK 18   // T_CLK

#define TFT_MOSI 23 
#define TFT_SCLK 18 
#define TFT_MISO 19 

I downloaded and created the two sketches as recommended by “horace”.
Installed on the ESP32 it works, I see on the serial monitor the 3 values when I press with the coop,,, but I don't see on the display the writings with the values as seen in the picture attached in post# 13.
I figured out how the pins are connected.
I checked the connections and they are correct.
Did I maybe forget some verification?

To what ESP32 pin is the screen's CS pin connected ?
Your code used TFT_CS = 21. Horace's uses TFT_CS = 15.

I downloaded and used the sketch from “Horace”.
The TFT_Cs pin is 15.
Should the TFT_MISO pin be connected with the XPT2046_MISO ? (19)
Should the TFT_MOSI pin be connected with the XPT2046_MOSI ? (23)
Should the TFT_CLK pin be connected with the XPT2046_CLK ? (18)
I have not connected them together, should I do so ?

Yes.
You've said the touch screen works so the above three SPI bus pins are clearly correct.

That means, for example that the same ESP32 pin (19) must be connected to both XPT2046_MISO (marked TDO on the display module)
and TFT_MISO ( marked SDO(MISO) on the display module).

That is just as implied here: