Source: https://mokxi.com/libraries/xpt2046
Updated: 2026-11-02

Libraries / Input
Arduino library · Input

# XPT2046_Touchscreen: reading a resistive touch panel

The resistive touch layer on most cheap SPI TFTs, including the 2.4 and 2.8 inch ILI9341 boards and the ESP32 Cheap Yellow Display, is read by an XPT2046 controller. Paul Stoffregen’s XPT2046_Touchscreen library asks it where the panel is being pressed and how hard.

Open the example in the editor See the calls

Built in on Mokxi: #include <XPT2046_Touchscreen.h> and it builds, with nothing to install. Free, and no account needed.

## What XPT2046_Touchscreen does

The controller shares the SPI bus with the screen but has its own chip select, marked T_CS on the module, plus an optional T_IRQ pin that goes low while the panel is touched. Only one device may have its chip select low at a time, so the screen’s CS and the touch’s T_CS must be different pins.

Readings are raw 12-bit numbers, roughly 200 to 3900 across each axis, not pixels. You turn them into screen coordinates with map(), using the raw values you read at the corners of your own panel, and you match the touch rotation to the screen’s so that x and y point the same way.

Library Manager name | XPT2046_Touchscreen

Author | Paul Stoffregen

Include | #include <XPT2046_Touchscreen.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Simulated: it runs against the part the way it does on the bench.

Parts it drives | Touch TFT, 2.8 inch ILI9341 with XPT2046, Touch TFT, 3.5 inch ILI9488 with XPT2046, Touch TFT, 4 inch ST7796S with XPT2046

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <XPT2046_Touchscreen.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "XPT2046_Touchscreen" by Paul Stoffregen from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add XPT2046_Touchscreen to lib_deps in PlatformIO. Mokxi follows the calls of release 1.4, so a sketch written here builds there too.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### XPT2046_Touchscreen ts(TOUCH_CS, TOUCH_IRQ)

The touch controller on the given chip select. The IRQ pin is optional; with it, touched() can answer without an SPI transfer.

### ts.begin()

Sets up the chip select and starts SPI. Call it after the screen’s own begin().

### ts.setRotation(1)

Rotates the touch coordinates. Use the same value you give the screen’s setRotation().

### ts.touched()

True while the panel is pressed hard enough to count.

### TS_Point p = ts.getPoint()

The latest reading: p.x and p.y raw from about 200 to 3900, and p.z, the pressure.

### ts.tirqTouched()

Checks the IRQ pin only, a cheap test to call often before deciding to read a full point.

## Example: CYD touch paint

On the Cheap Yellow Display (ESP32-2432S028R). It runs in the browser exactly as printed.

sketch.ino · Cheap Yellow Display (ESP32-2432S028R)

Open it in the editor, wired and ready to run · CYD touch paint: the project page

## Common errors and how to fix them

### x and y are swapped, or a touch on the left registers on the right

The touch rotation does not match the screen. Give ts.setRotation() the same number as tft.setRotation(), then swap or reverse the map() ranges if one axis still runs backward.

### touched() is true all the time, or points jump around

The screen and the touch controller are both selected at once. Make sure CS and T_CS are separate pins and that every other SPI device has its chip select held high.

### Touches land a few pixels away from the finger

The map() ranges are someone else’s. Print the raw p.x and p.y while you press each corner of your own panel, and use those numbers as the input range.

## Projects that use XPT2046_Touchscreen

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Touch paint Draw on a 2.8 inch TFT with its resistive touch panel: two SPI chips on one module, and the raw touch counts mapped to pixels.

## Keep going

### Learn

- Arduino ILI9341 TFT Display: Wiring and Code
- Cheap Yellow Display Pinout: CYD Screen, Touch, LED

### Parts

- Touch TFT, 2.8 inch ILI9341 with XPT2046
- Touch TFT, 3.5 inch ILI9488 with XPT2046
- Touch TFT, 4 inch ST7796S with XPT2046

### More input libraries

- Keypad
- OneButton
- Encoder
- RotaryEncoder
- CapacitiveSensor
- IRremote
- Adafruit MCP23017 Arduino Library
- PCF8574 (Rob Tillaart) and PCF8574 library (Renzo Mischianti)

Every built-in library · Open the editor

## Try XPT2046_Touchscreen without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
