Touch control with ILI9486 display in conjunction with DOIT ESP32 DEVKIT V1

Hello. Can someone advise me how to activate the touch on the ILI9486? I am using ESP32 DEVKIT V1 and Adafruit_GFX, MCUFRIEND_kbv and TouchScreen libraries.
I am using Arduino IDE 2.2.1 and Visual Studio Code with Platformio IDE

Sorry for the English, but I'm translating it from Czech using Google.

/* 
Test MCU Friend parallel display and resistive touchscreen by drawing touch points
on screen, use something pointed for more accuracy

Need this modified Touchscreen library and one of:
- TFT_eSPI        much faster for ESP32, must select correct display driver 
- MCUFRIEND_kbv   more display driver support, auto detects display driver
 */

//#define TFT_eSPIlib  // comment out to use MCUFRIEND_kbv

#ifdef TFT_eSPIlib
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
#else
#include <MCUFRIEND_kbv.h> 
MCUFRIEND_kbv tft;
#endif

#include <TouchScreen.h>

// adjust pressure sensitivity - note works 'backwards'
#define MINPRESSURE 200
#define MAXPRESSURE 1000

// some colours to play with
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
 
// Either run TouchScreen_Calibr_native.ino and apply results to the arrays below
// or just use trial and error from drawing on screen
// ESP32 coordinates at default 12 bit resolution have range 0 - 4095
// however the ADC cannot read voltages below 150mv and tops out around 3.15V
// so the actual coordinates will not be at the extremes
// each library and driver may have different coordination and rotation sequence
const int coords[] = {3800, 500, 300, 3800}; // portrait - left, right, top, bottom

const int rotation = 0; //  in rotation order - portrait, landscape, etc

const int XP = 27, XM = 15, YP = 4, YM = 14; // default ESP32 Uno touchscreen pins
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

void setup() {
    Serial.begin(115200);
#ifdef TFT_eSPIlib
    Serial.println("TFT_eSPI library");
    tft.begin();
#else
    Serial.println("MCUFRIEND_kbv library");
    idDisplay();
#endif
    // screen orientation and background
    String orientation;
    switch (rotation) {
      case 0: 
        orientation = "Portrait";
      break;
      case 1: 
        orientation = "Landscape";
      break;
      case 2: 
        orientation = "Portrait Inverted";
      break;
      case 3: 
        orientation = "Landscape Inverted";
      break;
    }
    Serial.println(orientation);
    tft.setRotation(rotation);  
    tft.fillScreen(BLACK);
}

void loop() {
    // display touched point with colored dot
    uint16_t pixel_x, pixel_y;    
    boolean pressed = Touch_getXY(&pixel_x, &pixel_y, true);
}

boolean Touch_getXY(uint16_t *x, uint16_t *y, boolean showTouch) {
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
      switch (rotation) {
        case 0: // portrait
          *x = map(p.x, coords[0], coords[1], 0, tft.width()); 
          *y = map(p.y, coords[2], coords[3], 0, tft.height());
        break;
        case 1: // landscape
          *x = map(p.y, coords[1], coords[0], 0, tft.width()); 
          *y = map(p.x, coords[2], coords[3], 0, tft.height());
        break;
        case 2: // portrait inverted
          *x = map(p.x, coords[1], coords[0], 0, tft.width()); 
          *y = map(p.y, coords[3], coords[2], 0, tft.height());
        break;
        case 3: // landscape inverted
          *x = map(p.y, coords[0], coords[1], 0, tft.width()); 
          *y = map(p.x, coords[3], coords[2], 0, tft.height());
        break;
      }      
      if (showTouch) tft.fillCircle(*x, *y, 2, YELLOW);
    }
    return pressed;
}

#ifndef TFT_eSPIlib
void idDisplay() {
    // MCUFRIEND_kbv library only
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
}
#endif



Here is the connection of the terminals that work for me with the display.

/* Funkční propojení se setup16 ILI9488 parallel
// ESP32 pins used
#define TFT_CS   33  // Chip select control pin
#define TFT_DC   15  // Data Command control pin - must use a pin in the range 0-31
#define TFT_RST  32  // Reset pin

#define TFT_WR    4  // Write strobe control pin - must use a pin in the range 0-31
#define TFT_RD    2

#define TFT_D0   12  // Must use pins in the range 0-31 for the data bus
#define TFT_D1   13  // so a single register write sets/clears all bits
#define TFT_D2   26
#define TFT_D3   25
#define TFT_D4   17
#define TFT_D5   16
#define TFT_D6   27
#define TFT_D7   14
*/

Hey, did you figure out what the problem was??....im struggling. :frowning:

I solved the problem online with Bing's ChatAI and we solved it. I removed the TFT_eSPI library and replaced it with the TouchScreen_kbv library. Here is the touch pin wiring:
// ILI9488
#define YP 15
#define XM 33
#define YM 12
#define XP 13

Using the MCUfriend_kbv library, I found out what display I have. In Setup() I entered the code
"uint16_t ID = tft.readID()" and then had it written to the serial monitor. According to the seller, I should have ILI9486, but the library reported ILI9488, so I adjusted everything to 9488. Then just writing the basic code for the touch:
"boolean Touch_getXY(int16_t *pixel_x, int16_t *pixel_y)
{
TSPoint_kbv p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because of TFT control pins
digitalWrite(XM, HIGH);
bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);

 if (p.z > MINPRESSURE && p.z < MAXPRESSURE); {
   // * pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width()); // ESP32 ILI9486
   // * pixel_y = map(p.x, TS_BOT, TS_TOP, 0, tft.height()); // ESP32 ILI9486
   * pixel_x = map(p.y, TS_LEFT, TS_RT, tft.width(),0); // ESP32 ILI9488
   * pixel_y = map(p.x, TS_BOT, TS_TOP, tft.height(),0); // ESP32 ILI9488

tft.fillCircle(*pixel_x, *pixel_y, 2, TFT_GREENYELLOW)} // render touch point.
Everything I wrote here was advised by ChatAI. Just ask the right questions. I used touch in a clock project.

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