Hello, I am designing a ESP32-S3 board where I will have a cheap ILI9341 based TFT LCD with an XPT2046 touch screen driver. I have had no issues with the graphics, however I have had an issue with the XPT2046 library from PaulStoffregen. His library uses the "default" SPI pins of any board (MISO, MOSI, CLK) which has been a huge pain in my neck to try and find for my chip (they are all slightly different). the ESP32 allows you to use ANY pins as an SPI peripheral, or so I am told.
I have had no issue with the graphics because my library I use lets me define the SPI pins ahead of time. the xpt2046 library doesn't.
my question is: is there any way to use custom SPI pins? it would help me out so much. Maybe there's a different library I haven't seen, maybe there is some trick in the IDE I'm not familiar with, I truly have no clue. And if there is nothing I can do, just let me know! i can find some other way to do it.
and If i am completely wrong about this, don't hesitate to tell me. thank you so much!
Unfortunately, it lacks support for custom SPI in the constructor. So instead, you can bypass this by manually creating an SPIClass instance and modifying the library slightly.
2. Manually pass custom SPI using begin() and SPIClass
You can use ESP32’s flexibility with SPIClass to define custom SPI pins.
#include <XPT2046_Touchscreen.h>
#include <SPI.h>
#define T_CS 4 // Your custom Touch CS pin
#define T_CLK 18 // Custom SCK
#define T_MISO 19 // Custom MISO
#define T_MOSI 23 // Custom MOSI — unused by XPT2046, but required by SPIClass
SPIClass mySPI = SPIClass(1); // Use SPI1 or SPI2, not default VSPI/HSPI
XPT2046_Touchscreen touch(T_CS); // Default constructor
void setup() {
mySPI.begin(T_CLK, T_MISO, T_MOSI, T_CS); // Custom pin init
touch.begin(mySPI); // Use custom SPI bus
}
This works with minor tweaks and allows you to fully control SPI routing. Notes:
ILI9341 and XPT2046 must NOT share the same CS pin, but they can share the bus (MOSI/MISO/CLK).
XPT2046 typically needs <10MHz SPI frequency — set this before touching .begin() or .transfer():
If you want full control and easy integration, consider using the TFT_eSPI library, which now supports touch drivers (including XPT2046) via User_Setup.h. You define both SPI buses and pins in one place.