This guide walks you through everything needed to detect a touch and print live X, Y, and pressure coordinates to the Serial Monitor. From a fresh install to your first working output takes fewer than five minutes.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.
Prerequisites
Before starting, make sure you have the following:- The XPT2046_Touchscreen library installed (see Installation)
- An Arduino-compatible board with a hardware SPI port (Arduino Uno, Mega, Leonardo, Teensy, ESP8266, ESP32, or similar)
- An XPT2046-based TFT touch module — these are the inexpensive 2.4″/2.8″/3.2″ color displays that expose
T_CS,T_CLK,T_DIN,T_DO, and (optionally)T_IRQpins - A USB cable and the Arduino IDE or PlatformIO
Step-by-Step Setup
Define the chip-select pin
The only pin you must configure yourself is the chip-select (CS) line that connects to the display’s Optionally, define the touch-interrupt pin if your display’s
T_CS pin. The SPI clock, MOSI, and MISO lines are managed by the SPI library using the hardware SPI pins for your board (pins 13, 11, 12 on Arduino Uno).T_IRQ line is wired up. Using the interrupt pin makes touched() and getPoint() skip the SPI bus entirely when no touch is present, reducing overhead and preventing spurious SPI activity.Create the touchscreen instance
Declare an
XPT2046_Touchscreen object at global scope (outside any function). Pass the CS pin as the first argument. The second argument is the optional interrupt pin — omit it or pass 255 to disable interrupt-driven polling.The constructor is
constexpr, so the object is fully initialized at compile time. No heap allocation takes place.Initialize in setup()
Call To use a non-default SPI port, pass it to
ts.begin() inside setup() to initialize the SPI bus and configure the CS pin as an output. Follow it with ts.setRotation(n) to align the touch coordinate frame with your display orientation, where n is 0–3 matching the rotation value you pass to your TFT library.begin():Read touch data in loop()
Call
ts.touched() to check whether the screen is currently being pressed. When it returns true, call ts.getPoint() to retrieve the current position as a TS_Point struct containing x, y, and z fields.p.xandp.yare 12-bit values in the range 0–4095.p.zis a 12-bit pressure value (int16_t) in the range 0–4095.touched()returnstrueonly whenp.zis at or above the internal threshold of 300, which reliably filters light contact noise.
Complete Sketch
The following sketch combines all four steps into a single ready-to-upload file. It mirrors theTouchTest example that ships with the library. Wire your display’s T_CS to pin 8, T_IRQ to pin 2, and the SPI lines (T_CLK→13, T_DIN→11, T_DO→12) using the Arduino Uno hardware SPI pins, then open the Serial Monitor at 38400 baud.
Expected Serial Output
When you touch the display and open the Serial Monitor at 38400 baud, you should see lines similar to:z) rises with firmer contact and drops to zero when you lift your finger.