ESP32 TFT how to recognise touch

I have this TFT Screen connected to my ESP32.
The wiring is:

TFT_D0 12
TFT_D1 13
TFT_D2 26
TFT_D3 25
TFT_D4 17
TFT_D5 16
TFT_D6 27
TFT_D7 14

I use the TFT_espi library for displaying text and images and it works fine.
Some time ago i used the touch with an arduino uno, now i want to do so on the ESP32.
I installed the Adafruit TouchScreen library and wanted to try the example sketch, but i don't know which pins i do have to set.
Can anyone please help me?

You should read about using the ESP32's GPIO pins and what is the difference between portA and portB and why the pins of portB should not be used for your thing do. A ink to the ESP32's API.

I'm really new to the ESP32. I can't find anything about "port a and port b", can you tell me what they are?

Edit: The display has been connected for a while now, only the touch software is missing.

The MCUFRIEND Calibartion sketch says: XP=27,XM=15,YP=4,YM=14;
But with

// Touch screen library with X Y and Z (pressure) readings as well
// as oversampling to avoid 'bouncing'
// This demo code returns raw readings, public domain

#include <stdint.h>
#include "TouchScreen.h"

// These are the pins for the shield!
#define YP 4  // must be an analog pin, use "An" notation!
#define XM 15  // must be an analog pin, use "An" notation!
#define YM 14   // can be a digital pin
#define XP 27   // can be a digital pin

#define MINPRESSURE 10
#define MAXPRESSURE 1000

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

void setup(void) {

void loop(void) {
  // a point object holds x y and z coordinates
  TSPoint p = ts.getPoint();
  // we have some minimum pressure we consider 'valid'
  // pressure of 0 means no pressing!

  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
     Serial.print("X = "); Serial.print(p.x);
     Serial.print("\tY = "); Serial.print(p.y);
     Serial.print("\tPressure = "); Serial.println(p.z);


I get nothing on the serial monitor.
What am I doing wrong?

You might want to use the ESP32 API documentation to figure out which GPIO's are the analog to digital pins.

You might want to print out the values before the if statement to see why the if statement is not evaluating to true.

The first step is post a link to the actual Shield that you have bought.
Or at least say what colour the pcb is.

You identify the Touch pins by running the calibration sketch on a Uno.
Or by measuring the resistance with a DMM.

I suspect that you have a Blue 3.5 inch Shield. These tend to have Touch pins on

MCUFRIEND_kbv ID=0x7796  320 x 480      AliExpress shield
const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x7796
const int TS_LEFT=895,TS_RT=127,TS_TOP=74,TS_BOT=930;

It looks as if Adafruit Touchscreen.h does not work very well on ESP32.

MCUFRIEND_kbv ID=0x7796  320 x 480      ESP32 USE_LOCAL_KBV
const int XP=12,XM=15,YP=33,YM=13; //320x480 ID=0x7796
const int TS_LEFT=927,TS_RT=138,TS_TOP=16365,TS_BOT=925;

Note that you can use any of those pins as Analog. e.g.

MCUFRIEND_kbv ID=0x7796  320 x 480      ESP32 USE_LOCAL_KBV Analog on D8,D9
const int XP=15,XM=12,YP=13,YM=33; //320x480 ID=0x7796
const int TS_LEFT=107,TS_RT=892,TS_TOP=935,TS_BOT=5;

My Calibration values are for a Blue ST7796S Shield. I suspect that your ILI9486 Shield has similar wiring. However most Red shields use D6, D7, A1, A2 and some Blue Shields also use that wiring.

I strongly recommend that you gain experience with the TFT_eSPI examples and library first. Before attempting Touch.

You also need to experiment with the Touch threshold values.

I am busy with other things. However if a reader posts links to their hardware and quotes a library example by name is likely to get readers to replicate your project.

I do not want to waste my time by guessing.


I have bought this one.

The Calibartion sketch says: XP=27,XM=15,YP=4,YM=14

Tomorrow I will try some more.

As far as I know by now, my screen does not have a touch controller, but 4 analog pins.
But I wonder which pins they are, because there are display problems no matter which of the cables I unplug...
I have connections at:


Can someone please explain to me how to handle the touch on my Shield?
I would be really happy.

Is it possible that some pins are used for both Touch and Image and i have to sent to the screen which one i want to use?

In the meantime I managed that touch with the Shield example of the TouchScreen library outputs the coordinates on the Serielen monitor, but not really reliable (some areas work better than others) and now and then the coordinates do not really make much sense. My guess is that the 3V of the esp are the problem here. Does anyone have experience with this?

I managed it now to get touch working. All i had to do, was to set the ADC resolution to 10.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.