Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/PaulStoffregen/XPT2046_Touchscreen/llms.txt

Use this file to discover all available pages before exploring further.

The XPT2046 communicates over SPI and exposes five pins on most TFT breakout modules: T_CLK (clock), T_DIN (MOSI), T_DO (MISO), T_CS (chip select), and T_IRQ (touch interrupt). The first four are required; T_IRQ is optional but enables interrupt-driven touch detection. The SPI data pins are shared with the board’s hardware SPI bus, while T_CS and T_IRQ can be connected to any available digital GPIO.

SPI Pin Mapping by Board

The library uses the hardware SPI peripheral automatically. Connect T_CLK, T_DIN, and T_DO to the corresponding SPI pins for your board:
BoardMOSIMISOSCK
Arduino Uno / Nano111213
Arduino Mega515052
Teensy 4.x (SPI0)111213
ESP32 (VSPI)231918
ESP8266D7D6D5

TFT Module Wiring Table

Most low-cost TFT modules label their touch pins with a T_ prefix. Wire them as follows:
TFT Module PinFunctionConnect To
T_CSChip SelectAny GPIO (e.g. D8)
T_CLKSPI ClockSCK
T_DINSPI MOSIMOSI
T_DOSPI MISOMISO
T_IRQTouch InterruptAny interrupt-capable pin (optional)

Defining Pins in Your Sketch

The CS pin number is passed directly to the XPT2046_Touchscreen constructor. The TIRQ_PIN argument is optional — pass it only if you have T_IRQ wired up and want interrupt-driven touch detection.
#include <XPT2046_Touchscreen.h>
#include <SPI.h>

#define CS_PIN    8
#define TIRQ_PIN  2

// With interrupt pin (recommended):
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);

// Without interrupt pin:
// XPT2046_Touchscreen ts(CS_PIN);
Call ts.begin() inside setup() to initialise the SPI bus and configure the CS pin as an output:
void setup() {
  ts.begin();
  ts.setRotation(1);
}

Sharing SPI with a TFT Display

When a TFT display driver (such as ILI9341) and the XPT2046 touchscreen share the same SPI bus, each device must have its own dedicated CS pin. The library uses Arduino’s SPI.beginTransaction() / SPI.endTransaction() API, so bus access is correctly arbitrated between devices.
A typical two-device setup wires the display CS to one pin and the touch CS to another:
#define TFT_CS   10   // display chip select
#define TOUCH_CS  8   // touchscreen chip select

Using an Alternate SPI Port

On boards with multiple SPI peripherals (e.g. Teensy 4.x), you can pass a different SPIClass instance to begin(). This is useful when the primary SPI bus is already heavily used by other peripherals.
ts.begin(SPI1); // use SPI1 instead of the default SPI0

Build docs developers (and LLMs) love