The TouchTest example is the quickest way to verify that your XPT2046 touch controller is wired correctly and responding. Every time you press the screen, it reads the raw X, Y, and Z (pressure) values and prints them to the Arduino Serial Monitor. By the end of this walkthrough you will understand the three constructor options, howDocumentation 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.
ts.begin() initialises the SPI bus, and what ts.touched() and ts.getPoint() actually return.
Hardware Required
Microcontroller
Any Arduino-compatible board that exposes a hardware SPI bus (Uno, Mega, Teensy, ESP32, etc.)
Display Module
Any XPT2046-based resistive TFT module (2.4″ or 2.8″ ILI9341 combo boards are most common)
Default Pin Assignments
| Signal | Arduino Pin |
|---|---|
| CS (T_CS) | 8 |
| MOSI | 11 |
| MISO | 12 |
| SCK | 13 |
| TIRQ (optional) | 2 |
Full Source Code
Code Walkthrough
Constructor Options
Three constructor forms are shown — two are commented out so you can swap between them:The active line uses
TIRQ_PIN (pin 2). When a valid pin is passed, begin() will call attachInterrupt() on that pin and the library sets the internal isrWake flag on every falling edge. When no interrupt pin is used (255), every call to touched() performs a full SPI read regardless of screen state.ts.begin()
ts.begin() performs three jobs: it stores a reference to the SPI bus (SPI by default, or an alternate port if you pass one like SPI1), asserts the CS pin HIGH to deselect the chip, and — if a valid tirqPin was provided to the constructor — attaches a falling-edge ISR that sets isrWake = true.setRotation(1) — Landscape Mode
setRotation(n) rotates the coordinate frame to match how the display is physically oriented. The value is taken modulo 4:| n | Orientation |
|---|---|
| 0 | Portrait |
| 1 | Landscape |
| 2 | Portrait (flipped) |
| 3 | Landscape (flipped) |
ts.touched() — Detecting a Press
ts.touched() initiates an SPI transaction to sample the Z (pressure) channel. It returns true when the measured pressure is ≥ 300 raw ADC counts. Values below that threshold are treated as noise or a released screen.ts.getPoint() — Reading Coordinates
ts.getPoint() returns a TS_Point struct containing three int16_t members:| Member | Range | Meaning |
|---|---|---|
p.x | 0 – 4095 | Horizontal position |
p.y | 0 – 4095 | Vertical position |
p.z | 0 – 4095 | Touch pressure (higher = harder press) |
Expected Serial Output
Open the Serial Monitor (or any terminal) at 38400 baud. Each touch event produces one line:The
while (!Serial && (millis() <= 1000)); line at the end of setup() gives USB-serial boards (like the Teensy or Leonardo) up to one second to enumerate on the host before the sketch continues. On classic Uno/Mega boards with a separate USB-to-serial chip this line has no effect and can be left as-is.