Display does not work after touch initialization

I am using an ESP32 connected to a ILI9341 display with an XPT2046 controller, using the following libraries:

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ILI9341.h> // Hardware-specific library for ILI9341
#include <U8g2_for_Adafruit_GFX.h>
#include "XPT2046_Touchscreen.h"

Display was working perfectly until I initialize the touch controller. The controller then works, but I cannot output anything to the display.

Here is my code (portions that deal with this):

//  Display SPI  - default VSPI for ESP32 WROOM32
#define TFT_MISO 19  // shared with T_DO
#define TFT_MOSI 23 // shared with T_DIN
#define TFT_SCLK 18 // shared with T_CLK
#define TFT_CS    5  // Chip select control pin
#define TFT_DC    4  // Data Command control pin
#define TFT_RST   2  // Reset pin (could connect to RST pin)   
#define TFT_BL          16    // backlight

// screen initialization 
Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST, TFT_MISO);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;

//  Touch SPI - default HSPI for ESP32 WROOM32,  XPT2046 touch controller
#define TOUCH_CS        15   //HSPI CS
#define TOUCH_IRQ       17

// touch controller init
XPT2046_Touchscreen touch(TOUCH_CS); //, TOUCH_IRQ );
TS_Point rawLocation;

void initDisplay() {

  // connect u8g2 procedures to Adafruit GFX
  u8g2_for_adafruit_gfx.begin(display);                 
  display.begin();

  display.setRotation(3);
  display.fillScreen(0x0000);               // clear the screen, very slow!
  
  // display state - diagnostics
  uint8_t x = display.readcommand8(ILI9341_RDMODE);
  Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
  x = display.readcommand8(ILI9341_RDMADCTL);
  Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
  x = display.readcommand8(ILI9341_RDPIXFMT);
  Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
  x = display.readcommand8(ILI9341_RDIMGFMT);
  Serial.print("Image Format: 0x"); Serial.println(x, HEX);
  x = display.readcommand8(ILI9341_RDSELFDIAG);
  Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX); 
}

// draw static elements of the sensor screen
void drawStaticSensorScreen () { 
  // set up lines n
  u8g2_for_adafruit_gfx.setForegroundColor(COLOR_WHITE);
  display.drawFastHLine(1, LINE1_Y, 320, COLOR_WHITE);  // draw banner line
  display.drawFastHLine(1, LINE1_Y+2, 320, COLOR_WHITE);  // draw banner line

  // set up four measurement labels
  u8g2_for_adafruit_gfx.setFont(MEAS_TEXT_FONT);

  u8g2_for_adafruit_gfx.setCursor(MEAS1_TEXT_X, MEAS1_TEXT_Y);
  u8g2_for_adafruit_gfx.print(MEAS1_TEXT);
  u8g2_for_adafruit_gfx.setCursor(MEAS2_TEXT_X, MEAS2_TEXT_Y);
  u8g2_for_adafruit_gfx.print(MEAS2_TEXT);
  u8g2_for_adafruit_gfx.setCursor(MEAS3_TEXT_X, MEAS3_TEXT_Y);
  u8g2_for_adafruit_gfx.print(MEAS3_TEXT);
  u8g2_for_adafruit_gfx.setCursor(MEAS4_TEXT_X, MEAS4_TEXT_Y);
  u8g2_for_adafruit_gfx.print(MEAS4_TEXT);
  
  // for test purposes, fill in some sample measurements
  // banner measurements
  /* 
  u8g2_for_adafruit_gfx.setFont(MEAS_VAL_FONT);
  u8g2_for_adafruit_gfx.setCursor(BANNER_VAL1_X, BANNER_VAL_Y);
  u8g2_for_adafruit_gfx.print("22");
  u8g2_for_adafruit_gfx.print(BANNER_VAL1_UNIT);
  u8g2_for_adafruit_gfx.setCursor(320 - BANNER_VAL2_X - 100, BANNER_VAL_Y);
  u8g2_for_adafruit_gfx.print("1000");
  u8g2_for_adafruit_gfx.print(BANNER_VAL2_UNIT);*/

  // main measurements
  u8g2_for_adafruit_gfx.setFont(BANNER_FONT);
  u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR24_tr);  //https://github.com/olikraus/u8g2/wiki/fntlistall

  u8g2_for_adafruit_gfx.setCursor(MEAS1_VAL_X, MEAS1_VAL_Y);
  boost = 15;
  u8g2_for_adafruit_gfx.print(banner_val1, 0);
  u8g2_for_adafruit_gfx.print(MEAS1_VAL_UNIT);

  u8g2_for_adafruit_gfx.setCursor(MEAS2_VAL_X, MEAS2_VAL_Y);
  egt_temp_f = 700;
  u8g2_for_adafruit_gfx.print(egt_temp_f, 0);
  u8g2_for_adafruit_gfx.print(MEAS2_VAL_UNIT);

  u8g2_for_adafruit_gfx.setCursor(MEAS3_VAL_X, MEAS3_VAL_Y);
  coolant_temp_f = 180;
  u8g2_for_adafruit_gfx.print(coolant_temp_f, 0);
  u8g2_for_adafruit_gfx.print(MEAS3_VAL_UNIT);

  u8g2_for_adafruit_gfx.setCursor(MEAS4_VAL_X, MEAS4_VAL_Y);
  trans_temp_f = 170;
  u8g2_for_adafruit_gfx.print(trans_temp_f, 0);
  u8g2_for_adafruit_gfx.print(MEAS4_VAL_UNIT);
}

