The XPT2046 reports raw 12-bit ADC values for the X and Y axes. These raw values do not automatically correspond to pixel coordinates on your screen — their mapping depends on how the touchscreen overlay is oriented relative to the display.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.
setRotation(n) remaps the raw values so that the touch coordinates align with the pixel coordinates produced by your TFT display library.
Rotation Values
Pass a value from0 to 3 to setRotation(). The default is 1, set at the class level before begin() is called. Each value applies a 90-degree step and an optional axis inversion, matching the rotation convention used by ILI9341_t3 and the Adafruit display libraries.
| Value | Reported x | Reported y | Orientation |
|---|---|---|---|
0 | 4095 − y_raw | x_raw | Portrait, connector at top |
1 | x_raw | y_raw | Landscape (default) |
2 | y_raw | 4095 − x_raw | Portrait, connector at bottom |
3 | 4095 − x_raw | 4095 − y_raw | Landscape, upside-down |
Basic Usage
setRotation() at any time after begin() — it takes effect immediately on the next getPoint() or readData() call.
Finding the Right Rotation
If touch coordinates feel inverted or mirrored, work through the four values systematically:Start with rotation 1
Call
ts.setRotation(1). Draw a crosshair at the centre of your display and touch it. Print p.x and p.y to the serial monitor.Touch the corners
Touch each corner of the screen and note the x/y values. The top-left corner should produce low x and low y; bottom-right should produce high x and high y for landscape orientation.
Check for mirroring
If x increases towards the left instead of the right, try
setRotation(3) (landscape, flipped). If x and y axes are swapped, switch to a portrait mode (0 or 2).Coordinate Scaling Example
Raw coordinates span 0–4095. To convert to screen pixels, scale linearly to your display resolution:The
map() function performs integer linear interpolation. For best accuracy, calibrate the minimum and maximum raw values by touching the actual corners of your specific display, since the active touch area rarely spans the full 0–4095 range.