XPT2046 at a glance
Item | Value | Source |
|---|---|---|
Touch type | Resistive | ESPHome docs |
Interface | SPI with chip select; optional pen-interrupt pin (often | ESPHome docs, Raspberry Pi overlay |
Reading range | 12-bit, 0–4095 | ESPHome driver, Raspberry Pi overlay defaults |
Read commands | X | ESPHome driver |
Linux driver | | Raspberry Pi overlays, LCD-show |
SPI clock on Raspberry Pi | 2 MHz default, 3.25 MHz maximum | Raspberry Pi overlay README |
We could not download an XPT2046 datasheet from a source we trust, so this page has no supply-voltage or timing figures. The values above come from driver source and board documentation.
How a reading works
ESPHome's driver shows the sequence most libraries follow:
- Read the two pressure channels Z1 and Z2 and compute
Z1 + 4095 − Z2. If the result is below the threshold (400 by default), there is no touch. - Throw away the first X conversion, which the driver comments is "always noisy".
- Take three Y and three X readings and average the two closest values of each, which rejects a single outlier.
- End with a conversion that powers the ADC down and re-enables the pen-interrupt pin.
Each reading is a raw ADC value, so the next step is always calibration.
Calibrating XPT2046 touch
Resistive panels differ from unit to unit, and ESPHome makes calibration mandatory for this chip. The process is the same in any library:
- Log raw values while you touch the left, right, top and bottom edges.
- Use those as
x_min,x_max,y_minandy_max. ESPHome's documentation example uses 280–3860 for X and 340–3860 for Y; yours will differ. - Swap X and Y, or mirror an axis, until the touch follows your finger in the rotation you use.
On Raspberry Pi the same idea appears in an X11 calibration file. LCD-show's file for its 3.5″ display matches the device "ADS7846 Touchscreen" and sets the calibration to 3936 227 268 3880 with SwapAxes 1: the X range runs high to low because that axis is reversed on this panel.
XPT2046 on the ESP32 Cheap Yellow Display
The ESP32-2432S028R (CYD) wires the XPT2046 to its own pins:
Signal | GPIO |
|---|---|
CLK | 25 |
MOSI | 32 |
MISO | 39 |
CS | 33 |
IRQ | 36 |
The display and the SD card are on two other buses. The CYD project's troubleshooting notes say the ESP32 has two usable hardware SPI buses, so to use display, touch and SD card at once, one of them has to run in software. They recommend doing that for touch, for example with XPT2046_Bitbang_Slim. Configurations that expect touch to share the display's SPI bus will not see any touches on this board.
XPT2046 on Raspberry Pi 3.5″ displays
Linux has no separate XPT2046 driver. Raspberry Pi OS loads it through the ads7846 overlay, and the input device shows up as "ADS7846 Touchscreen". The overlay's main parameters:
penirq: the GPIO used for the pen interrupt. It is required.speed: SPI clock, 2 MHz by default and 3.25 MHz at most.xohms: X-plate resistance. The README says this usually needs setting to 60–100.pmax(often 255) andswapxyare also common.
The rest of the calibration can be done with xinput-calibrator, as the overlay README notes. One known conflict: the fbcp-ili9341 display driver cannot use XPT2046/ADS7846 touch, and its README says to remove touch overlays when using it. For other Pi display problems, see Raspberry Pi display not working.
Resistive or capacitive?
The XPT2046 suits low-cost panels and works with gloves or a stylus, but it needs calibration and ESPHome's driver reports a single touch point. If you need multi-touch and a glass front, look at capacitive controllers such as GT911 or FT6336. Our 3.5-inch TFT page compares the two options for that size.
Dev boards using XPT2046
- ESP32-3248S035 (3.5″ CYD)3.5″ · 320×480 · GT911 capacitive (C version) or XPT2046 resistive (R version)
- ESP32-2432S028R (CYD)2.8″ · 240×320 · XPT2046 resistive (separate SPI pins)
- ESP32-2432S0242.4″ · 240×320 · XPT2046 resistive (R version) or CST816S / CST820 capacitive (C version); the N version has no touch
XPT2046 troubleshooting & FAQ
Why are my XPT2046 touch coordinates wrong, offset or mirrored?
The XPT2046 reports raw 12-bit values, not pixels, and the range differs from panel to panel. Touch the screen edges, note the raw minimum and maximum for X and Y, and map them to your resolution; ESPHome makes these four calibration values mandatory. Then swap or mirror the axes to match the display rotation. A calibration where the minimum is larger than the maximum, as in the LCD-show 3.5″ file (3936 to 227), simply means that axis is reversed.
On the Cheap Yellow Display, why do touch, display and SD card not work together?
On the ESP32-2432S028R each of the display, touch and SD card sits on a different SPI bus, but the ESP32 has only two usable hardware SPI buses. The CYD project’s advice is to run touch on a software SPI library such as XPT2046_Bitbang_Slim, because touch needs little bandwidth. Touch is wired to CLK 25, MOSI 32, MISO 39, CS 33 and IRQ 36.
Why does XPT2046 touch register without a finger, or not at all?
Touch is decided by a pressure value, not by the X and Y readings. ESPHome computes it from the two pressure channels and reports a touch when it reaches the threshold, 400 by default; raise it for phantom touches and lower it if light presses are missed. Also note that the pen-interrupt pin changes state while channels are being read, which is why ESPHome polls that pin instead of wiring it as a true interrupt.
Sources
- ESPHome: XPT2046 touch screen controller
- ESPHome source: components/xpt2046/touchscreen/xpt2046.cpp
- ESP32-Cheap-Yellow-Display: PINS.md (touch screen pins)
- ESP32-Cheap-Yellow-Display: TROUBLESHOOTING.md (display, touch and SD on separate SPI buses)
- Raspberry Pi overlays README: ads7846
- goodtft/LCD-show: 99-calibration.conf-35-90
- fbcp-ili9341 README, copy in goodtft/LCD-show (XPT2046/ADS7846 touch not supported)