void setup() {
  Serial.begin(115200);
  
  //initialize the display
  initDisplay();
  drawStaticSensorScreen();     //draw static screen for sensor measurements, including banner
 
  touch.begin();
  touch.setRotation(3);
}

In loop() I output to the screen using the same method as in drawStaticSensorScreen, which works perfectly until I initialize the touch controller.

I understood that both the ILI and XPT controllers are ok being connected to the same SPT interface on the ESP32, as long as they have separate CS pins of course, and yet here I am.

Thoughts?

You need to verify this. TFT uses VSPI on ESP32, touch seems to use HSPI. If this is through the same pins, it will not work.

Please report which "XPT2046_Touchscreen.h" you use.
You can look at library.properties to find out.
The one I have installed has:

url=https://github.com/PaulStoffregen/XPT2046_Touchscreen

It uses the standard global SPI instance, which uses VSPI on standard ESP32.

Thanks for the reply! As I understand, perhaps incorrectly, I can slave both TFT and Touch off the VSPI, and this is what I did, the MISO, MOSI, and SCLK pins are shared for both TFT and touch, only the CS pins are different.

The XPT2046_Touchscreen.h I am using is the same one as you are.

name=XPT2046_Touchscreen
version=1.4
author=Paul Stoffregen
maintainer=Paul Stoffregen
sentence=Touchscreens using the XPT2046 controller chip.
paragraph=Many very low cost color TFT displays with touch screens have this chip.
category=Display
url=https://github.com/PaulStoffregen/XPT2046_Touchscreen
architectures=*

Notably, the touch controller does work, I am able to get coordinates from it, but the TFT stops working.

Yes, this should work. I have not found a reason in XPT2046_Touchscreen why it shouldn't. But I don't think I ever tried; I had the intention when I installed that library a year ago, most likely triggered by a post, but I still have no experience with touch.

I just searched for XPT2046 in the whole forum, and get a lot of results. e.g.:
ILI9488 incompatibility with touch screen + TFT_eSPI + XPT2046_Touchscreen library: - Using Arduino / Programming Questions - Arduino Forum

Just put ts.begin() before tft.begin() for everything to work fine. I don't understand why it doesn't work if ts.begin() is placed after tft.begin().

I've switched to using TFT_eSPI library, and it does not have this issue, display and touch work together.

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