[Solved] Need help for Touchscreen TFT display on ESP32

Hi
I'm trying to get the touch of this display work with an ESP32:


The ESP is this one:

Without the touch, I had the display do its job, using hardware SPI with

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"

#define TFT_cs   17  // goes to TFT CS
#define TFT_dc   16  // goes to TFT DC
#define TFT_mosi 23  // goes to TFT MOSI
#define TFT_sclk 18  // goes to TFT SCK/CLK
#define TFT_rst  5   // goes to TFT RESET
#define TFT_miso     // Not connected
//       3.3V     // Goes to TFT LED
//       5v       // Goes to TFT Vcc
//       Gnd      // Goes to TFT Gnd

// Use hardware SPI
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_cs, TFT_dc, TFT_rst);

But when I add the touch, the latter does not work:

// Touch
#include <XPT2046_Touchscreen.h> // Touch
#define TOUCH_CS    34
//#define TOUCH_TIRQ 2
XPT2046_Touchscreen ts(TOUCH_CS);
//XPT2046_Touchscreen ts(TOUCH_CS, TOUCH_TIRQ);  // Param 2 - Touch IRQ Pin - interrupt enabled polling

Here is the code of the loop:

void loop()
{
  affiche_point ();
  digitalWrite(TFT_cs, HIGH);
  boolean istouched = ts.touched();
  if (istouched) {
    TS_Point p = ts.getPoint();
//    if (!wastouched) {
      int x_touch = p.x;
      int y_touch = p.y;
      Serial.print(x_touch); Serial.print("\t"); Serial.println(y_touch);
      if (x_touch < 41)
      {
        num_courbe = y_touch / 40 + 1;
        angle = 0;
        echelle = 0.1;
        init_affichage();
      }
//    }
  }
  digitalWrite(TOUCH_CS, HIGH);
  wastouched = istouched;
}

When I don't touch the screen or wherever I touch it, I always get x=0 and y=0

Did anyone manage to have such a touchscreen display work with an ESP32 and can help me?
Thanks

Try Bodmer's TFT_eSPI library.
And I would definitely put all SPI devices onto a single SPI bus. (with transactions)

David.

1 Like

david_prentice:
Try Bodmer's TFT_eSPI library.
And I would definitely put all SPI devices onto a single SPI bus. (with transactions)

David.

Thanks, I'll try this library.
BTW, what do you mean? I think it's what I'm doing : both the display and the touch device share MOSI and CLK pins. Is this what you mean?

Also, I have had a look at the library but I don't see how to connect the display. The instructions (in the examples) to declare the display are

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

#define TFT_GREY 0x5AEB

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

but I can't see any pin definition (CS, RST, CLK, MOSI, MISO)... ?

Pins in the range 34-39 are input only and thus cannot be used as an output for the touch chip select. Try another pin.

The Adafruit library is quite slow but may be sufficient for your needs. If you migrate to the TFT_ESPI you will get a significant (up to x10) performance boost.

The ReadMe file provides quidance:

"Configuration of the library font selections, pins used to interface with the TFT and other features is made by editting the User_Setup.h file in the library folder, or by selecting a configuration in the library "User_Setup_Selet,h" file. Fonts and features can easily be disabled by commenting out lines."

The advantage of this approach is that, once you have edited the setup file, any example will run without modification. There are numerous comments in these setup files that should help.

Thanks Bodmer, I missed that point ! And bravo for the great work. I'll come back when I have tried.

It works ! Thanks again. I just need to understand how to a user setup file. Where do I put what to tell the compiler to use my setup file?

Great to hear it is working.

The "User_Setup_Select.h" file in the library folder is always called up during compilation. Inside that header file is a list of file paths. By default it uses the "User_Setup.h" header but you can edit the paths to callup you own setup or one of the pre-configured ones. Read the comments in these files.

OK, I understand, thanks a lot!