TFT_eSPI has built-in support for the XPT2046 resistive touch screen controller that is integrated on most SPI-connected TFT modules. The touch controller shares the SPI bus with the display and uses a dedicated chip-select pin to arbitrate access. Because the support is compiled directly into the library, you get a complete touch API — raw ADC readings, pressure measurement, calibrated screen coordinates, and a 4-point calibration routine — without pulling in a third-party dependency.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Marcussacapuces91/doc-TFT_eSPI/llms.txt
Use this file to discover all available pages before exploring further.
Enabling Touch Support
Touch functionality is activated at compile time by definingTOUCH_CS in your User_Setup.h file (or platformio.ini). Set it to the GPIO pin connected to the touch controller’s chip-select line:
User_Setup.h if needed:
Touch API Reference
All touch methods are member functions ofTFT_eSPI and are available on your tft object whenever TOUCH_CS is defined.
Read raw ADC values directly from the XPT2046. Populates
x and y with the raw ADC counts from the touch controller without applying any calibration mapping.Read the raw pressure (Z) measurement from the touch controller. Returns a value roughly proportional to touch pressure. Returns
0 when not touched.Read calibrated screen coordinates. Validates the touch reading and maps the raw ADC values through the stored calibration matrix to return pixel coordinates. Returns
true (1) when a valid touch is detected above threshold. If the touch coordinates fall off-screen, x and y are not updated.Run an interactive 4-point calibration routine. Draws crosshairs on screen at the four corners and prompts the user to touch each one. Stores the resulting calibration matrix in the array pointed to by
data (must be uint16_t[5]). color_fg and color_bg set the foreground and background colours for the calibration UI. size controls the size of the calibration markers.Restore previously saved calibration data. Pass the same
uint16_t[5] array that was populated by calibrateTouch().Basic Touch Reading Example
Calibration and Saving to Non-Volatile Storage
Raw ADC readings do not map linearly to screen pixels and vary between hardware revisions. Running a calibration once and storing the result means the mapping is accurate on every subsequent boot.Run the calibration routine
Call
calibrateTouch() to display the interactive calibration UI. The user touches four corners of the screen; the function writes five uint16_t values into your array.Persist the calibration data
Save the five values to NVS / EEPROM so calibration survives a power cycle. On ESP32 use the Preferences library:
Reading Raw Pressure
getTouchRawZ() returns the raw Z (pressure) value from the XPT2046. It is useful for detecting touch pressure independently of position, or for implementing your own debounce logic before calling getTouchRaw():
getTouch() is 600. Lower values make detection more sensitive; higher values require a firmer press. Adjust the threshold argument to suit your panel’s characteristics.
Alternative Libraries for Parallel Interfaces
When driving a display over an 8-bit or 16-bit parallel bus — or via the RP2040 PIO interface — the XPT2046 touch support in TFT_eSPI is disabled at compile time. In those cases, wire the touch controller to a separate SPI bus and use an independent library such as XPT2046_Touchscreen by Paul Stoffregen, which communicates over its ownSPIClass instance and does not conflict with the display bus.